Read INI Files??

  • Thread starter Thread starter Jason Gyetko
  • Start date Start date
J

Jason Gyetko

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
 
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
 
Thanks, works great!!

Dan Artuso said:
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


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
 
I am trying to get the GetPrivateProfileString to work, but am having
difficulties. here is my setup

Module1.bas General Declaration
Declare Function GetPrivateProfileString% Lib "kernel32.DLL" (ByVal
lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnString$, ByVal
nSize%, ByVal lpFileName$)


Code in Form (always errors out and goes to FileError.)

on error go to FileError:

Valid% = GetPrivateProfileString("AutoPrint", "database", lpDefault$,
lpReturnString$, Size%, "c:\winnt\autoprint.ini")

FileError:
MsgBox "Can't find startup ini file", 16, "Error lpReturnString"
Resume Ne

autoprint.ini contents
[AutoPrint]
database = biometric

This always goes to FileError, Is there a Reference that I need to add to my
project or am I not call it correct?




Dan Artuso said:
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
 
Your declaration is wrong. % is Integer, and the 32-bit version requires
Long.

Personally, I think you'd be far better off using Dan's declaration, rather
than the deprecated type-declaration character, but if you insist, replace
the % with &.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



molonede said:
I am trying to get the GetPrivateProfileString to work, but am having
difficulties. here is my setup

Module1.bas General Declaration
Declare Function GetPrivateProfileString% Lib "kernel32.DLL" (ByVal
lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnString$, ByVal
nSize%, ByVal lpFileName$)


Code in Form (always errors out and goes to FileError.)

on error go to FileError:

Valid% = GetPrivateProfileString("AutoPrint", "database", lpDefault$,
lpReturnString$, Size%, "c:\winnt\autoprint.ini")

FileError:
MsgBox "Can't find startup ini file", 16, "Error lpReturnString"
Resume Ne

autoprint.ini contents
[AutoPrint]
database = biometric

This always goes to FileError, Is there a Reference that I need to add to my
project or am I not call it correct?




Dan Artuso said:
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


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
 
Back
Top