All about "INI" files

Windows uses the concept of using an INI file to initialise and set parameters while opening an application.

We have used INI files for the applications we develop in much the same way that Windows uses these files. We use these to set the path of the last database accessed, user preferences, path statements, and to set global variables.

You will need to declare the two functions called GetPrivateProfileString() and WritePrivateProfileString() as shown below in the declarations section of your module...

Declare Function GetPrivateProfileString Lib "Kernel" (ByVal _  

lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault_

As String, ByVal lpReturnedString As String, ByVal nSize As Integer,_

ByVal lpFileName As String) As Integer

 

Declare Function WritePrivateProfileString Lib "Kernel" (ByVal _

 

lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString_

 

As Any, ByVal lpFileName As String) As Integer

Where

lpApplicationName is the name of the section

lpKeyName is the name of the key under this section

lpDefault is the default value for this key

lpReturnedString is the value that is read from the INI file

nSize is the max size for this string

lpFileName is the name of your INI file

lpString is the value you want to write under the key

In the StartUp code of your application, you should declare a variant which we usually call 'vardummy' and read in the values from your INI file. If your INI file is called MyApp.INI and has a section called [BackendMDB] with a key called MDBFile, then your code would look something like this:

Dim vardummy as variant

Dim strdbFileName as string

Dim strdbFileNameTemp as string

Dim strIniFile as string

On Error goto Your_Err_Rtn

strdbFileName = "MyDB.MDB"

strIniFile = "MyApp.INI"

vardummy = GetPrivateProfileString("BackendMDB", "MDBFile",_

 

strdbFileName, strdbFileNameTemp, 150, strIniFile)

 ....

and before you close your application, you may want to write the last used MDB to the INI so that the user opens with that as the BackEnd the next time. In that case, you need the following line (with the appropriate declarations...) 

vardummy = WritePrivateProfileString("BackEndMDB", "MdbFile",_

 

strdbFileName, sIniFile)

So you can see how this can be useful for applications that allow the user to switch between different back-end databases. To add functionality to this particular utility, you could use the Common Dialog Control to allow the user to select his own backend database and proceed with that.

In the same way that the backend databse has been set, you could initialise many more settings for your application...so get to it!!! It really is no big deal once you have copied and pasted this code into your application - be it an Access database or a VB application - it works! And it impresses the clients, too!!

 

Back to MS Access Tips

Perfect Paradigm - A Solutions Company