Edit

Share via


Calling the Windows API (differences in string function operations)

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

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.