-
Writing to a file...
I want to record the data inputed by a user in 7 textboxes to a txt file...
the code that I have is this...
Code:
Open "C:\File.txt" For Append As #1
Print #1, txtTime1.Text, txtTime2.Text, txtTime3.Text, txtTime4.Text, txtTime5.Text, txtTime6.Text, txtTime7.Text
Close #1
But for some reason, it doesn't record the data, it just puts spaces in the file.. there is nothing in it..
What am I doing wrong?
Thanks!
-Emo
-
You needto write just a single string to the file:
Code:
Dim lstrOut as String
lstrOut = Text1.Text & "," & Text2.Text & "," & Text3.Text 'And so on
Open "C:\File.txt" For Append As #1
Print #1, lstrOut
Close #1
And don't use #1 - use the FreeFile function instead to get a valid file handle
- gaffa
-
I found that this also worked fine, and formatted the entries into
columns within the files itself.
Code:
Option Explicit
Private Sub Command1_Click()
Dim iFileNum As Integer
iFileNum = FreeFile
Open App.Path & "\test.txt" For Append As iFileNum
Print #iFileNum, Text1.Text, Text2.Text, Text3.Text
Close #iFileNum
End Sub
I would also look towards using a control array of text box's.
-
gaffa, with you code all I got was ,,,,,,,
Smithy, with you code I got the same as me... nothing in text file and whole lotta spaces...
Am I doing this worng??? Why can it work with other people and it can't work with me? :confused:
-Emo
-
Smithy,
With the exception of FreeFile, that is exactly the same code that Emo posted orginally. So if it works for you then Emo must have another problem.
Emo,
Put a break point on the Print line and check the value of text in the textboxes.
-
Found something...
With gaffa's code... when I input everything in the textboxes and press the command button the first time it displays ,,,,,,, but then if I press it again, it works...:confused: I have no Idea how, but it does!
Thanks Gaffa!
By the way, how can make it write to a new line of the txt file whenever it reaches a point, let's say at the end of textbox7 it should go to a new line if something else is written afterwords?
Thanks!
-Emo
-
Just add vbCrlf (visual basic carriage return line feed) or vbNewLine to your code where you want it:
Code:
Print #iFileNum, Text1.Text, Text2.Text & vbNewLine & Text3.Text
-