Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retreive from, and write to .ini files from Access

VBA and Custom Functions

How to retreive from, and write to .ini files from Access

by  jimmythegeek  Posted    (Edited  )
.ini Files are great to use if you want to store data, or retrieve data from somewhere outside of a database. Let's say you want to distribute an application, but you need to be able to change variable data periodically, and it would be nice not to have to mess with the database. .ini Files work great. They are easy to copy and replace, e-mail, put on disk, and if you use them, you never have to touch the database to adjust values.

You can retrieve values from existing .ini Files, or create your own. In this example, I will create my own. It works great, I do it all the time, and easy to use.

I will retreive a tax rate for sake of simplicity, but you can use any value:

-----
First, I open notepad, and add my Section, Key and value:

Example:
[Section]
Key=Value

So for me, it would be:

[Data]
Taxrate=.082
Markuprate=.55

Note: You can have as many sections, keys and values in an .ini as you like. Also, I named my first section "Data", you could name it anything you want
Now I am going to save the file as dbData.ini (make sure you add the .ini when saving)

Now Copy everything between the double lines below into a new module: Trust me, it will work. Save the module as anything you want

==============================
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function GetINI(sINIFile As String, sSection As String, sKey As String, sDefault As String) As String
'Purpose: Returns a value FROM an INI File
'GetINI(Path of INI File, Name of section, Name of Key, Default value if not found)
'Example: GetINI("C:\WINNT\ACROREAD.ini", "AdobeViewer", "MaxApp", "0")

Dim sTemp As String * 256
Dim nLength As Integer

sTemp = Space$(256)
nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, 255, sINIFile)
GetINI = Left$(sTemp, nLength)
End Function
Public Sub WriteINI(sINIFile As String, sSection As String, sKey As String, sValue As String)
'Purpose: Writes value TO an INI File
'GetINI(Path of INI File, Name of Section, Name of Key, Value)
'Example: WriteINI("C:\WINNT\ACROREAD.ini", "AdobeViewer", "AntialiasThreshold", "25")

Dim iCounter As Integer
Dim sTemp As String

sTemp = sValue

'Replace any CR/LF characters with spaces
For iCounter = 1 To Len(sValue)
If Mid$(sValue, iCounter, 1) = vbCr Or Mid$(sValue, iCounter, 1) = vbLf Then Mid$(sValue, iCounter) = " "
Next iCounter

iCounter = WritePrivateProfileString(sSection, sKey, sTemp, sINIFile)
End Sub
==============================

OK, now to retrieve the value you use the GetINI function. You can just retrieve the value, or set a variable equal to it, or even set a public variable equal to it on open of your application, then use it throughout, it's up to you.

Dim myVar As Single (Dim as whatever you want, depends on what type of data you are retrieving)
myvar = GetINI("C:\My Documents\dbData.ini", "Data", "Taxrate", "0")
The "0" is the value if not found

To write a value out, us the WriteINI function:

WriteINI("C:\My Documents\dbData.ini", "Data", "Taxrate", "YourValue")

This is assuming your dbData.ini file is located in your "My Documents" folder. You could use constants here for paths, as well as the db.Name function stripping off the database name to retrieve the path of the database.

That's all there is to it. You can copy this module from database to database, and use .ini Files all you want.

Note: Actually, the files don't have to be .ini extensions, they can end with anything you want. but you have to follow the Section, Key, Value layout.

==========================

Jim Lunde
jimlunde@gmail.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top