INI Wrappers

So you Want the Wrap...

  1. You need to set-up a default section in the Ini file (startup form layout, file locations etc.). As an example the code below would place your About or splash screen form at the required and default locations. In this way you don't have to supply the ini file, as the values will be written to it at the initial start-up of the application. And, if required, you could delete the ini file and restart with a set of default values.
      '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))
    

  2. You need to set up a database to use. As you don't know in advance which database the user requires you could do as the code demonstrates.

      DatabaseType = GetINIString(gvAppIni File, "Database", "Type", "Paradox", True)
      Select DatabaseType 
        Case “Paradox”
          DoParadoxSetup
        Case “Access”
          DoAccessSetup
        Case Else
          ErrorMessage
          End
      End Select
    

  3. You need to know the location of a particular file on a network, but don't want to write a default value to the ini file;

      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.


But now to the code...
  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.


[HOME] [TABLE OF CONTENTS] [SEARCH]