Hi,
Put all this code into a standard module. Watch out for line your newsreader wrapping the lines.
All of the Declares should be on one line.
Public Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal
lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
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
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String,
ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
'
Function ReadWriteINI(INIFile As String, Mode As String, tmpSecname As String, tmpKeyname As String, Optional tmpKeyValue) As String
Dim tmpString As String
Dim FileName As String
Dim secname As String
Dim keyname As String
Dim keyvalue As String
Dim anInt As Long
Dim defaultkey As String
On Error GoTo ReadWriteINIError
'Mode should be WRITE or GET
'Set the return value to OK
ReadWriteINI = "OK"
'Test for good data to work with
If IsNull(Mode) Or Len(Mode) = 0 Then
ReadWriteINI = "ERROR MODE" 'Set the return value
Exit Function
End If
If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then
ReadWriteINI = "ERROR Secname" 'Set the return value
Exit Function
End If
If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then
ReadWriteINI = "ERROR Keyname" 'Set the return value
Exit Function
End If
'Set the INI file name
FileName = INIFile
'WRITE MODE
If UCase(Mode) = "WRITE" Then
If IsNull(tmpKeyValue) Or Len(tmpKeyValue) = "0" Then
ReadWriteINI = "ERROR KeyValue"
Exit Function
Else
secname = tmpSecname
keyname = tmpKeyname
keyvalue = tmpKeyValue
anInt = WritePrivateProfileString(secname, keyname, keyvalue, FileName)
End If
End If
'GET MODE
If UCase(Mode) = "GET" Then
secname = tmpSecname
keyname = tmpKeyname
defaultkey = "Failed"
keyvalue = String$(50, 32)
anInt = GetPrivateProfileString(secname, keyname, defaultkey, keyvalue, Len(keyvalue), FileName)
If Left(keyvalue, 6) <> "Failed" Then 'Got it
tmpString = keyvalue
tmpString = RTrim(tmpString)
tmpString = Left(tmpString, Len(tmpString) - 1)
End If
ReadWriteINI = tmpString
End If
Exit Function
'In case of error
ReadWriteINIError:
MsgBox Err.Description
End Function
Now you can call the function like this:
strVar = ReadWriteINI("fullPathToYourIniFile", "Get", "TABS", "Tab1Visible")
the value of strVar would now be "Y"
--
HTH
Dan Artuso, Access MVP
Jason Gyetko said:
I would like to be able to read an ini file then configure my form according
to the settings in the ini file.
Is there a function that I can use where I specify (using the file below)
MyINI.ini, [TABS], TabVisible to have the value ("Y") returned? Thanks.
MyINI.ini file:
[TABS]
Tab1Visible=Y
Tab2Visible=Y
Tab3Visible=N
Tab1Caption=Tab1
Tab2Caption=Tab2
Tab3Caption=Tab3
[BUTTONS]
cmd1Visible=Y
cmd2Visible=Y
cmd3Visible=N
cmd1Caption=Button1
cmd2Caption=Button2
cmd3Caption=Button3