Not a Member Yet,
Click here to Register
How do you like the new design?

ID: 340
Viewed: 2967
Added: Aug 19, 2002
Snippet uploaded by: snippet
Written By: Unknown
Demo: Sorry, no demo

User Rated at: 0 Stars
Rate This:

Thank you for your vote. Please wait...

It appears you already voted for this snippet

It appears your vote value was empty

here is how you can store and retrieve info (in this case scores) from an ini file. Just create a new standard code module and put this code into it:

Now in your sub main or form load call ReadScores and in your form unload call SaveScores. The resulting ini file will look like:


Have fun

Highlight all by clicking in box
Option Explicit 
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
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
' <<< Public Declarations >>>
Public g_tbl_Scores() As String
'table to hold scores
' <<< Local Constants >>>
Const m_int_SCORES As Integer = 9
'number of scores to track
' <<< Public Functions >>>
Public Function ReadINI(Section, KeyName, FileName As String) As String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfileString(Section, ByVal KeyName, "", sRet, Len(sRet), FileName))
End Function
Public Function writeini(sSection As String, sKeyName As String, sNewString As String, sFileName) As Integer
Dim variable 'doesn't seem to matter
variable = WritePrivateProfileString(sSection, sKeyName, sNewString, sFileName)
End Function
Public Function NormPath(sPath As String, Optional sSeperator As String = "") As String
If Right(sPath, Len(sSeperator)) = sSeperator Then
NormPath = sPath
NormPath = sPath & sSeperator
End If
End Function

Highlight All
Public Sub ReadScores() 
'this sub will load the scores from an ini file into 'an array
Dim sFile As String
Dim iControl As Integer
sFile = NormPath(App.Path) & "HighScores.ini"
ReDim g_tbl_Scores(m_int_SCORES) 'resize array
For iControl = 0 To m_int_SCORES g_tbl_Scores(iControl) = ReadINI("HighScore", _ "Score." & iControl, sFile)
Next iControl
End Sub
Public Sub SaveScores()
'this sub will save scores from an array
Dim sFile As String
Dim iControl As Integer
sFile = NormPath(App.Path) & "HighScores.ini"
For iControl = 0 To m_int_SCORES
writeini "HighScore", "Score." & iControl, _ g_tbl_Scores(iControl), sFile
Next iControl
End Sub

No Comments to show

Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.

Replying to a Comment...

Adding your comment. Please wait...

Thanks for adding your comment!. After further review it will be added.

There was a problem adding your comment. Please try again.

Please complete all the fields in the form before sending.

© 2002 - 2017 All Rights Reserved. Conditions