Results 1 to 14 of 14

Thread: Text with delimter, how do i do this? (RESOLVED)

  1. #1

    Thread Starter
    Lively Member Frank Mantooth's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    66

    Text with delimter, how do i do this? (RESOLVED)

    Hi, quite a newbee into VB and got stuck with this problem.

    Hope someone can help me with this.

    I have a text file with delimters which looks like this. This is two lines:

    Skybanner;DSK;;Aero Algarve;Portugal
    Aero Asia;RSO;E4;Aero Asia International (Private);Pakistan

    As you can see in the first line there is missing some data but is supposed to be ok.

    Here´s what i can´t figure out how to do:
    I want a combobox with the first names, in this case, Skybanner and Aero Asia and nothing else from these lines.

    The next 4 informations, i.e. from line 2, RSO;E4;Aero Asia International (Private);Pakistan i want to be able to display on 4 different labels.

    Also when i scroll the combobox the labels should update with whatever name i choose from the list i.e. the Skybanner name.

    Can someone give me an idea of how to do this?

    Kind regards

    Frank
    Last edited by Frank Mantooth; Sep 7th, 2004 at 03:05 AM.

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Here is part A. (Loading the ComboBox)
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim strArrBuff() As String
    5. Dim intIdx As Integer
    6.  
    7. On Error GoTo Err_Handler
    8.  
    9.     'Open the text file and slit up each lin into a string array
    10.     Open App.Path & "\myFile.txt" For Input As #1
    11.         strArrBuff = Split(Input(LOF(1), 1), vbCrLf)
    12.     Close #1
    13.  
    14.     'Iterate past each array element, and strip out the first part of the string
    15.     For intIdx = 0 To UBound(strArrBuff)
    16.         Combo1.AddItem Split(strArrBuff(intIdx), ";")(0)
    17.     Next
    18.  
    19.     Combo1.ListIndex = 0    'Show the fist entry
    20.  
    21. Exit Sub
    22.  
    23. Err_Handler:
    24.  
    25.     MsgBox "Description: " & Err.Description & vbCrLf & _
    26.         "Number: " & Err.Number, vbOKOnly + vbInformation, "Error"
    27. End Sub




    Bruce.

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    VB Code:
    1. Option Explicit
    2.  
    3. Dim AllLines() As String
    4.  
    5. Private Sub Form_Load()
    6.     Dim Buffer() As Byte, A As Long
    7.     'I like to open file in binary...
    8.     Open "c:\test.txt" For Binary Access Read As #1
    9.         'resize buffer to match file size
    10.         ReDim Preserve Buffer(LOF(1) - 1)
    11.         'read the buffer
    12.         Get #1, , Buffer
    13.     Close #1
    14.     'split all the lines
    15.     AllLines = Split(CStr(Buffer), vbCrLf)
    16.     'add all lines to combobox
    17.     For A = 0 To UBound(AllLines)
    18.         'note! if there are empty lines, this might give an error!
    19.         'no error checking!
    20.         Combo1.AddItem Split(AllLines(A), ";")(0)
    21.         'this stores the index number to the AllLines array
    22.         'makes it possible to have a sorted combobox!
    23.         Combo1.ItemData(Combo1.NewIndex) = A
    24.     Next A
    25.     'if there are items, activate the first one
    26.     If Combo1.ListCount > 0 Then Combo1.ListIndex = 0
    27. End Sub
    28.  
    29. Private Sub Combo1_Click()
    30.     Dim Temps() As String, A As Long
    31.     'get the index number refering the current item to the AllLines array
    32.     A = Combo1.ItemData(Combo1.ListIndex)
    33.     'split using ;    
    34.     Temps = Split(AllLines(A), ";")
    35.     'note! again no error checking here!
    36.     Label1.Caption = Temps(0)
    37.     Label2.Caption = Temps(1)
    38.     Label3.Caption = Temps(2)
    39.     Label4.Caption = Temps(3)
    40.     Label5.Caption = Temps(4)
    41. End Sub

    I felt too lazy to start writing out a proper description. So here you see code. Feel free to wonder how it works

  4. #4
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    I'm sure this isn't entirerly correct but there are bits of your question which aren't 100% clear.

    VB Code:
    1. Private i  As Integer
    2.  
    3.  
    4. Private Sub Combo1_Click()
    5.  
    6.     Label1.Caption = Combo1.Text
    7.     Label2.Caption = Combo1.Text
    8.     Label3.Caption = Combo1.Text
    9.     Label4.Caption = Combo1.Text
    10.  
    11. End Sub
    12.  
    13. Private Sub Command1_Click()
    14.  
    15. Dim strTxt As String
    16.  
    17.     Open "d:\file.txt" For Input As #1
    18.    
    19.     Do While Not EOF(1)
    20.         Line Input #1, strTxt
    21.         Combo1.AddItem Split(strTxt, ";")(0)
    22.         If i = 1 Then
    23.             Label1.Caption = Split(strTxt, ";")(1)
    24.             Label2.Caption = Split(strTxt, ";")(2)
    25.             Label3.Caption = Split(strTxt, ";")(3)
    26.             Label4.Caption = Split(strTxt, ";")(4)
    27.         End If
    28.         i = i + 1
    29.     Loop
    30.  
    31. End Sub
    32.  
    33. Private Sub Form_Load()
    34.  
    35.     i = 0
    36.  
    37. End Sub

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    That wouldn't work too well, all labels would show the same information

  6. #6

    Thread Starter
    Lively Member Frank Mantooth's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    66

    Well thanks

    I went for Bruces solution, worked really well, so Bruce......is there a part 2 coming ;-))

    Thanks in advance!

    Frank

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    It was, but Merri has the solution


    Impliment that, if you have any trouble let us know .





    Bruce.

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Try this (implimenting Merri's idea):
    VB Code:
    1. Option Explicit
    2.  
    3. Dim strArrBuff() As String
    4.  
    5. Private Sub Form_Load()
    6. Dim intIdx As Integer
    7.  
    8. On Error GoTo Err_Handler
    9.  
    10.     'Open the text file and slit up each lin into a string array
    11.     Open App.Path & "\myFile.txt" For Input As #1
    12.         strArrBuff = Split(Input(LOF(1), 1), vbCrLf)
    13.     Close #1
    14.  
    15.     'Iterate past each array element, and strip out the first part of the string
    16.     For intIdx = 0 To UBound(strArrBuff)
    17.         Combo1.AddItem Split(strArrBuff(intIdx), ";")(0)
    18.     Next
    19.  
    20.     Combo1.ListIndex = 0    'Show the fist entry
    21.  
    22. Exit Sub
    23.  
    24. Err_Handler:
    25.  
    26.     MsgBox "Description: " & Err.Description & vbCrLf & _
    27.         "Number: " & Err.Number, vbOKOnly + vbInformation, "Error"
    28. End Sub
    29.  
    30. Private Sub Combo1_Click()
    31. Dim strLineArr() As String
    32.  
    33.     strLineArr = Split(strArrBuff(Combo1.ListIndex), ";")
    34.  
    35.     Label1.Caption = strLineArr(1)
    36.     Label2.Caption = strLineArr(2)
    37.     Label3.Caption = strLineArr(3)
    38.     Label4.Caption = strLineArr(4)
    39. End Sub




    Bruce.

  9. #9

    Thread Starter
    Lively Member Frank Mantooth's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    66

    Run time error

    Getting this message when i run the app:

    Subscript out of range ------->

    strLineArr = Split(strArrBuff(Combo1.ListIndex), ";")

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Firstly Wecome to the Forum


    The code has been modified, try copying verbatim!
    (subtle changes were made - it tested OK)




    Bruce.
    Last edited by Bruce Fox; Sep 7th, 2004 at 02:48 AM.

  11. #11

    Thread Starter
    Lively Member Frank Mantooth's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    66

    Thank You!

    Works really nice, thanks all!

    Frank

  12. #12

    Thread Starter
    Lively Member Frank Mantooth's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    66

    Damn, too quick here

    Ah, sorry sorry.... i was too quick, anything is working just fine.
    But i needed the 4th information to be added to the combobox, and not the first.. i.e.


    Skybanner;DSK;;Aero Algarve;Portugal -----> Aero Algarve should be added to the Combobox and the other 4 to labels..

    How to change the code to accomplish this?

    Frank

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Edit (0) to (3) in the Combo1.AddItem line.

  14. #14

    Thread Starter
    Lively Member Frank Mantooth's Avatar
    Join Date
    Sep 2004
    Location
    Denmark
    Posts
    66

    Thx

    Got i working now, thx alot everyone!

    Frank

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