I am not to sure if it helps but I have a class module for reading and
writing ini files. That way you won't need to store the settings, you can
just read them on the fly. I will send you a copy... Here is the code
though...
Class*****************
Option Explicit
Private c_strSection As String
Private c_strKey As String
Private c_strININame As String
Public Property Let Section(strSection As String)
c_strSection = strSection
End Property
Public Property Let Key(strKey As String)
c_strKey = strKey
End Property
Public Property Let ININame(strININame As String)
c_strININame = strININame
End Property
Public Function ReadINISettings() As Variant
Dim sDestination As String * 255
Dim lReturnVal As Long
On Error GoTo ErrorHandler
lReturnVal = GetPrivateProfileString(c_strSection, c_strKey, "", _
sDestination, Len(sDestination), c_strININame)
'lReturnVal will always equal the length of the returned string not
including vbNullChar 's at the end!!!
If lReturnVal <> 0 Then
sDestination = Left(sDestination, InStr(sDestination, Chr(0)) - 1)
'chr(0)=vbNullChar
ReadINISettings = Trim(sDestination)
Else
Err.Raise vbObjectError + 513 + 1001,
"clsINISettings.ReadINISettings", _
"Initialization File Error!"
End If
Exit Function
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description _
, vbCritical, "Initialization File Error"
End Function
Public Sub WriteINISettings(ByRef strWriteValue As String)
Dim lReturnVal As Long
On Error GoTo ErrorHandler
lReturnVal = WritePrivateProfileString(c_strSection, c_strKey,
strWriteValue, _
c_strININame)
If lReturnVal = 0 Then Err.Raise vbObjectError + 513 + 1001, _
"clsINISettings.WriteINISettings", "Initialization File Error!"
Exit Sub
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description _
, vbCritical, "Initialization File Error"
End Sub
Module*********************
Option Explicit
'API Function Declarations
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.dll" Alias
"WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
'Global Variables
Public INI_SETTINGS As clsINISettings
Public Sub Aut
pen()
Set INI_SETTINGS = New clsINISettings
INI_SETTINGS.ININame = ThisWorkbook.Path & "\" & "Settings.ini" 'pass
INI file location and name
End Sub
Public Sub Auto_Close()
Set INI_SETTINGS = Nothing
End Sub
User Form***********************
Private Sub UserForm_Initialize()
On Error GoTo ErrorHandler
With INI_SETTINGS
.Section = "StartUp"
.Key = "UID"
txtUID.Text = .ReadINISettings
.Key = "Server"
txtServerName.Text = .ReadINISettings
.Key = "DBName"
txtDBName.Text = .ReadINISettings
.Key = "Driver"
txtDriver.Text = .ReadINISettings
.Section = "Test"
.Key = "Test"
txtTest = .ReadINISettings
End With
Exit Sub
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "frmMain.FormLoad"
Err.Clear
End Sub
Private Sub cmdTestThat_Click()
With INI_SETTINGS
.Section = "Test"
.Key = "Test"
.WriteINISettings ("That")
End With
frmMain.Show
End Sub
Private Sub cmdTestThis_Click()
With INI_SETTINGS
.Section = "Test"
.Key = "Test"
.WriteINISettings ("This")
End With
frmMain.Show
End Sub