|
-
Apr 13th, 2002, 12:17 AM
#1
Thread Starter
Lively Member
counting textboxes in a control array
If I make a control array for the textbox that gets the value of how many items I want, how can I count the upperbound for that control array for textboxes...
for example in a combobox i used For i = 0 To cboItem.Count - 1
thanks
-
Apr 13th, 2002, 12:20 AM
#2
You can use .Count , or .UBound
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 13th, 2002, 12:22 AM
#3
Stuck in the 80s
Originally posted by crptcblade
You can use .Count , or .UBound
Just remember to do .Count - 1 if the index starts at 0.
-
Apr 13th, 2002, 12:30 AM
#4
Thread Starter
Lively Member
wow that was interesting.... i tried that the first time but it didnt work so i was confused, but it worked fine... thanks crptcblade
Ok well one more thing then, when i type in a qty first before i choose an item from the combobox it gives me a type mismatch error, but it only does this if i havent already selected an item and put a quantity in there.
thanks again
For i = 0 To txtQty.Count - 1
If IsNumeric(txtQty(i).Text) Then
lblTotal(i).Caption = FormatCurrency(txtQty(i).Text * lblPrice(i).Caption)
Else
lblTotal(i).Caption = " "
End If
Next i
-
Apr 13th, 2002, 01:13 PM
#5
Stuck in the 80s
Try this and see if it works:
VB Code:
For i = 0 To txtQty.Count - 1
If IsNumeric(txtQty(i).Text) Then
lblTotal(i).Caption = FormatCurrency(Val(txtQty(i).Text) * lblPrice(i).Caption)
Else
lblTotal(i).Caption = " "
End If
Next i
It shouldn't need it though
-
Apr 13th, 2002, 02:01 PM
#6
Thread Starter
Lively Member
hobo,
yeah it results in the same error with that line of code as well... When i hit debug after the error prompts up then it goes to the line where it reads:
lblTotal(i).Caption = FormatCurrency(Val(txtQty(i).Text) * lblPrice(i).Caption)
i tried a few different things as well but havent been successful.
I wouldnt think that it would make a differnece that i enter the qty before i choose an item from the combobox but i guess so
-
Apr 13th, 2002, 04:56 PM
#7
PowerPoster
Either LblTotal(i).caption is not an integer to begin with or you are trying to multiply a string with an integer. That is the only reason you would get that error.
Also the format function will raise that error if an invalid value is inserted. So step through you Variables and make sure they are what you think they are. And make sure you are only working with Integers not strings..
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 13th, 2002, 04:58 PM
#8
PowerPoster
Well
try
VB Code:
cdbl(lblPrice(i).Caption)
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Apr 14th, 2002, 12:07 PM
#9
Stuck in the 80s
Still having problems or is it fixed, ShoqG?
-
Apr 14th, 2002, 05:58 PM
#10
Thread Starter
Lively Member
Private Sub txtQty_Change(Index As Integer)
Dim i As Integer
For i = 0 To txtQty.Count - 1
If IsNumeric(txtQty(i).Text) Then
lblTotal(i).Caption = FormatCurrency(Val(txtQty(i).Text) * CDbl(lblPrice(i).Caption))
Else
lblTotal(i).Caption = " "
End If
Next i
End Sub
That line is still causing problems...
-
Apr 14th, 2002, 06:10 PM
#11
Stuck in the 80s
What are the lblPrice's captions?
-
Apr 14th, 2002, 06:12 PM
#12
Thread Starter
Lively Member
lblprice caption is empty
-
Apr 14th, 2002, 06:20 PM
#13
Originally posted by ShoqG
lblprice caption is empty
Shouldn't it be '0' (Zero), not " " for the calculation to work!
-
Apr 14th, 2002, 06:22 PM
#14
Stuck in the 80s
Originally posted by ShoqG
lblprice caption is empty
That's your problem right there...You can't multiply by nothing. That's worse than multiplying by 0.
-
Apr 14th, 2002, 06:25 PM
#15
Thread Starter
Lively Member
what am i supposed to put in there then? cuz when the form loads up the lblprice is empty... after the user selects the item from the combobox then the lblprice is set.. how can i handle that then
-
Apr 14th, 2002, 06:33 PM
#16
Maybe:
The user will have to choose the Price first, then select Qty.
(Hide the Qty until Price is selected)
Or,
Call a NEW sub called Calculate(Qty As Integer, Price As Integer, Index As Integer)
from either Price_Change OR txtQty_Change, with IF checks
to ensure BOTH Qty and Price have been entered.
-
Apr 14th, 2002, 07:15 PM
#17
All you have to do is run another Val on it.
Val(lblPrice(i).Caption)
-
Apr 14th, 2002, 07:22 PM
#18
Originally posted by Grimfort
All you have to do is run another Val on it.
Val(lblPrice(i).Caption)
With what he's explaned above I'm not sure that the code will
function as it should.
Cause, If the user selects the QTY first, (and a Calc is done by
multiplying by zero), the Total will = 0. His code dosn't seem to
Claculate if, and when Price is entered.
In any event, the 'default' price should be set to "0", that
would fix that problem, and prompt a user to enter one
Last edited by Bruce Fox; Apr 14th, 2002 at 07:26 PM.
-
Apr 14th, 2002, 10:00 PM
#19
Thread Starter
Lively Member
ok you guys are very helpful, getting a lot of good ideas.. What i thought about was having a msgbox pop up saying that you must select an item from the menu before selecting qty but now the problem is that when i enter a qty into the txtqty the msgbox shows up once and then twice. anyway i can just have it pop up once?
thanks guys
Private Sub txtQty_Change(Index As Integer)
Dim i As Integer
If cboItem(Index).Text = "" Then
MsgBox ("Please Select Item First")
cboItem(Index).SetFocus
txtQty(Index).Text = ""
Else
For i = 0 To txtQty.Count - 1
If IsNumeric(txtQty(Index).Text) Then
lblTotal(i).Caption = FormatCurrency(Val(txtQty(i).Text) * (lblPrice(i).Caption))
Else
lblTotal(i).Caption = " "
End If
Next i
End If
End Sub
-
Apr 14th, 2002, 10:05 PM
#20
You could use a 'Calculate' button, place the calc code there and
if a TextBox is empty, prompt for an input. That way ALL Qty and
Prices could be evaluated at the same time, if correct - display
all Totals.
-
Apr 14th, 2002, 10:06 PM
#21
-
Apr 14th, 2002, 10:07 PM
#22
Thread Starter
Lively Member
bruce,
this seems like it might actually work out fine but the only thing is that the msgbox comes up twice instead of once... do u know what the reason for that might be?
-
Apr 14th, 2002, 10:10 PM
#23
Frenzied Member
Originally posted by The Hobo
That's your problem right there...You can't multiply by nothing. That's worse than multiplying by 0.
Much worse. Multiplying by 0 isnt technically wrong, it just results in zero.
You just proved that sig advertisements work.
-
Apr 14th, 2002, 10:13 PM
#24
Thread Starter
Lively Member
yeah i'm trying to stay away from setting the price or any other value to 0 at design time... i think what i have now will actually work but the msgbox just shows up twice when you enter in a qty before selecting an item from the combobox...
anyhow while we are discussing other things... what is the difference between using index and declaring i or whatever as a counter?
-
Apr 15th, 2002, 03:31 AM
#25
Originally posted by ShoqG
anyhow while we are discussing other things... what is the difference between using index and declaring i or whatever as a counter?
There is no difference.
When the code is compiled and ran it would expand both variables out to real numbers before they are used.
The Index is just passed by the event to help to know which control was used, and your own counter is controlled by yourself.
-
Apr 15th, 2002, 03:33 AM
#26
Last edited by Grimfort; Apr 15th, 2002 at 03:36 AM.
-
Apr 15th, 2002, 03:36 AM
#27
Also, just looking back at the code above, if you are making a kind of purchase system, then you should make it that you always have at least 1 item in your QTY field.
Just do a simple check to see what the value is in the field, and if it is blank or 0 then default the value to 1.
i.e
if val(txtQty(i).text) = 0 then txtQty(i).text = 1
lblTotal(i).Caption = FormatCurrency(Val(txtQty(i).Text) * Val(lblPrice(i).Caption))
If they have not selected an item, then the calculation will work out right, at 0.00. As qty = 1 and price = 0.
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
|