Click to See Complete Forum and Search --> : INI files
cjwares
Dec 13th, 1999, 12:38 PM
Does anyone have any working code for getting data from an INI file? please help.
AlanTodd
Dec 13th, 1999, 03:40 PM
Hi,
Here is some quick code I used in a simple project. Basically I load all the info from the INI file at the time the main form loads. These are then held in global variables so the data is available quickly at any time in the application.
A couple of other things to note, I initialise all the variables with my default values, so that if the INI file does not exist or there are any missing values it will not cause a problem later on.
CODE:
Sub GetINI()
Dim InBuf As String, Eq As Integer
Dim Key As String, KeyValue As String
DataFile = "Hotline.mdb"
AttacheData = "Attache.mdb"
Ho0 = 897796
Ho1 = vbBlack
Ho2 = vbBlue
WeC = vbRed
On Error GoTo NoINI
Open "Hotline.ini" For Input As #1
On Error GoTo 0
Do Until EOF(1)
Line Input #1, InBuf
Eq = InStr(1, InBuf, "=")
If Eq > 0 Then
Key = Left$(InBuf, (Eq - 1))
KeyValue = Mid$(InBuf, (Eq + 1), Len(InBuf))
Select Case UCase$(Key)
Case "DATABASE"
DataFile = KeyValue
Case "ATTACHEDATA"
AttacheData = KeyValue
Case "CURRENTCOLOR"
Ho0 = Val(KeyValue)
Case "COMPLETEDCOLOR"
Ho1 = Val(KeyValue)
Case "TASKCOLOR"
Ho2 = Val(KeyValue)
Case "WEEKENDCOLOR"
WeC = Val(KeyValue)
End Select
End If
Loop
Close #1
Exit Sub
NoINI:
End Sub
This is very simple and very reliable.
Alan
hayessj
Dec 13th, 1999, 03:51 PM
This is code that uses the Win32 API, if you change it a little it should work for you. Put the code below in a standard module in your project and change the name of the ini file and section it reads as required.
Option Explicit
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
Public 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
' Writes to the ini file XFer.ini in the application directory for a given key and value
Public Sub gWriteIniFile(ByVal sKey As String, ByVal sValue As String)
Dim sFileName As String ' Ini file path
Dim nRet As Long ' Api result
sFileName = App.Path & "\XFer.ini"
nRet = WritePrivateProfileString("XFer", sKey, sValue, sFileName)
End Sub
' Reads from the ini file XFer.ini in the application directory for a given key
Public Function gsReadIniFile(ByVal sKey As String) As String
Dim sBuffer As String ' String to pass to api call
Dim nBufferSize As Integer ' Size of sBuffer
Dim sFileName As String ' Ini file path
Dim nValid As Integer ' No of bytes read
On Error GoTo Err_gbReadIniFile
sBuffer = Space$(255) ' Pad string to pass to api
nBufferSize = Len(sBuffer) ' store length
sFileName = App.Path & "\XFer.ini"
' Get value from INI file
nValid = GetPrivateProfileString("XFer", sKey, "", sBuffer, nBufferSize, sFileName)
' Discard the trailing spaces and null character.
gsReadIniFile = Left$(sBuffer, nValid)
Exit Function
Err_gbReadIniFile:
gsReadIniFile = ""
End Function
Serge
Dec 13th, 1999, 06:13 PM
To get the data from INI file, first you have to know the structure of that file first. All INI files have the structure shown below:
[Section Name]
KeyName = Value
So lets say you have an INI file with this structure and this file called C:\MyINI.ini:
[Person]
Name=John
Now using GetPrivateProfileString API you can easilly get the value "John"
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
'----Put this on any event you want
Dim strBuffer As String
Dim lRet As Long
strBuffer = Space(125)
lRet = GetPrivateProfileString("Person", "Name", "No Name", strBuffer, Len(strBuffer), "C:\MyINI.ini")
If lRet Then strBuffer = Left(strBuffer, lRet)
MsgBox "This person's name is " & strBuffer
------------------
Serge
Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.