PDA

View Full Version : need help for Visual Basic Code



misscomp9
02-27-2011, 02:56 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.

mdmackillop
02-27-2011, 03:33 PM
Set FSobj = CreateObject("Scripting.FileSystemObject")
Set InputTextFile = FSobj.OpenTextFile("C:\_SrcUtilTest\Test\TEXTtest.txt", ForReading, False)
Do While Not InputTextFile.AtEndOfStream
TempText = InputTextFile.ReadLine
Loop
InputTextFile.Close
Set InputTextFile = Nothing
Set FSobj = Nothing

JP2112
03-01-2011, 02:01 PM
I like the GetText function from Charley Kyd: http://www.exceluser.com/explore/questions/vba_textcols.htm

This function will open your text file and split each line into an array:


Sub getfiletest()
Dim str As String
Dim lines() As String
Dim i As Long
str = GetText("C:\Users\Documents\Data.Txt")
' split each line into its own array element
lines = Split(str, vbCrLf)
For i = LBound(lines) To UBound(lines)
Debug.Print lines(i)
Next i
End Sub

frilantika
04-25-2011, 07:51 PM
how to coded the animation of color?