Link to home
Start Free TrialLog in
Avatar of AirOgre
AirOgre

asked on

How do I read/write files?

Hi,
I'm new to Visual Basic 6. I'm trying to create a program that needs to read and write many files. One of which is the options (.ini) file. How can I read and write certain lines and settings in that file?
Ex. Line 22 of a .ini file:
Close to System Tray: True

Also, I have another file which will store information much like a database. The format would be [Customer #],[Customer Name],[Customer Location] with a separator between records.
For example:
[1029],[John Doe],[New York]|||[1030],[Don Joe],[California]

There would need to be many many lines of these, so how can I read all these for presentation in a table like format? I'm trying to create an Access 2002 like database except using Visual Basic to show forms, tables, and queries while also having the ability to run it in the system tray, give alerts at certain times, access other files, etc.

Thanks in advance.

PS: I'm very very new to VB, so please keep it simple(if possible) =) If not I'll be able to figure it out.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

To read from text files, I recommend FSO. See a complete sample at http://support.microsoft.com/default.aspx?scid=kb;en-us;Q190882 

For the INI file, I suggest that you use a regular INI file (do not use : but use =). To read from this kind of file, use this:

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

Public Function ReadIniFile(ByVal pstrINIFile As String, _
                            ByVal pstrAppName As String, _
                            ByVal pstrKeyName As String, _
                            ByVal pstrDefault As String) As String
'This funciton is returning the value of a key in the INI file
Dim strReturn As String

    strReturn = Space$(255)
    If GetPrivateProfileString(pstrAppName, pstrKeyName, pstrDefault, strReturn, Len(strReturn), pstrINIFile) > 0 Then
        ReadIniFile = strReturn
    Else
        ReadIniFile = pstrDefault
    End If
End Function
Avatar of AirOgre
AirOgre

ASKER

Thanks, I'll try to do that and see what happens. Also, how can I read something in the format above? How can I program VB to ignore the "|||" as a separator in:
[1029],[John Doe],[New York]|||[1030],[Don Joe],[California]
You would need to search (using INSTR) for the position of these characters and use the MID function to retreive data.

OR

You can use the SPLIT function to create an array from your line:

arrX = split(strYourLine, "|||")
for i = 0 to ubound(arrx)
   msgbox arrx(i)
next
Avatar of AirOgre

ASKER

Thanks, I'll try to do that and see what happens. Also, how can I read something in the format above? How can I program VB to ignore the "|||" as a separator in:
[1029],[John Doe],[New York]|||[1030],[Don Joe],[California]
Avatar of AirOgre

ASKER

Hm I have no idea how I double posted, sorry about that. I'll try the split.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Force accepted as proposed

modulo

Community Support Moderator
Experts Exchange