Link to home
Start Free TrialLog in
Avatar of James Coats
James CoatsFlag for United States of America

asked on

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
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

What is your problem?

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

/gustav
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James Coats

ASKER

Thank you that answered my question.