|
-
Sep 6th, 2004, 03:32 PM
#1
Thread Starter
Lively Member
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.
-
Sep 6th, 2004, 03:54 PM
#2
Here is part A. (Loading the ComboBox)
VB Code:
Option Explicit
Private Sub Form_Load()
Dim strArrBuff() As String
Dim intIdx As Integer
On Error GoTo Err_Handler
'Open the text file and slit up each lin into a string array
Open App.Path & "\myFile.txt" For Input As #1
strArrBuff = Split(Input(LOF(1), 1), vbCrLf)
Close #1
'Iterate past each array element, and strip out the first part of the string
For intIdx = 0 To UBound(strArrBuff)
Combo1.AddItem Split(strArrBuff(intIdx), ";")(0)
Next
Combo1.ListIndex = 0 'Show the fist entry
Exit Sub
Err_Handler:
MsgBox "Description: " & Err.Description & vbCrLf & _
"Number: " & Err.Number, vbOKOnly + vbInformation, "Error"
End Sub
Bruce.
-
Sep 6th, 2004, 03:57 PM
#3
VB Code:
Option Explicit
Dim AllLines() As String
Private Sub Form_Load()
Dim Buffer() As Byte, A As Long
'I like to open file in binary...
Open "c:\test.txt" For Binary Access Read As #1
'resize buffer to match file size
ReDim Preserve Buffer(LOF(1) - 1)
'read the buffer
Get #1, , Buffer
Close #1
'split all the lines
AllLines = Split(CStr(Buffer), vbCrLf)
'add all lines to combobox
For A = 0 To UBound(AllLines)
'note! if there are empty lines, this might give an error!
'no error checking!
Combo1.AddItem Split(AllLines(A), ";")(0)
'this stores the index number to the AllLines array
'makes it possible to have a sorted combobox!
Combo1.ItemData(Combo1.NewIndex) = A
Next A
'if there are items, activate the first one
If Combo1.ListCount > 0 Then Combo1.ListIndex = 0
End Sub
Private Sub Combo1_Click()
Dim Temps() As String, A As Long
'get the index number refering the current item to the AllLines array
A = Combo1.ItemData(Combo1.ListIndex)
'split using ;
Temps = Split(AllLines(A), ";")
'note! again no error checking here!
Label1.Caption = Temps(0)
Label2.Caption = Temps(1)
Label3.Caption = Temps(2)
Label4.Caption = Temps(3)
Label5.Caption = Temps(4)
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
-
Sep 6th, 2004, 04:00 PM
#4
I'm sure this isn't entirerly correct but there are bits of your question which aren't 100% clear.
VB Code:
Private i As Integer
Private Sub Combo1_Click()
Label1.Caption = Combo1.Text
Label2.Caption = Combo1.Text
Label3.Caption = Combo1.Text
Label4.Caption = Combo1.Text
End Sub
Private Sub Command1_Click()
Dim strTxt As String
Open "d:\file.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, strTxt
Combo1.AddItem Split(strTxt, ";")(0)
If i = 1 Then
Label1.Caption = Split(strTxt, ";")(1)
Label2.Caption = Split(strTxt, ";")(2)
Label3.Caption = Split(strTxt, ";")(3)
Label4.Caption = Split(strTxt, ";")(4)
End If
i = i + 1
Loop
End Sub
Private Sub Form_Load()
i = 0
End Sub
-
Sep 6th, 2004, 04:05 PM
#5
That wouldn't work too well, all labels would show the same information
-
Sep 6th, 2004, 04:34 PM
#6
Thread Starter
Lively Member
Well thanks
I went for Bruces solution, worked really well, so Bruce......is there a part 2 coming ;-))
Thanks in advance!
Frank
-
Sep 6th, 2004, 05:16 PM
#7
It was, but Merri has the solution 
Impliment that, if you have any trouble let us know .
Bruce.
-
Sep 6th, 2004, 05:24 PM
#8
Try this (implimenting Merri's idea):
VB Code:
Option Explicit
Dim strArrBuff() As String
Private Sub Form_Load()
Dim intIdx As Integer
On Error GoTo Err_Handler
'Open the text file and slit up each lin into a string array
Open App.Path & "\myFile.txt" For Input As #1
strArrBuff = Split(Input(LOF(1), 1), vbCrLf)
Close #1
'Iterate past each array element, and strip out the first part of the string
For intIdx = 0 To UBound(strArrBuff)
Combo1.AddItem Split(strArrBuff(intIdx), ";")(0)
Next
Combo1.ListIndex = 0 'Show the fist entry
Exit Sub
Err_Handler:
MsgBox "Description: " & Err.Description & vbCrLf & _
"Number: " & Err.Number, vbOKOnly + vbInformation, "Error"
End Sub
Private Sub Combo1_Click()
Dim strLineArr() As String
strLineArr = Split(strArrBuff(Combo1.ListIndex), ";")
Label1.Caption = strLineArr(1)
Label2.Caption = strLineArr(2)
Label3.Caption = strLineArr(3)
Label4.Caption = strLineArr(4)
End Sub
Bruce.
-
Sep 7th, 2004, 02:04 AM
#9
Thread Starter
Lively Member
Run time error
Getting this message when i run the app:
Subscript out of range ------->
strLineArr = Split(strArrBuff(Combo1.ListIndex), ";")
-
Sep 7th, 2004, 02:34 AM
#10
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.
-
Sep 7th, 2004, 03:04 AM
#11
Thread Starter
Lively Member
Thank You!
Works really nice, thanks all!
Frank
-
Sep 7th, 2004, 03:42 AM
#12
Thread Starter
Lively Member
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
-
Sep 7th, 2004, 08:00 AM
#13
Edit (0) to (3) in the Combo1.AddItem line.
-
Sep 7th, 2004, 04:39 PM
#14
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|