Results 1 to 23 of 23

Thread: Reading lines from a text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Resolved Reading lines from a text file

    First of all, I am far from experienced so if there are any obvious mistakes, it's because I don't know any better.

    I have a text file in this format:

    Code:
            
           A       B
    1     356     234
    2     275     597
    3     365     674
    4     286     375
    5     256     752
    6     674     465
    On the form, I have a text box where a user can enter a number. How can I make it so that when a user enters 2 and clicks a button, it will store 275 as variable x and 597 as variable y?

    I've got some code that I've been playing with, but I just can't put it together. I don't think it's even worth putting up because it doesn't really do anything. Here's part of it, the rest is just a mess that I don't understand.

    Code:
    Private Sub Command_Click()
    Dim varCount As Integer
    Dim varTemp As String
    Open "C:\MyFile.txt" For Input As #1
    Do Until EOF(1)
    varCount = varCount + 1
    Line Input #1, varTemp
    Loop
    Close #1
    End Sub
    I know I need to use the Split() function somewhere, and that the actual lines have to be stored in a array. I just can't figure out how to incorporate that. I would actually like something like jumping to a line in the text file or something because I think it would be quicker and easier. The text file will contain around 400 lines, but something about bad programming practice? The text file actually originated from Excel, but I figured it would be easier to just use a text file. If it isn't, then that would be even better. If someone could help me out, thanks.
    Last edited by Wander; Dec 13th, 2004 at 09:14 PM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Reading lines from a text file

    to split the line, use arr=split(varTemp," ")
    the arr(0) will be the number.

    if you want an array for num, x, and y,

    Code:
    Dim arr()
    Dim n(5)
    Dim x(5)
    Dim y(5)
    varCount=0
    Do Until EOF(1)
      Line Input #1, varTemp
      arr=split(varTemp," ")
      arr(0) = n(varCount)
      arr(1) = x(varCount)
      arr(2) = y(varCount)
      varCount = varCount + 1
    Loop
    ...etc
    then, you can get the second item by using the value - 1, or x(1)
    you could make the arrays dynamic, by redim'ing them while you read them to be one bigger than the array is. to find out how big it is, use
    size=Ubound(x)
    it will give you the size or the array. then
    redim preserve n(size) + 1
    do it after you fill the current item.

    where do you want to save the selected information?
    Last edited by dglienna; Dec 13th, 2004 at 07:24 PM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    Hey, I'll just be displaying them on the form. I'll try playing with your code, thanks.

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Reading lines from a text file

    you may want to use a listbox.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    Would that be easier? I've never used them . There's actually more than 2 variables that I'm going to need to be displaying, more like 8, but I'm hoping if I can get 2 working, then it will be good. I'll look at this "listbox"

    Do you mean just use a listbox to store the data and call it from there?
    Last edited by Wander; Dec 13th, 2004 at 07:37 PM.

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Reading lines from a text file

    a listbox doesn't really have columns, per se. You would be better off using a flexgrid. this is like a spreadsheet, which can have labels.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    I think a listbox would work for my purposes until I can figure out the flexgrid. The only thing I need is to be able to call the data. Say I add an item on the list through .additem. How do I call that item back?

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Reading lines from a text file

    you have to loop through them one at a time. do you want to just find the item? there was some code that would do that for you automatically. search the forum for Sendmessage() by martinliss.

    here it is...

    http://www.vbforums.com/showthread.p...ht=sendmessage

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    Right, I got the looping part. I'm actually still going to use the text files since that's where all the data is. I'm just going to format the text files so that there's only one column. When the form loads, I have a loop that goes through the file and adds each line to the listbox (so the index goes in that order, which is fine). I want to be able to recall that information without the user having to click anything on the listbox (it's invisible). I'm just using it for a storage place. I thought it was List1.Item(Index) but that doesn't seem to be working.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Reading lines from a text file

    Code:
    list1.List(list1.ListIndex)
    will return the line

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    That works when the listbox is visible and a user clicks on a item on the listbox. I don't intend for it to be visible. Is there a way to simulate a click or just an internal command to access a certain item (index) on the listbox?

  12. #12
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Reading lines from a text file

    Here you have an example... It shows how to do it using a FlexGrid and a ListBox.

    I hope it helps you... I commented each line so you can understand what is being done.
    Attached Files Attached Files
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    Hey, thanks a lot Tec-Nico. This should come in handy. It will take me a while to look at everything. I think there's a problem though. I'm not sure if I'm able to use the FlexGrid. I get errors trying to load the form.

    From the error log

    Line 34: Class MSFlexGridLib.MSFlexGrid of control gridShow was not a loaded control class.
    Line 40: The property name _ExtentX in gridShow is invalid.
    Line 41: The property name _ExtentY in gridShow is invalid.
    Line 42: The property name _Version in gridShow is invalid.

  14. #14
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Reading lines from a text file

    David's code works even if the ListBox is not visible... You just have to change the "List1.ListIndex" to the number you would need.

    Example:
    Code:
     Dim strLineTwo As String
     
     strLineTwo = List1.List(1)
    In the example we are getting the second line of the ListBox. Why do we use 1 instead of 2? Well, because the ListIndex begins from 0... So 0 = 1, 1 = 2, and so on.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    Whoops, well that shows how experienced I am . I thought I figured it out too. What I just figured out before you said that was
    Code:
    List1.ListIndex = x
    list1.List(list1.ListIndex)
    Anyway, I hope I can get this to work with all the variables. Thank you to both of you and I'll be back, hopefully not asking for help.

  16. #16
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Reading lines from a text file

    That's strange... Please check this (Maybe you don't have the MSFlexGrid OCX):

    Open your Components... Menu
    (First Image)
    Attached Images Attached Images  
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Reading lines from a text file

    Code:
    List1.ListIndex = x
    list1.List(list1.ListIndex)
    is the same as

    Code:
    list1.List(x)
    ' If X+1 is the number that you want

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    I have a Microsoft FlexGrid Control 6.0 (MSFLXGRD.OCX) and a MSFlexGrid Wizard (FLEXWIZ.OCX)

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    Quote Originally Posted by dglienna
    Code:
    List1.ListIndex = x
    list1.List(list1.ListIndex)
    is the same as

    Code:
    list1.List(x)
    ' If X+1 is the number that you want
    Yeah, I didn't know how to use the other thing before

  20. #20
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Reading lines from a text file

    click on the project to open it, so that it gets the folder correct. i've had problems in the past. the project worked fine for me.

  21. #21
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Reading lines from a text file

    You say you have the MSFlexGrid? Then you shouldn't have any problem at all...

    Anyway... If it fails again then just remove the FlexGrid I named gridShow and add another FlexGrid with the name "gridShow" and that should do the trick.

    (Just remember you must have it checked so it is loaded in the ToolBox)
    Attached Images Attached Images  
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    18

    Re: Reading lines from a text file

    David's suggestion worked. I was just opening the form. Thanks again to both of you

  23. #23
    Banned
    Join Date
    Mar 2005
    Posts
    23

    Angry Re: Reading lines from a text file

    Hey, I'll just be displaying them on the form. I'll try playing with your code, thanks

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