Results 1 to 12 of 12

Thread: output with text files

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8

    output with text files

    i have this complicated (at least i think so :P) project for programming class: Create a program that loads and breaks up a text file into different fields.

    Private Sub Command1_Click()

    Do While Not EOF(1) And Left$(Line1, 10) <> "Card Name:"
    Line Input #1, Line1
    Text1 = Line1

    Loop
    End Sub

    that'll return the full line of text beginning with "card name:" but thats all i got for now, i need each line in a different text box so im getting stuck now, any help would be great!

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    VB Code:
    1. Do While Not EOF(1) And Left$(Line1, 10) <> "Card Name:"

    That line of code is NOT good! You do not want to keep reading the file if you reach eof. Your code requires both conditions to be true. What if eof is true but left$(Line1,10)<>"Card Name:" is not true? Then your program would try to keep reading the file even though it was already at the eof - crash! Never put another condition with eof. Eof should be the only test performed when reading a file to the end.

    As for your question.

    If you put a text box on a form and then copy and paste the same text box on the same form, vb will ask if you want to create a control array. (click on Yes!) A control array is just like an array, but with control.

    So the first text box you put on the form would be text1(0) and the next one would be text1(1) and so on . . .

    Assuming you had 20 text boxes and 20 lines in your imput file . . .

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strLineOfText As String
    3.     Dim i As Integer
    4.    
    5.     i = 0
    6.     Do While Not EOF(1)
    7.         Line Input #1, strLineOfText
    8.         Text1(i) = strLineOfText
    9.         i = i + 1
    10.     Loop
    11. End Sub

    I really don't want to do your homework because that wouldn't help you in the long run, but I hope that helps. If that example doesn't make sense, just let me know.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8
    i was thinking of using an array, my teacher told me i shouldn't have to but i guess my initial idea was right... thanks i'll try to work with what you've told me

  4. #4
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    You and your teacher are both right. An array would be easier (you are correct) but you also don't need to (she is correct).

    Good luck with the project and let us know if you run into any snags.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If your text file is delimited by commas or tabs you could look up
    the Split command and put it into an array and put each element
    into their respective text boxes.

  6. #6
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    india
    Posts
    418
    hi,

    why to take more text boxes.take one text box and display like this

    Dim strLineOfText As String
    Dim i As Integer

    Do While Not EOF(1)
    Line Input #1, strLineOfText
    text1.text=text1.text & vbcrlf
    Loop

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8
    armbruster: i tried the code and when i got an error i remembered trying something almost the same but gave up on the idea because of this error:

    Complie Error:
    Can't Assign to Read Only Property

    because of this line:

    Text1(i) = strLineOfText

  8. #8
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Ya know, it's always the easy stuff I screw up - the hard stuff is no problem !

    should be . . .
    VB Code:
    1. Text1(i)[COLOR=red].Text[/COLOR]  = strLineOfText
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8
    yikes, i wish i wasn't so bad at VB... but another error came up:

    Compile Error:

    Wrong number of arguments or invalid property assignment



  10. #10
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    post your code or zip your project . . .

    what line is highlighted when you get the error?
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8
    i included other attempts but leave them commented out in my program

    ERROR
    Compile Error:

    Wrong number of arguments or invalid property assignment
    /ERROR

    Dim strLineOfText As String
    Dim i As Integer
    Dim Line1 As String
    Option Explicit



    Private Sub Command1_Click()

    i = 0
    Do While Not EOF(1)
    Line Input #1, strLineOfText
    Text1(i).Text = strLineOfText
    i = i + 1
    Loop


    'Do While Not EOF(1)
    ' Line Input #1, strLineOfText
    ' Text1(i).Text = strLineOfText
    ' i = i + 1
    'Loop

    'Do While Not EOF(1) And Left$(Line1, 10) <> "Card Name:"
    ' Line Input #1, Line1
    ' Text1 = Line1
    'Loop
    End Sub

    Private Sub Form_Load()
    i = 0
    Open "H:\Visual Basic\Magic Database\od_spoiler_en.txt" For Input As #1
    End Sub

  12. #12
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Don't open your file until you need to and close it when you are done!

    VB Code:
    1. Option Explicit
    2.  
    3. Dim strLineOfText As String
    4. Dim i As Integer
    5. Dim strFile As String
    6.  
    7. Private Sub Command1_Click()
    8.     i = 0
    9.     strFile = "H:\Visual Basic\Magic Database\od_spoiler_en.txt"
    10.     Open strFile For Input As #1
    11.         Do While Not EOF(1)
    12.             Line Input #1, strLineOfText
    13.             Text1(i).Text = strLineOfText
    14.             i = i + 1
    15.         Loop
    16.     Close #1
    17. End Sub


    I suspected that you did not have a control array on your form so I created one text box and got the same error you got.

    Make sure you have several text boxes on your form with the same name!

    If you put a text box on a form and then copy and paste the same text box on the same form, vb will ask if you want to create a control array. (CLICK ON YES!!!!!) A control array is just like an array, but with control.
    You need to have a textbox control array for this to work!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

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