Results 1 to 8 of 8

Thread: Writing to a file...

  1. #1

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428

    Unhappy 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
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  2. #2
    gaffa
    Guest
    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

  3. #3
    Junior Member
    Join Date
    Jun 2001
    Location
    Redcar, UK
    Posts
    24
    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.
    Last edited by Smithy; Jun 3rd, 2001 at 07:04 AM.
    "Three minutes thought would suffice to find this out; but thought is irksome and three minutes is a long time."


    "The supreme irony of life is that hardly anyone gets out of it alive."

  4. #4

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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?

    -Emo
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  5. #5
    Addicted Member stevess's Avatar
    Join Date
    May 2001
    Posts
    251
    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.

  6. #6

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428

    Talking

    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... 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
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  7. #7
    Junior Member
    Join Date
    Jun 2001
    Location
    Redcar, UK
    Posts
    24
    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
    "Three minutes thought would suffice to find this out; but thought is irksome and three minutes is a long time."


    "The supreme irony of life is that hardly anyone gets out of it alive."

  8. #8

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428

    Talking

    Thanks!

    -Emo
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width