Results 1 to 13 of 13
  1. #1
    akrylik is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    47

    creating loop

    Im trying to create a loop that searches through a table and increments a variable every time it finds a record with a specific field set to a specific value. Below is the code but its not working. I then use the variable for another field. can you see anything wrong? This is used in a form btw.

    Dim counter As Integer
    counter = 0
    Set rst = CurrentDb.OpenRecordset("TableName")
    Do Until rst.EOF
    If rst(10) = "True" Then
    counter = counter + 1
    End If

    rst.MoveNext
    Loop


    Me.Text24 = counter

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    It's not how I old do it, but it should work. If that's a yes/no field you wouldn't want unites around True.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    akrylik is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    47
    the field is a combo box w/ a value list with values "True","False","Undefined". How would you have done it?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    What does "not working" mean then?

    It is very inefficient to loop through an entire table to get a count. One option would be a DCount with the appropriate criteria:

    DLookup Usage Samples

    To use a recordset, I'd use an SQL statement that did the count:

    Set rst = CurrentDb.OpenRecordset("SELECT Count(*) As HowMany FROM TableName WHERE FieldName = 'True'")

    Me.Text24 = rst!HowMany
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    akrylik is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    47
    that sql statement worked perfectly, but when i tried this:
    Set rst = CurrentDb.OpenRecordset("SELECT Count(*) As HowMany FROM TableName WHERE Last_Name = me.txtLastName and Headcount = 'True'")

    it gave me this error : run-time error '3061' : Too few parameters. expected 2.

    me.txtLastName is a combo box in a form. i am trying to count how many records in the table that has the last name that is in the form. did i enter the fields wrong?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    You have to concatenate the last name:

    Set rst = CurrentDb.OpenRecordset("SELECT Count(*) As HowMany FROM TableName WHERE Last_Name = '" & me.txtLastName & "' and Headcount = 'True'")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    akrylik is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    47
    i tried doing it but i had it coded wrong, forgot the " " . Ill give that a try and let you know.

  8. #8
    akrylik is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    47
    its still throwing me an error. but now its saying expected 1 instead of 2. i even tried deleting what i had and copied yours. Should i try me.txtLastName.Column(1) since it is a combo box?

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    You need to use the column that contains the data for the field you specified. If you're still getting that error, double check the spelling of the table and field names.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    akrylik is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    47
    Got it to work. This is just stage 1 of this functionality so ill probably be posting on this thread again once it gets more advanced. Thanks for the help.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    akrylik is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    47
    i cant find the error in the code. says theres an else without an if.

    If Me.Text24 = 1 Then Headcount = "True"
    ElseIf Me.Text24 > 1 Then
    If Me.Percent_Utilization = 1 Then Headcount = "True"
    ElseIf Me.Percent_Utilization < 1 Then Headcount = "False"
    End If
    End If

    you see it?

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    You're mixing the one-line and block formats of If/Then. Try

    Code:
      If Me.Text24 = 1 Then
        Headcount = "True"
      ElseIf Me.Text24 > 1 Then
        If Me.Percent_Utilization = 1 Then
          Headcount = "True"
        ElseIf Me.Percent_Utilization < 1 Then
          Headcount = "False"
        End If
      End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. To Loop, To Query or something else
    By Perdo123 in forum Access
    Replies: 12
    Last Post: 03-02-2012, 02:51 AM
  2. Loop through recordset
    By eliedebs in forum Programming
    Replies: 1
    Last Post: 01-11-2012, 12:26 AM
  3. Help with a For Loop in VB
    By JFo in forum Programming
    Replies: 5
    Last Post: 09-29-2011, 02:45 AM
  4. Do While loop
    By jgelpi16 in forum Programming
    Replies: 7
    Last Post: 07-23-2010, 08:21 AM
  5. VBA Loop
    By DreamOn in forum Programming
    Replies: 4
    Last Post: 06-25-2010, 03:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums