INI Wrappers
So you Want the Wrap...
'get form position frmAbout.Top = CDbl(GetINIString(gvAppIniFile, "About", "Top", "2520", False)) frmAbout.Left = CDbl(GetINIString(gvAppIniFile, "About", "Left", "2460", False)) frmAbout.Width = CDbl(GetINIString(gvAppIniFile, "About","Width", "6765", False)) frmAbout.Height = CDbl(GetINIString(gvAppIniFile, "About","Height","3825", False))
DatabaseType = GetINIString(gvAppIni File, "Database", "Type", "Paradox", True)
Select DatabaseType
Case “Paradox”
DoParadoxSetup
Case “Access”
DoAccessSetup
Case Else
ErrorMessage
End
End Select
vFileName = GetINIString(gvAppIniFile, "Other", "Filename", "", True)
There are other example on how to use this wrapper, but they include getting a whole section of the ini file or deleting a section (or key) out of the ini file.
Function GetINIString$(strFN$, strSN$, strKN$, strDefault$, AskForDefault As Integer)
' Function performs no. of ini file doings
' (depends on parameters passed)
' Parameters
' strFN - String File Name - Holds ini file
' strSN - String Section Name - Holds Section
' strKN - String Key Name - Holds Key name
' strDefault - Default string - Holds default string to use if key value not found
' AskForDefault - Boolean specifying if user’s asked to supply value if no default value/key value is found
' Return Value
' Function returns string value(depends on initial parameters)
Dim intLen% ' length ini string buffer
Dim x% ' loop variable
Dim intRes% ' return value,number chars or zero
Dim R$
Dim StrTIB As String*5000 'temp. ini buffer
intLen = 255
' test to see if we need to delete any section of the ini file
' occurs if key name set to null string
If strKN = "" Then
intRes = GetPrivateProfileString(strSN, 0&, "", StrTIB, Len(StrTIB), strFN)
GetINIString = Left$(StrTIB, intRes)
Else ' since we’re not deleting a section we must be trying to get a string - look for it
intRes = GetPrivateProfileString(strSN, strKN, "", StrTIB, Len(StrTIB), strFN)
If intRes = 0 Then ' if intRes = 0 didn’t find string - let’s see what’s next
Select Case strDefault0
Case "" ' no default value given-find out if need to ask user to supply value and loop until user supplies a value/presses cancel,
Do
If AskForDefault Then ' ask for a value
Msg = "Value Not Found - Please Enter"
R = InputBox((strSN & " " & strKN & Msg), "Error")
WriteINIString strFN, strSN, strKN, R
GetINIString = GetINIString (strFN, strSN, strKN, "", AskForDefault)
Else ' user hasn’t supplied value or pressed cancel so exit-value might be critical to program later,(to do-add parameter to function called Value Required-not allowed to exit unless value entered)
R = ""
Exit Do
End If
Loop While R = ""
Case Chr$(255) ' No default supplied & no need to ask for default value-return null string
GetINIString = ""
Case Else ' Not found value in ini file but default value been supplied so write it to ini file so we’ve it available in either other parts of program or next time program is started.
WriteINIString strFN, strSN, strKN, strDefault
GetINIString = GetINIString(strFN, strSN, strKN, strDefault, AskForDefault)
End Select
Else ' if here, we’ve found value in ini file- so return required part of temporary buffer to program
For x = 1 To intLen
If Asc(Mid$(StrTIB, x, 1)) = 0 Then Exit For
Next x
GetINIString = Left$(StrTIB, x - 1)
End If
End If
End Function
Most of it should be self explanatory, if not, read the API documentation about GetPrivateProfileString to get a clearer understanding of the API function. Basically the code does just one thing, it tests what the programmer wants and then writes the profile string into the ini file and also returning it for the function value.
![]()