--------------------------------------------
Code:
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Function ReadWriteINI(Mode As String, tmpSecname As String, tmpKeyname As String, Optional tmpKeyValue) As String
On Error GoTo ReadWriteINIError
Dim AppPath As String
Dim tmpString As String
Dim FileName As String
Dim secname As String
Dim keyname As String
Dim KeyValue As String
Dim anInt
Dim defaultkey As String
AppPath = App.Path
' ******* 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 FILENAME ***************************
FileName = AppPath & "\IniFile.ini"
' ******* 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
' ******* READ 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
ReadWriteINIError:
MsgBox Error
Stop
End Function
Public Sub Initialise_local_variables()
LocalVar1 = App.Path & ReadWriteINI("GET", "SECTION_A", "key_a"
LocalVar2 = App.Path & ReadWriteINI("GET", "SECTION_B", "key_b"
End sub
--------------------------------------------
The local variables are declared in a module.
When I want to fill LocalVar2 the application gives the overflow-error.
--------------------------------------------
this is the ini-file:
[SECTION_A]
key_a = "Test1"
Key_b = "Test2"
--------------------------------------------
Any help is welcome!
Thanx in advance!