VB Access Loop Problem: exits loop prematurely even if conditions are met

kapaw

Registered User.
Local time
Today, 05:38
Joined
Mar 30, 2011
Messages
30
I am filling up a listbox and I have this code which exits prematurely even if conditions are met. Starting value is 0.1

Code:
dblHighest = rs.Fields(0).Value 'dblHighest = 3.06
dblDCR = 0.1
    
    For j = 0.1 To dblHighest
        If dblDCR <= dblHighest Then
            lstDCR.AddItem (dblDCR)
            dblDCR = dblDCR + 0.1
        End If
    Next
    rs.Close
    Set rs = Nothing

Code stops at 0.3 so my listbox only have values from 0.1 to 0.3 instead of 0.1 to 3.0. Did I miss anything here?
 
Yeah. Completely wrong type of loop. Your loop runs 3 times because that is what the code says it should. Lookup

for next loop

in the help or google it for VBA and read.

And then use some other type of loop.
 
Thanks spikepl! What's weird is I'm using the same format for my other listboxes and they were all working fine.

Code:
    X = 500
    For i = 500 To intHighest
        If X <= intHighest Then
            lstIrated.AddItem (X)
            X = X + 250
        End If
    Next
 
Did you go to the docs and read about "for next "- and what is defaulted and how it works? If not then do that please and then it will not be "weird". It is a bit pointless for me to copy info from the help doc when you can read it yourself.
 
You can still use a For Loop, but structure it like this:

Code:
For j = 0.1 To dblHighest Step 0.1
        
Next
 
I doubt very much if a loop control using real numbers will work. Offhand, I would have thought the for ... next construct requires integer values.

But no - just checked, and you can use any datatype for the loop control.
 

Users who are viewing this thread

Back
Top Bottom