|
-
Oct 22nd, 2002, 07:26 PM
#1
Thread Starter
Lively Member
How do I put a txt document in a listbox?
Does anyone know how I am supposed to put a txt document called payroll.txt in a listbox called lstemployees. Once I have the info put in a i have to get certain parts of it and have them put in seperate text boxes. Here is an example of a couple of lines of info to be put in the listbox:
Klinton Carpenter 18.00
*Cynthia Crenshaw 18.00
Richard Crouse 19.00
After that info is in the listbox. I need to put the FIRST NAME, LAST NAME, and NUMBER and INITIALS in seperate textboxes.
Can anyone help me with this? I've tried everything.
-
Oct 22nd, 2002, 07:44 PM
#2
PowerPoster
[Highlight=VB]
Dim strLine As String
Open "myfile_path" for Output As #1
Do
Line Input #1, strLine
List1.AddItem strLine
Loop Until EOF(1)
Close #1
-
Oct 22nd, 2002, 07:48 PM
#3
PowerPoster
Sorry, I hit Send too early, but anyway, to simplify processing your file layout must be consistent or you'll have a major headach otherwise.
-
Oct 22nd, 2002, 07:57 PM
#4
PowerPoster
It may look something like the following:
VB Code:
Dim strLine As String
Dim Pos%
Open "myfile_path" For Output As #1
Do
Line Input #1, strLine
'if layout is always as follows: "FirstName LastName 18.00" then
Pos = InStrRev(strLine, Space(1))
List1.AddItem Left(strLine, Pos - 1)
Pos = InStr(1, Left(strLine, Pos - 1), Space(1))
Text1.Text = UCase(Left(strLine, 1) & Mid(strLine, Pos + 1, 1))
Text2.Text = Mid(strLine, Pos + 1)
Loop Until EOF(1)
Close #1
-
Oct 22nd, 2002, 08:13 PM
#5
Thread Starter
Lively Member
-
Oct 22nd, 2002, 08:44 PM
#6
Thread Starter
Lively Member
Can anyone help me out with this? I still can't get this working...
-
Oct 22nd, 2002, 08:59 PM
#7
PowerPoster
Well
VB Code:
Open SFILE For Input As #1
Dim STRING1 As String, STRING2 As String
Do Until EOF(1) = True
Input #1, STRING1, STRING2
Msgbox String1 & " - " & String2
Loop
Close #1
Is this what you are after?
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Oct 22nd, 2002, 09:00 PM
#8
PowerPoster
Dim pos% is the same as
Dim pos As Integer
What isn't working for you? Whatever you've done - post it, so we have a chance to see what's wrong, otherwise is like pointing to the sky and tring to find your star.
-
Oct 22nd, 2002, 09:19 PM
#9
Thread Starter
Lively Member
Dim strLine As String
Dim intPos As Integer
Open "c:/student docs/payroll.txt" For Output As #1
Do
Line Input #1, strLine ****
intPos = InStrRev(strLine, Space(1))
lstEmployees.AddItem Left(strLine, intPos - 1)
intPos = InStr(1, Left(strLine, intPos - 1), Space(1))
txtFirstName.Text = UCase(Left(strLine, 1) & Mid(strLine, intPos + 1, 1))
txtLastName.Text = Mid(strLine, Pos + 1)
Loop Until EOF(1)
Close #1
****I get an error here "bad file mode"
-
Oct 22nd, 2002, 09:23 PM
#10
PowerPoster
For Output As #1
Change it to For Input
-
Oct 22nd, 2002, 09:25 PM
#11
PowerPoster
When you open file For Output - you are actually outputting from VB into a file
When file is open For Input - data will be Inputed from the file into a variable
-
Oct 22nd, 2002, 09:25 PM
#12
Thread Starter
Lively Member
Ok, I ran it after I changed it and it said... "input past end of file".... AHHHHH!
-
Oct 22nd, 2002, 09:27 PM
#13
PowerPoster
Of course - your previous attempt erased it (For Output clears file almost immediately). Restore file and try again.
-
Oct 22nd, 2002, 09:28 PM
#14
Thread Starter
Lively Member
How do I restore file? I'm so sorry for all the questions.
-
Oct 22nd, 2002, 10:33 PM
#15
Thread Starter
Lively Member
Hi, I got the names to get in the lstbox...but not the numbers by their names. This is what I have. Does anyone have any ideas as to why?
Open "c:/student docs/payroll.txt" For Input As #1
Do
Line Input #1, strLine
intPos = InStrRev(strLine, Space(1))
lstEmployees.AddItem Left(strLine, intPos - 1)
Loop Until EOF(1)
Close #1
This is what is supposed to be in the listbox
Klinton Carpenter 18.00
*Cynthia Crenshaw 18.00
Richard Crouse 19.00
-
Oct 22nd, 2002, 10:43 PM
#16
VB Code:
' The following line opens the text file for input into your VB program
Open "c:/student docs/payroll.txt" For Input As #1
' Start a loop
Do
' Get a line of text file the file
Line Input #1, strLine
' Find the last space in the line of text
intPos = InStrRev(strLine, Space(1))
' Add the name (without the amount) to the list box
lstEmployees.AddItem Left(strLine, intPos - 1)
' Add the name AND amount to the list box
' (un-comment this line and comment the previous AddItem line to see the difference.)
' lstEmployees.AddItem strLine
' Continue the loop until the end of file is reached
Loop Until EOF(1)
' Close the file
Close #1
-
Oct 22nd, 2002, 10:48 PM
#17
Thread Starter
Lively Member
Thanks!! It worked!! Now can you help me put the first name, last name, initials and amount in seperate textboxes. I got them all done except the amount. For some reason it comes out in the Last name text box. I also have to make all this info change when the user highlights a different name. Can you help me with this? This is what I have so far:
Open "c:/student docs/payroll.txt" For Input As #1
Do
Line Input #1, strLine
intPos = InStrRev(strLine, Space(3))
lstEmployees.AddItem strLine
intPos = InStr(1, Left(strLine, intPos - 1), Space(1))
txtFirstName.Text = Left(strLine, intPos + 0)
txtLastName.Text = Mid(strLine, intPos + 1)
txtInitials.Text = UCase(Left(strLine, 1) & Mid(strLine, intPos + 1, 1))
Loop Until EOF(1)
Close #1
-
Oct 23rd, 2002, 02:35 AM
#18
The following should get the correct data into your text boxes:
VB Code:
' The following line opens the text file for input into your VB program
Open "c:/student docs/payroll.txt" For Input As #1
' Start a loop
Do
' Get a line of text file the file
Line Input #1, strLine
' Find the last space in the line of text
intPos = InStrRev(strLine, Space(1))
' Add the name (without the amount) to the list box
' lstEmployees.AddItem Left(strLine, intPos - 1)
' Add the name AND amount to the list box
' (un-comment this line and comment the previous AddItem line to see the difference.)
' lstEmployees.AddItem strLine
intPos = InStrRev(strLine, Space(3))
lstEmployees.AddItem strLine
intPos2 = InStr(1, Left(strLine, intPos - 1), Space(1))
txtFirstName.Text = Left(strLine, intPos - 1)
intPos3 = InStr(intPos2, Left(strLine, intPos - 1), Space(1))
txtLastName.Text = Mid(strLine, intPos2 + 1, (intPos3 - 1) - intPos2)
' txtInitials.Text = UCase(Left(strLine, 1) & Mid(strLine, intPos + 1, 1))
txtInitials.Text = UCase(Left(txtFirstName.Text, 1) & UCase(Left(txtLastName.Text, 1))
txtAmount.Text = Trim(Right(strLine, Len(strLine) - intPos3))
' Continue the loop until the end of file is reached
Loop Until EOF(1)
' Close the file
Close #1
-
Oct 23rd, 2002, 02:45 AM
#19
To get the text box contents to change when a list box item is selected, you will need to move the code to put the data into them into the list box's 'click' event sub:
VB Code:
Private Sub lstEmployees_Click()
Dim strLineSelected As String
If lstEmployees.ListIndex < 0 Then Exit Sub
strLineSelected = lstEmployees.List(lstEmployees.ListIndex)
intPos = InStrRev(strLineSelected, Space(3))
intPos2 = InStr(1, Left(strLineSelected, intPos - 1), Space(1))
txtFirstName.Text = Left(strLineSelected, intPos - 1)
intPos3 = InStr(intPos2, Left(strLineSelected, intPos - 1), Space(1))
txtLastName.Text = Mid(strLineSelected, intPos2 + 1, (intPos3 - 1) - intPos2)
' txtInitials.Text = UCase(Left(strLineSelected, 1) & Mid(strLineSelected, intPos + 1, 1))
txtInitials.Text = UCase(Left(txtFirstName.Text, 1) & UCase(Left(txtLastName.Text, 1))
txtAmount.Text = Trim(Right(strLineSelected, Len(strLineSelected) - intPos3))
End Sub
-
Oct 23rd, 2002, 02:54 AM
#20
Thread Starter
Lively Member
Thank you so much for your help. It is very appreciated. I tried that. The hourly rate shows the Last Name and the hourly rate. The First name shows the First and Last name. The last name shows nothing. And the initials shows the first initial only.
-
Oct 23rd, 2002, 03:07 AM
#21
Fanatic Member
Try this:
Code:
Private Sub lstEmployees_Click()
Dim sinfo() As String
If lstEmployees.ListIndex < 0 Then Exit Sub
sInfo = Split(lstEmployees.List(lstEmployees.ListIndex)," ")
txtFirstName.Text = sInfo(0)
txtLastName.Text = sInfo(1)
txtInitials.Text = Left(sInfo(0),1) & " " & Left(sInfo(1),1)
txtAmount.Text = sinfo(2)
End Sub
Leather Face is comin...
MCSD
-
Oct 23rd, 2002, 03:14 AM
#22
Thread Starter
Lively Member
wow! Thanks!! that worked. except for the hourly rate. It doesnt show up at all.
-
Oct 23rd, 2002, 03:18 AM
#23
Fanatic Member
Hmm...
Try putting a break point on this line and however the mouse over sInfo(2).. What is in it?
txtAmount.Text = sinfo(2)
Leather Face is comin...
MCSD
-
Oct 23rd, 2002, 03:22 AM
#24
Thread Starter
Lively Member
whats a break point? sorry i'm a newbie
-
Oct 23rd, 2002, 03:45 AM
#25
Fanatic Member
Highlight the line in the code editor and press F9.
This means that the program will stop executing at this line...
Then run your program, click on the list box and it will stop... and you can find out what is actually going on.
Leather Face is comin...
MCSD
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
|