Results 1 to 27 of 27

Thread: counting textboxes in a control array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116

    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

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by crptcblade
    You can use .Count , or .UBound

    Just remember to do .Count - 1 if the index starts at 0.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    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

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Try this and see if it works:

    VB Code:
    1. For i = 0 To txtQty.Count - 1
    2.   If IsNumeric(txtQty(i).Text) Then
    3.     lblTotal(i).Caption = FormatCurrency(Val(txtQty(i).Text) * lblPrice(i).Caption)
    4.   Else
    5.     lblTotal(i).Caption = " "
    6.   End If
    7. Next i

    It shouldn't need it though
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    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

  7. #7
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    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.


  8. #8
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    try

    VB Code:
    1. cdbl(lblPrice(i).Caption)
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Still having problems or is it fixed, ShoqG?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    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...

  11. #11
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    What are the lblPrice's captions?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    lblprice caption is empty

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by ShoqG
    lblprice caption is empty
    Shouldn't it be '0' (Zero), not " " for the calculation to work!

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    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

  16. #16
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    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.

  17. #17
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    All you have to do is run another Val on it.

    Val(lblPrice(i).Caption)

  18. #18
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    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.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    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

  20. #20
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    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.

  21. #21
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Just set the labels caption to 0 in the design inviroment or in the forms load event. problem solved.
    -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.


  22. #22

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    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?

  23. #23
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    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.

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116
    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?

  25. #25
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    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.

  26. #26
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    .
    Last edited by Grimfort; Apr 15th, 2002 at 03:36 AM.

  27. #27
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    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
  •  



Click Here to Expand Forum to Full Width