Help with writing to simple data text file

krolm
04-11-2003, 04:18 PM
Hello all, first of all I'd like to say that I'm very new to visual basic, we just started working on it at school, and we have to make a project but I can't seem to get it to work. I tried searching the forum for answer to my question but without success.

Here it is:

I'm trying to make a simple kind of phonebook, using 2 forms: one for reading from a text based datafile, and one for writing to it.
The forms look like this, 4 textboxes, (first name, last name, phone number, adress), and a commandbutton to set it in motion.

The datafile is quite simple. A text file made with notepad, looking kind of like this..

2
John, Johnson, 555-4444, Johnstreet 6
Jack, Jackson, 666-5555, Jackstreet 9

the 2 would be the number of records, and the comma's seperate the different data. Now, for Reading the data from the file and displaying it in the proper textboxes, I have this code:

Private Sub Command1_Click()
Dim number As Integer
Dim n As Integer
Dim name(1000000#) As String
Dim phonenr(1000000#) As String
Dim lastname(1000000#) As String
Dim bdate(1000000#) As String
'1000000# is the maximum number of records that can be in the file

Open "C:\file.aaa" For Input As #1
'file.aaa would be the textfile
Input #1, number

For n = 1 To number
Input #1, name(n), lastname(n), bdate(n), phonenr(n)
Next n

Close #1

'now display in proper textboxes
For n = 1 To number
If name(n) = Text1 Then
Text2 = lastname(n)
Text3 = bdate(n)
Text4 = phonenr(n)
End If

If lastname(n) = Text2 Then
Text1 = name(n)
Text3 = bdate(n)
Text4 = phonenr(n)
End If

If bdate(n) = Text3 Then
Text1 = name(n)
Text2 = lastname(n)
Text4 = phonenr(n)
End If

If phonenr(n) = Text4 Then
Text1 = name(n)
Text2 = lastname(n)
Text3 = bdate(n)
End If

Next n

End Sub
-----------
(end of code)

This works fine, now the problem is, how can I make it write the strings the user types in the 4 textboxes to the bottom of the file and change the number of records that it holds? I tried to change the code using Output and Write instead of input but I seem to be doing something wrong.. Thank you for any help you can give me.

PWNettle
04-11-2003, 05:20 PM
You couldn't really update the number of records you are storing in the text file without completely rewriting the text file (which you could do since you store everything when you read it).

You don't really need to store the number of records in your text file. It'd be better to use an EOF (end of file) loop to read in your data...

Do While Not EOF(yourfilehandlenumber)
' data reading code here
intRecords = intRecords + 1 ' counter
Loop

and have a counter in your loop to count track the number of records (if you really need that value for anything).

If you didn't have that number of records value at the top of your file you could simply open the file in APPEND mode to save data to the end of the file (when you wanted to save new records).

Paul

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum