Access VBA Counting Loops

I am having a bit of trouble figuring out how to count the number of loops that will occur in a bit of programing. There must be a short cut of some sort that you can use a calculator to determine the count. I can step through the code using the F8 key and watch the code count the loops but I have trouble determining the count prior to running the code itself.

Below are examples:

'Do ... Loop Until - evaluates at bottom - continues until the test is true

Private Sub cmdForNext_Click()
  'declare variables
  Dim intNumber As Integer, intCounter As Integer, strText As String
  'assing values
  intNumber = Val(Me.txtNumber)
  strText = Me.txtText
  'set up loop
  Dim intI As Integer
  For intI = intNumber To 10 Step 2
  'concatenate strText
  strText = strText & " * "
  'count loops
  intCounter = intCounter + 1
  'the next reserved word increments the control variable, in this case intI
  Next intI
   
    Me.lblResult.Caption = "The loop ran " & intCounter & " times." & vbCrLf & "The text concatnated to: " & strText & vbCrLf & "The value of intI is: " & intI
   
   

End Sub


Another example:


'evaluates at top

Private Sub cmdDoUntil_Click()
  'declare variables
  Dim sngNumber As Single, intCounter As Integer, strText As String
  'assing values
  sngNumber = Val(Me.txtNumber)
  strText = Me.txtText
  'set up loop
  Do Until sngNumber = 4
  'increment sngNumber
  sngNumber = sngNumber + 1
  'count loops
  intCounter = intCounter + 1
  'concatenate strText
  strText = strText & " * "
 
  Loop
    Me.lblResult.Caption = "The loop ran " & intCounter & " times." & vbCrLf & "The number changed to: " & sngNumber & vbCrLf & "The text concatneated to: " & strText
   
   

End Sub


Final example:


'evaluates at top

Private Sub cmdDoWhile_Click()
'declare variables
Dim sngNumber As Single, intCounter As Integer, strText As String
'assign values
sngNumber = Val(Me.txtNumber)
strText = Me.txtText
'set up loop
Do While sngNumber <= 5
  'increment sngNumber
  sngNumber = sngNumber + 1
  'count loops
  intCounter = intCounter + 1
  'concatenate strText
  strText = strText & " * "
  Loop
 
 
 
  Me.lblResult.Caption = "The loop ran " & intCounter & " times." & vbCrLf & "The number changed to: " & sngNumber & vbCrLf & "The text concatenated to: " & strText


End Sub

I have provided the file containing the above code.
240JRC-TutC2.accdb
James CoatsComputer Info. Sys. StudentAsked:
Who is Participating?
 
Rey Obrero (Capricorn1)Commented:
first, what items are you going to process inside your loop?
if you are going to process recordsets, you can do this

dim recCount as integer, j as integer
if not rs.eof then
rs.movelast
recCount=rs.recordcount
end if
rs.movefirst
for j=1 to reccount
'codes here
rs.movenext
next j
0
 
Gustav BrockCIOCommented:
What is your problem?

The loop count will vary - and will adopt - to the input parameter (txtNumber).

/gustav
0
 
James CoatsComputer Info. Sys. StudentAuthor Commented:
Thank you that answered my question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.