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
Printable View
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
You can use .Count , or .UBound
:)
Just remember to do .Count - 1 if the index starts at 0.Quote:
Originally posted by crptcblade
You can use .Count , or .UBound
:)
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
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 :confused:
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
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..
try
VB Code:
cdbl(lblPrice(i).Caption)
Still having problems or is it fixed, ShoqG?
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...
What are the lblPrice's captions?
lblprice caption is empty
Shouldn't it be '0' (Zero), not " " for the calculation to work!Quote:
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.Quote:
Originally posted by ShoqG
lblprice caption is empty
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
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.
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 willQuote:
Originally posted by Grimfort
All you have to do is run another Val on it.
Val(lblPrice(i).Caption)
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 :)
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
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.
Just set the labels caption to 0 in the design inviroment or in the forms load event. problem solved.:D
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?
Much worse. Multiplying by 0 isnt technically wrong, it just results in zero.Quote:
Originally posted by The Hobo
That's your problem right there...You can't multiply by nothing. That's worse than multiplying by 0.
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?
There is no difference.Quote:
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?
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.
.
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.