Log in

View Full Version : Programming language need help



misscomp9
02-27-2011, 02:54 PM
hy can anyone help me how to read data from notepad by using VB

i've tried this code but it wont work.. or can you edit or tell me what is my mistake here..

here are eg data in notepad
name id no sex maritalstatus
emma 011 f single
jimmy 022 m married
lim 033 m single


when i entered the code in VB, i can only read 1 line of my data. for e.g only emma details are came out.

Private Sub Command1_Click()
Dim name As Integer
Dim idno As String
Dim sex As String
Dim maritalstatus As Currency

Open "C:\Users\Documents\Data.Txt" For Input As #1

Input #1, strName, intidno, Strsex, strmarital status 'Read four variables from the file
Print strName, intidno, Strsex, strmarital status 'Print the Variables
Close #1 'Close the file
End Sub


how can i read data line by line by using loop. can anyone give me other example im newbie. thnks in advance.

Paul_Hossler
02-28-2011, 02:15 PM
I did change variable names (name is a reserved word)

FreeFile() gets the next file handle (not needed if you can keep it streight, but I like to play it safe)

EOF() returns a True/False if you've read the last record


Option Explicit
Private Sub Command1_Click()
Dim sName As Integer
Dim sID As String
Dim sSex As String
Dim sMaritalStatus As String
Dim iFile As Long

iFile = FreeFile

Open "C:\Users\Documents\Data.Txt" For Input As iFile

While Not EOF(iFile)
Input #iFile, sName, sID, sSex, sMaritalStatus
Debug.Print sName, sID, sSex, sMaritalStatus
Wend
Close #iFile 'Close the file
End Sub


Paul

PS. Complied, but not tested