You can use an ini file quite easily, code follows plus a ouple of sample
calls.
Paste the following code into a class module (call the module cReg):-
' ****************************************
' cReg class code
' ****************************************
Option Explicit
Private 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
Private 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
Private Type MeProps
Filename As String
End Type
Private MP As MeProps
Property Get IniValue(Section As String, ValueName As String) As String
Dim lngRet As Long
Dim lpReturnedString As String
Dim nSize As Long
Dim lngTermChar As Long
Dim lngCount As Long
nSize = 255
If Len(Section) < 1 Or Len(ValueName) < 1 Then
lngTermChar = 2
Else
lngTermChar = 1
End If
Do
lngCount = lngCount + 1
nSize = nSize * lngCount
lpReturnedString = Space(nSize)
lngRet = GetPrivateProfileString(Section, ValueName, _
"", lpReturnedString, _
nSize, MP.Filename _
)
Loop While lngRet = (nSize - lngTermChar)
IniValue = Left(lpReturnedString, lngRet)
End Property
Property Let IniValue(Section As String, ValueName As String, Value As
String)
Dim lngRet As Long
Call CreatePath(MP.Filename)
lngRet = WritePrivateProfileString(Section, ValueName, _
Value, MP.Filename)
End Property
Property Let Filename(RHS As String)
Const FILE_TYPE = ".ini"
MP.Filename = Trim(RHS)
If LCase(Right(MP.Filename, Len(FILE_TYPE))) <> FILE_TYPE Then
MP.Filename = MP.Filename & FILE_TYPE
End If
End Property
Private Sub CreatePath(UDLFilename As String)
Dim varPath As Variant
Dim strTempPath As String
Dim intX As Integer
Const FOLDER_SEP = "\"
varPath = Split(UDLFilename, FOLDER_SEP, compare:=vbBinaryCompare)
If IsArray(varPath) Then
ReDim Preserve varPath(0 To UBound(varPath) - 1)
For intX = 0 To UBound(varPath)
strTempPath = strTempPath & varPath(intX) & FOLDER_SEP
If Len(Dir(strTempPath, vbDirectory)) < 1 Then
MkDir strTempPath
End If
Next
Else
Err.Raise 76
End If
End Sub
' ****************************************
' /cReg class code
' ****************************************
Sample calls for this are as follows
' ****************************************
' Use cReg to set an ini file value
' ****************************************
Private Sub Command1_Click()
Dim cR As cReg
Set cR = New cReg
With cR
.Filename = "c:\fred.ini"
.IniValue("Test", "Val1") = "MyValue"
End With
Set cR = Nothing
End Sub
' ****************************************
' /Use cReg to set an ini file value
' ****************************************
' ****************************************
' Use cReg to retrieve an ini file value
' ****************************************
Private Sub Command2_Click()
Dim cR As cReg
Dim strValue As String
Set cR = New cReg
With cR
.Filename = "c:\fred.ini"
strValue = .IniValue("Test", "Val1")
End With
Set cR = Nothing
MsgBox strValue
End Sub
' ****************************************
' /Use cReg to retrieve an ini file value
' ****************************************