Table of contents
Collapse the table of content
Expand the table of content

Calling the Windows API (Differences in String Function Operations)

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

Applies to: Access 2013 | Access 2016

The memory storage formats for text differ between Visual Basic for Applications (VBA) code and Access Basic code. (Access Basic was used in early versions of Microsoft Access.) Text is stored in ANSI format within Access Basic code and in Unicode format in Visual Basic. This topic discusses one potential issue when handling strings in the current version of Microsoft Access. For more information, see Differences in String Function Operations.

In several Windows API functions, the byte length of a string has a special meaning. For example, the following program returns a folder set up in Windows. In Microsoft Access, LeftB (Buffer, ret) does not return the correct string. This is because, in spite of the fact that it shows the byte length of an ANSI string, the LeftB function processes Unicode strings. In this case, use the InStr function so that only the character string, without nulls, is returned.

Private Declare Function GetWindowsDirectory Lib "kernel32" _ 
 Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _ 
 ByVal nSize As Long) As Long 

Private Sub Command1_Click() 
 Buffer$ = Space(255) 
 ret = GetWindowsDirectory(Buffer$, 255) 
 ' WinDir = LeftB(Buffer, ret) '<--- Incorrect code" 

 WinDir = Left(Buffer$, InStr(Buffer$, Chr(0)) - 1) 
 '<--Correct code" 
 Print WinDir 
End Sub

Access for developers forum on MSDN
Access help on
Access help on
Search for specific Access error codes on Bing
Access forums on UtterAccess
Access wiki on UtterAcess
Access developer and VBA programming help center (FMS)
Access posts on StackOverflow

© 2018 Microsoft