Read and Write to .ini file

J

J Romine

I used to have a DB that read and wrote to an .ini file. The DB is now
located on a new PC after a year of inacvtivity and the read and write do
not work.
The following code is what I had all in the same module.

Declare Function GetModuleUsage% Lib "kernel32" (ByVal hModule%)
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 Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long

****************************************************************************
****************************
Function GetIniStr(app$, var$, file$, def$) As String

' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
' def$ is a default return string if the function is unable to find the
heading or variable. Could be 'ERROR' to let you know it failed.

Dim msg As String
Dim Response As Integer

On Error GoTo Errorcond

Dim temp As String
Dim x As Integer

temp$ = String$(145, 0) ' Size Buffer
x = GetPrivateProfileString(app, var, def, temp$, 145, file)
GetIniStr = Left$(temp$, x) ' Trim Buffer

Exit Function

Errorcond:
msg = "A problem has occurred while getting data from your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
****************************************************************************
****************************
Function SetIniStr(app$, var$, value$, file$) As Long
On Error GoTo Errorcond
' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
Dim msg As String
Dim Response As Integer

SetIniStr = WritePrivateProfileString(app, var, value, file)

Exit Function

Errorcond:
msg = "A problem has occurred while updating data in your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
 
J

J Romine

No error just does not write the info to the file.

TIA
J Romine

Douglas J. Steele said:
Other than the GetModuleUsage declaration (which you don't seem to be using
anyhow), everything looks okay.

What exactly do you mean by "do not work"? Do you get an error? If so,
what's the error? If there's no error, do you get the wrong results?

--
Doug Steele, Microsoft Access MVP



J Romine said:
I used to have a DB that read and wrote to an .ini file. The DB is now
located on a new PC after a year of inacvtivity and the read and write do
not work.
The following code is what I had all in the same module.

Declare Function GetModuleUsage% Lib "kernel32" (ByVal hModule%)
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 Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long
****************************************************************************
****************************
Function GetIniStr(app$, var$, file$, def$) As String

' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
' def$ is a default return string if the function is unable to find the
heading or variable. Could be 'ERROR' to let you know it failed.

Dim msg As String
Dim Response As Integer

On Error GoTo Errorcond

Dim temp As String
Dim x As Integer

temp$ = String$(145, 0) ' Size Buffer
x = GetPrivateProfileString(app, var, def, temp$, 145, file)
GetIniStr = Left$(temp$, x) ' Trim Buffer

Exit Function

Errorcond:
msg = "A problem has occurred while getting data from your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
****************************************************************************
****************************
Function SetIniStr(app$, var$, value$, file$) As Long
On Error GoTo Errorcond
' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
Dim msg As String
Dim Response As Integer

SetIniStr = WritePrivateProfileString(app, var, value, file)

Exit Function

Errorcond:
msg = "A problem has occurred while updating data in your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
 
D

Douglas J. Steele

Other than the GetModuleUsage declaration (which you don't seem to be using
anyhow), everything looks okay.

What exactly do you mean by "do not work"? Do you get an error? If so,
what's the error? If there's no error, do you get the wrong results?

--
Doug Steele, Microsoft Access MVP



J Romine said:
I used to have a DB that read and wrote to an .ini file. The DB is now
located on a new PC after a year of inacvtivity and the read and write do
not work.
The following code is what I had all in the same module.

Declare Function GetModuleUsage% Lib "kernel32" (ByVal hModule%)
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 Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long

****************************************************************************
****************************
Function GetIniStr(app$, var$, file$, def$) As String

' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
' def$ is a default return string if the function is unable to find the
heading or variable. Could be 'ERROR' to let you know it failed.

Dim msg As String
Dim Response As Integer

On Error GoTo Errorcond

Dim temp As String
Dim x As Integer

temp$ = String$(145, 0) ' Size Buffer
x = GetPrivateProfileString(app, var, def, temp$, 145, file)
GetIniStr = Left$(temp$, x) ' Trim Buffer

Exit Function

Errorcond:
msg = "A problem has occurred while getting data from your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
****************************************************************************
****************************
Function SetIniStr(app$, var$, value$, file$) As Long
On Error GoTo Errorcond
' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
Dim msg As String
Dim Response As Integer

SetIniStr = WritePrivateProfileString(app, var, value, file)

Exit Function

Errorcond:
msg = "A problem has occurred while updating data in your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
 
D

Douglas J. Steele

What value gets returned by the API call? If it's 0, then you have an error.

Try using the GetLastError API to determine what it is:

Public Declare Function GetLastError Lib "kernel32" () As Long

then in your code

SetIniStr = WritePrivateProfileString(app, var, value, file)
If SetIniStr = 0 Then
SetIniStr = GetLastError
End If


--
Doug Steele, Microsoft Access MVP



J Romine said:
No error just does not write the info to the file.

TIA
J Romine

Douglas J. Steele said:
Other than the GetModuleUsage declaration (which you don't seem to be using
anyhow), everything looks okay.

What exactly do you mean by "do not work"? Do you get an error? If so,
what's the error? If there's no error, do you get the wrong results?
****************************************************************************
****************************
Function GetIniStr(app$, var$, file$, def$) As String

' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
' def$ is a default return string if the function is unable to find the
heading or variable. Could be 'ERROR' to let you know it failed.

Dim msg As String
Dim Response As Integer

On Error GoTo Errorcond

Dim temp As String
Dim x As Integer

temp$ = String$(145, 0) ' Size Buffer
x = GetPrivateProfileString(app, var, def, temp$, 145, file)
GetIniStr = Left$(temp$, x) ' Trim Buffer

Exit Function

Errorcond:
msg = "A problem has occurred while getting data from your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
****************************************************************************
****************************
Function SetIniStr(app$, var$, value$, file$) As Long
On Error GoTo Errorcond
' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
Dim msg As String
Dim Response As Integer

SetIniStr = WritePrivateProfileString(app, var, value, file)

Exit Function

Errorcond:
msg = "A problem has occurred while updating data in your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization Error")

End Function
 
J

J Romine

I get a return value of 1

TIA.
J Romine

Douglas J. Steele said:
What value gets returned by the API call? If it's 0, then you have an error.

Try using the GetLastError API to determine what it is:

Public Declare Function GetLastError Lib "kernel32" () As Long

then in your code

SetIniStr = WritePrivateProfileString(app, var, value, file)
If SetIniStr = 0 Then
SetIniStr = GetLastError
End If


--
Doug Steele, Microsoft Access MVP



J Romine said:
No error just does not write the info to the file.

TIA
J Romine

write
do
****************************************************************************
****************************
Function GetIniStr(app$, var$, file$, def$) As String

' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
' def$ is a default return string if the function is unable to find the
heading or variable. Could be 'ERROR' to let you know it failed.

Dim msg As String
Dim Response As Integer

On Error GoTo Errorcond

Dim temp As String
Dim x As Integer

temp$ = String$(145, 0) ' Size Buffer
x = GetPrivateProfileString(app, var, def, temp$, 145, file)
GetIniStr = Left$(temp$, x) ' Trim Buffer

Exit Function

Errorcond:
msg = "A problem has occurred while getting data from your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization
Error")

End Function
****************************************************************************
****************************
Function SetIniStr(app$, var$, value$, file$) As Long
On Error GoTo Errorcond
' app$ is the INI file major heading, e.g., [Database], [Graphics], etc.
' var$ is the INI file variable name under the major heading, e.g.,
DatabaseName, ODBCstring, etc.
' file$ is the full path and filename for the INI file.
Dim msg As String
Dim Response As Integer

SetIniStr = WritePrivateProfileString(app, var, value, file)

Exit Function

Errorcond:
msg = "A problem has occurred while updating data in your .ini file.
Please contact your system administrator"
Response = MsgBox(msg, vbOKOnly + vbInformation, "Initialization
Error")

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top