VB Access Loop Problem: exits loop prematurely even if conditions are met (1 Viewer)

kapaw

Registered User.
Local time
Today, 12:34
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?
 

spikepl

Eledittingent Beliped
Local time
Today, 21:34
Joined
Nov 3, 2010
Messages
6,142
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.
 

kapaw

Registered User.
Local time
Today, 12:34
Joined
Mar 30, 2011
Messages
30
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
 

spikepl

Eledittingent Beliped
Local time
Today, 21:34
Joined
Nov 3, 2010
Messages
6,142
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.
 

Bahamas

New member
Local time
Today, 15:34
Joined
Aug 18, 2015
Messages
8
You can still use a For Loop, but structure it like this:

Code:
For j = 0.1 To dblHighest Step 0.1
        
Next
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:34
Joined
Sep 12, 2006
Messages
15,657
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

Top Bottom