Using a .ini file instead of registry entries

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm in a network here where access to writing anything to the registry is not
available - hence the getsetting, savesetting functions will not work.

I am trying to figure out some way to save a whole bunch of settings to a
local file and retrieve them when a form is loaded. For example, the settings
would include "Save Last Employee Number", "Popup when new message arrives"
etc etc...

Any ideas?
 
If they truly won't let you save to the user profile
in the registry where will you be allowed to save to?

The folder where the database is stored?

Just use the database to store your user settings in.
Capture the user name and use that as a key to your
settings table.

Or, did you mean that you want global settings?
Global Settings can't be stored with GetSetting,
SaveSetting, because those store to the user
profile. If you want global settings, just store
them to a table in your database.

(david)
 
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
' ****************************************
 
Fantastic! Works a charm!

Thank you so much!

Terry Kreft said:
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
' ****************************************
 
Each user will have a local front-end mdb file and the information is
gathered from a server directory (which is where the security permisisons /
info are kept too). The ini file would serve as a local-only settings file.

Cheers
 

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

Back
Top