Windows doesn’t treat comments in INI files the same way VB does in code. Typically, comments must be on a single line of their own. When calling the GetPrivateProfileString function, if the requested entry contains a Rem statement-such as “; this is a rem’d statement”-the entire entry, value and comment, will be returned.
If your code anticipates the entry without comment, confusion could result from a user entering a comment following the entry rather than on a separate line. Use this VB code example to properly receive a return using the GetPrivateProfileString function call under any circumstance:
Private Declare Function GetPrivateProfileString _ Lib "kernel32" Alias _ "GetPrivateProfileStringA" (ByVal _ lpApplicationName As Any, ByVal lpKeyName As _ Any, ByVal lpDefault As String, ByVal _ lpReturnedString As String, ByVal nSize As _ Long, ByVal lpFileName As String) As LongPrivate Sub Form_Click() Dim IniString As String Dim sDefault As String Dim lReturn As Long sDefault = "n/a" ' allocate sufficient buffer IniString = String$(260, 0) lReturn = GetPrivateProfileString("DB", _ "Path", sDefault, IniString, _ Len(IniString), "c: est.ini") If lReturn > 0 Then IniString = Left$(IniString, lReturn) Debug.Print IniString ' added to strip out trailing comments If InStr(IniString, ";") > 0 Then IniString = Trim$(Left$(IniString, _ InStr(IniString, ";") - 1)) If InStr(IniString, vbTab) > 0 Then IniString = Trim$(Left$(IniString, _ InStr(IniString, vbTab) - 1)) End If End If Else IniString = sDefault End If Debug.Print IniStringEnd Sub