douglas 08-26-2000, 11:32 AM I need to write and read the contents of a 3 column listbox.
f = FreeFile
Open myfile For Append As #f
For i = 0 To List1.ListCount - 1
Print #f, List1.List(i, 0) & vbTab & List1.List(i, 1) & vbTab & List1.List(i, 2)
Next
Close #f
....will write, however when I read read it back into a listbox, it seems to brings the row into
the first column instead of 3 separate columns. I think I need to treat rows as an array.(?)
After hours of trying sort it out in my thick brain, I thought I would write this post.
Any assistance is appreciated.
Thanks,
Doug Anderson
Sparkey 08-26-2000, 07:42 PM Don't know how you're getting this to write. I tried it and got a "list1.list parameters wrong". I did get it to work with only a few modifications (had to remove the i parameter), but it would help us if we could see the full code, especially how you are reading it in.
karimahta 08-26-2000, 11:36 PM What sort of list box are you using? The standard list box (in VB6) doesn't have this syntax!
How are you getting this to write at all?
douglas 08-27-2000, 09:38 AM Thanks for responding to my post.
I am using VBA 6, which I am discovering does not share all methods of vb 6.
Code I'm using to add 3 columns of data to listbox:
item = List1.ListCount
List1.ListIndex = List1.ListCount - 1
List1.AddItem "Bar Drink ", (item)
List1.List(item, 1) = "2.75"
List1.List(item, 2) = "0"
List1.ListIndex = List1.ListCount - 1
(when I run this code in vb, I also receive error, however in vba it works fine.)
To Clarify my objectives:
1) add 3 columns of data to a listbox (Item, price, extension) using a separate command button for each item added.
2) write listbox to file, read file into a listbox.
Code I'm using to write which seems to work in vba:
Dim xWaitress, yCheck, waitfile, i
xWaitress = lblWaitress.Caption
yCheck = lblCheck.Caption
waitfile = "d:servers"" & xWaitress & """ & yCheck
lstCheck.BoundColumn = 0
f = FreeFile
Open waitfile For Output As #f
For i = 0 To lstCheck.ListCount - 1
Print #1, lstCheck.List(i, 0)
Print #1, lstCheck.List(i, 1)
Print #1, lstCheck.List(i, 2)
Next
Close #f
Code I"m using to read:
Dim w, n, k, lne$
w = Label1.Caption ' subdirectory
n = Label2.Caption ' file
Filenam$ = "d:servers"" & w & """ & n
f = FreeFile
Dim InputData, x, y
Open Filenam$ For Input As #f
Do While Not EOF(1)
Input #f, lne$, x, y
z = Register.lstCheck.ListCount
Register.lstCheck.AddItem lne$
Register.lstCheck.List(z, 1) = x
Register.lstCheck.List(z, 2) = y
Loop
Close #f
What I'm trying to learn:
How to add 3 colums of data to a listbox in vb6, consistent with my objectives stated above. Write 3 columms, read 3 columns. (perhaps I'm using the wrong control)
Thanks again for your time.
Sparkey 08-27-2000, 11:48 AM You have been using VBA but you want this code to run in VB 6. Is this correct? We'll have a go at it and get back to you.
Let us know if this is incorrect.
douglas 08-27-2000, 01:02 PM That is correct, I would like to run in vb. Moreover, since vba help is scarce, I am curious as to what are the differences with regard to this list i/o method.
Thanks again for the help
Sparkey 08-27-2000, 01:54 PM
douglas 08-27-2000, 05:20 PM This project is for a bar-restaurant
1. Buttons for Item, Price, Extension. What is extension?
The click event of a command button supplies column 1 with an item, column 2 with non-taxable price, column 3 with taxable price. (the word extension was used only for example, prices are numerals.
2. Register???
Register is the name of a main form
3. What is lstCheck
lstCheck is a listbox of a listbox which is populated from two other listboxes, one for food(taxable), one for liquor(non-taxable)
and why are you using the BoundColumn property?
As I have code in VBA now, (and it is workable) the boundColumn property is used to sum the total of column 2- nontaxable sub total, and column 3 - taxable sub total. If a waitress orders a Beer, column 3 will contain a zero, since Beer is nontaxable. I did not bind to a data source, although I thought about it, and am considering it now.
This property suggests that you are using a data control and a recordset since (in VB 6) the BoundColumn property returns or sets the name of the source field in a Recordset object that is used to supply a data value to another Recordset.
First Code supplied was one of two pieces of code I was trying to make work. Yesterday, both pieces of code seemed not to work, albeit for different reasons.
Sample Code to Add to List1, which is non-taxable stuff:
If Label40.Caption = "Regular Prices" Then
item = List1.ListCount
List1.ListIndex = List1.ListCount - 1
List1.AddItem "Premimum Can Beer", (item)
List1.List(item, 1) = "2.25"
List1.List(item, 2) = "0"
List1.ListIndex = List1.ListCount - 1
CommandButton24.SetFocus
ElseIf Label40.Caption = "Happy Hour Prices" Then etc..........
Code to sum food & drink lists:
Dim sngSum As Currency
Dim sngItem As Currency
Dim sngDrinks As Currency
Dim sngDrinksSum As Currency
Dim sngFood As Currency
Dim sngFoodSum As Currency
Dim w, x, y, z, q, tz
Dim tq
tq = lstCheck.ListCount
For q = 0 To ListBox1.ListCount - 1
lstCheck.AddItem ListBox1.List(q)
lstCheck.Column(1, tq) = ListBox1.Column(1, q)
lstCheck.Column(2, tq) = ListBox1.Column(2, q)
tq = tq + 1
Next
tz = lstCheck.ListCount
For z = 0 To List1.ListCount - 1
lstCheck.AddItem List1.List(z)
lstCheck.Column(1, tz) = List1.Column(1, z)
lstCheck.Column(2, tz) = List1.Column(2, z)
tz = tz + 1
Next
Code to Save:
f = FreeFile
Open waitfile For Output As #f
For i = 0 To lstCheck.ListCount - 1
Print #1, lstCheck.List(i, 0)
Print #1, lstCheck.List(i, 1)
Print #1, lstCheck.List(i, 2)
Next
Close #f
Sample Code to Read:
f = FreeFile
Dim InputData, x, y
Open Filenam$ For Input As #f
Do While Not EOF(1)
Input #f, lne$, x, y
z = Register.lstCheck.ListCount
Register.lstCheck.AddItem lne$
Register.lstCheck.List(z, 1) = x
Register.lstCheck.List(z, 2) = y
Loop
Close #f
Again, thanks for the help.
Sparkey 08-27-2000, 05:39 PM
douglas 08-27-2000, 06:38 PM Data is added simply. The user sees a variety of buttons on the form. If she wants "Regular Beer" she presses it and it is added to a drinks listbox, if she wants "Cake" she goes to the food form and presses it . . piece of cake. (this time it is added to a food listbox... necessary to keep the two list separate because food is taxable, beer is not. (and other reasons - food needs to be sent to a kitchen printer, etc. The reason for the three columns is that a check needs to be opened and closed several times before a customer actually pays his fare. At this time both lists are combined and the check is closed for good. Combo box, no. Data is being entered it the most waitress-friendly maner I can possibly think of.
However with regard to binding data source to a database, I'm interested into looking into it, although data entry is covered well, and I'm not sure exactly how to bind to Access or another db.
Sparkey 08-30-2000, 10:37 AM You can't add items selectively to a standard multicolumn list box, the items are 'wrapped' to each column. However, I found a list box in VB6 (there are a few different ones)that is ideal for your purpose, and works with your existing code. I'm not familiar with VBA so check and see if it exists.
In VB6 got to the Project dop-down menu and select 'Components'. Tick the 'Microsoft Forms 2.0 Object Library' and apply the changes. You now have extra components so select the new listbox component. Set the properties to suit, but set columncount to 3 and the columnheads to true etc. Can use databound with it, and can be used with database. See if VBA has the same thing. Do you want this to write to file in VB6 or VBA?
|