views:

3001

answers:

4

Are any methods available in VBA to read and write INI files? I know I could use;

Open "C:\test.ini" For Input As #1

...and parse the data. Instead I am trying to see what tools are already available.

I know in C# you can do...

 using INI;
 INIFile ini = new INIFile("C:\test.ini");

Is there an equivalent for VBA?

I am attempting this in MS Access 2003 VBA.

+4  A: 

It's not pleasant, but you can use the Windows API. Here's a link, and another from MS.

Jon B
+1  A: 

FileSystemObject object with a TextStream is the usually recommended method for reading and writing textfiles in VBA.

Remou
Really? Why would you use an outside library (I assume late binding, of course) for something that VBA provides natively? What are the advantages?
David-W-Fenton
It makes the code read easier, in my opinion. And you can use For ... Each to iterate through folders easily.
HardCode
It is probably a good idea not to read and write them directly. The API approach is much better because it abstracts the need to understand the formatting of the INI file. No sense in writing code to understand INI files when it already exists.
JohnFx
+1  A: 

Karl Peterson's kpini has just about everything you're likely to need: API declarations, a Cinifile class, stuff like that. I'd start with that and morph it to taste, which shouldn't take long.

Mike Woodhouse
+3  A: 

Here are some code snippets that we use, it should help you to get the idea. These routines use the API calls. Two functions are included to read / write a string setting to a specific section in the ini file.

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 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 IniFileName() As String
  IniFileName = "c:\[yourpath here]\settings.ini"
End Function


Private Function ReadIniFileString(ByVal Sect As String, ByVal Keyname As String) As String
Dim Worked As Long
Dim RetStr As String * 128
Dim StrSize As Long

  iNoOfCharInIni = 0
  sIniString = ""
  If Sect = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
  Else
    sProfileString = ""
    RetStr = Space(128)
    StrSize = Len(RetStr)
    Worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, IniFileName)
    If Worked Then
      iNoOfCharInIni = Worked
      sIniString = Left$(RetStr, Worked)
    End If
  End If
  ReadIniFileString = sIniString
End Function

Private Function WriteIniFileString(ByVal Sect As String, ByVal Keyname As String, ByVal Wstr As String) As String
Dim Worked As Long

  iNoOfCharInIni = 0
  sIniString = ""
  If Sect = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
  Else
    Worked = WritePrivateProfileString(Sect, Keyname, Wstr, IniFileName)
    If Worked Then
      iNoOfCharInIni = Worked
      sIniString = Wstr
    End If
    WriteIniFileString = sIniString
  End If
End Function
birger