
Originally Posted by
Demon
What techniue would you use to store a "cookie" in VB? I set a drive, folder and filename in a form and I'd like to save that value between sessions.
I use INI files... that's mainly what they're for. Here's a module I use for them. Just add it to your project and use WriteProfileString & FetchProfileString functions to write and read to the INI file.
Code:
'ModINIFunctions.bas
Option Explicit
Private msWinPath As String
Private mlRetLength As Long
Private msDirReturn As String
Private 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
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private 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
Public Function FetchProfileString(ByVal sINIFile As String, ByVal sApp As String, _
ByVal sKey As String, Optional DefVal As Variant) As String
'Purpose: Reads information from a *.INI file
'Inputs: sINIFile String Name of file INI file including path
' sApp String Name of application section in INI file
' Example [My App]
' sKey String key name under application section
' DefValue String default value of key
'
' Example [MyApp]
' MyKey = MyValue
'Assumptions:
' 1. Windows API GetPrivateProfileString function declared with the following statement in the
' Private 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
'
'Effect: None
Dim sReturn As String
Dim sDefVal As String
Dim mlReturnLength As Integer
If IsMissing(DefVal) Then
sDefVal = ""
Else
sDefVal = CStr(DefVal)
End If
' msDirReturn = Dir(sINIFile)
' If msDirReturn = "" Then
' mlRetLength = WritePrivateProfileString(sApp, sKey, sDefVal, sINIFile)
' End If
sReturn = String$(255, 0)
mlRetLength = GetPrivateProfileString(sApp, sKey, sDefVal, sReturn, 255, sINIFile)
FetchProfileString = Left$(sReturn, mlRetLength)
End Function
Public Function WriteProfileString(ByVal sINIFile As String, ByVal sApp As String, _
ByVal sKey As String, ByVal sVal As String) As Boolean
'Purpose: stores information to a *.INI file
'Inputs: sINIFile String Name of file INI file including path
' sApp String Name of application section in INI fil
' Example [My App]
' sKey String key name under application section
' sValue String value of key
'
' Example [MyApp]
' MyKey = MyValue
'Assumptions:
' 1. Windows API WritePrivateProfileString function declared with the following statement in the
' Private 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
' 2. if *.INI file does not exist this function will create the file
'Effect: Contents of *.INI file
Dim sReturn As String
mlRetLength = WritePrivateProfileString(sApp, sKey, sVal, sINIFile)
End Function
Here's an example of reading in the language value under the Section named SAP in the Pack.ini file.
Code:
PstrLang = FetchProfileString(App.Path & "/PACK.INI", "SAP", "LANGUAGE", "EN")
Here's an example of how the data in the INI file would look.
Code:
;comments use the semicolon
;In this example parameter sApp = SAP
;parameter sKey = Language (there can be multiple keys under one app parameter)
;The value returned to PstrLang in the example above is EN
[SAP]
LANGUAGE=EN
This is a really useful way of storing different user preferences etc.
Another method would be writing and reading to the registry. But I'll let you explore that option.
Bookmarks