Results 1 to 7 of 7

Thread: For...Next Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    8

    Question

    Hmmmm.... This is a puzzle.

    I have five columns on my screen, as follows (sorry, it's wrapping here):

    storeID Units TotalStores AvgUnitsStore Retail ($000)
    --------------------------------------------------------------------------------------------------------------------------------
    txtSt(0) txtUnits(0) txtTotalStores(0) lblAvg(0) lblRetail(0)

    txtSt(1) txtUnits(1) txtTotalStores(1) lblAvg(1) lblRetail(1)

    txtSt(2) txtUnits(2) txtTotalStores(2) lblAvg(2) lblRetail(2)

    Each column continues through an index of 7...
    Note the indexes are set up the same for each element within the same storeID.

    When the user opens this screen, the TotalStores column will contain a default values which can be changed... no problem. The Units column, however, will be empty until the user enters a number, which is where I could run into problems.

    The AvgUnitsStore column fields lblAvg will be calculated like so:


    For i = 0 to 7
    lblAvg(i) =txtUnits(i) / txtTotalStores(i)
    Next

    When the calculation is performed, some of the txtUnits(i) fields will still be empty, which will cause an error unless I put additional instructions within the For...Next, telling it to skip the calculation for lblAvg(i) where txtUnits(i) equals null, and then to continue to the next (i).

    The lblRetail( ) field will be calculated similarly in the For...Next, using a different formula.

    Thanks for any and all suggestions in advance.


    Tracey

  2. #2
    Addicted Member dmr's Avatar
    Join Date
    Jul 2000
    Location
    West-Germany :) Timezone: GMT +1 [DST] North.......: 52° 16’ 09” East...: 10° 31’ 16”
    Posts
    255
    What exactly is your problem?
    Code that I author is neither elegant nor efficient. It is, however, functional. Once I get something that works, I'm generally satisfied with myself - I mean, if it works, that's good enough, right?

    Originally posted by aknisely
    Sorry, but I feel uncomfortable on CC with clothes on
    __________________
    The truth ... is out there!

  3. #3
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Hmm,

    To Quote:

    When the calculation is performed, some of the txtUnits(i) fields will still be empty, which will cause an error unless I put additional instructions within the For...Next, telling it to skip the calculation for lblAvg(i) where txtUnits(i) equals null, and then to continue to the next (i).

    :End Quote

    So, Sorry for being dense, It seems you know your problem and your solution. Can you elucidate?

    Seems to be a variant of the following

    Patient:
    "Doctor, it hurts when I do this"

    Doctor:
    "Then don't do that!"

  4. #4
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    If you use the following code, the val() ensures a value of 0 is used for the calculation instead of any null values.

    For i = 0 to 7
    lblAvg(i)=val(txtUnits(i) )/ val(txtTotalStores(i) )
    Next

    Chris

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    For i = 0 to 7 
       If Not IsNull(TxtUnits(i).text) then
           lblAvg(i) =txtUnits(i) / txtTotalStores(i)
       End If 
    Next
    This one will just skip that textbox if it's blank which I'm not too sure if you want or not.
    Code:
    For i = 0 to 7 
      lblAvg(i).caption =val(txtUnits(i).text)/ val(txtTotalStores(i).text) 
    Next
    Chris' example is much better, but I noticed that you're leaving vb to find the default property of the control. Instead of using Txtunits(i) try using Txtunits(i).text etc.
    Last edited by alex_read; Apr 9th, 2001 at 04:44 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    you need to make sure Val(txtTotalStores(i).Text) <> 0
    ie. empty or contains text.


    Code:
     
    
    For i = 0 To 7
      If Val(txtTotalStores(i).Text) <> 0 Then
        lblAvg(i).Caption = Val(txtUnits(i).Text) / Val(txtTotalStores(i).Text)
      End If
    Next
    Mark
    -------------------

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    8

    Smile

    Thanks for the responses. This is exactly what I need. I'll post another rsp. to let you know how it works out.



    Tracey

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