Dcount getting runtime error '2471'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vasago
    New Member
    • Mar 2014
    • 46

    #1

    Dcount getting runtime error '2471'

    Bookings ID is number
    ID is autonumber
    Error says:
    Run-Time error '2471':
    The expression you entered as a query parameter produced this error: '[Bookings ID]'

    Code:
    If DCount("[Click ID]", "Access Click log", "[Bookings ID] = " & Forms![Roll Out CRM]![Royal Rollout AM Routes]![ID]) = 0 Then
    Thank you for the help!
    Vasago
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If booking id is a string, you will need to surround the value you're looking for in quotes.

    Comment

    • Vasago
      New Member
      • Mar 2014
      • 46

      #3
      Booking ID's type is a number along with ID

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Then you will need to post the full exact error message.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          - The string theory:
          You've stumbled upon one of my pet peeves by building the criteria string within the command - and it's not your fault because that's how a majority of examples show how to use the command.
          Instead I suggest that you build the string first and then use the string in the command. Why you might ask, because you can then check how the string is actually resolving; thus, making troubleshooting the code so much easier as most of the time the issue is with something missing or not resolving properly/as expected within your string.
          So to use your code:

          Code:
          '>The following is air code; thus, not tested and may require more effort to operate properly
          '
              DIM strSQL as string
              Dim intCount As Integer
          '
          '
              strSQL = "[Bookings ID] = " & _
                  Forms![Roll Out CRM]![Royal Rollout AM Routes]![ID]
          '
          'now you can insert a debug print here for troubleshooting
          ' - press <ctrl><g> to open the immediate window
          ' - you can now cut and paste this information for review!
          '
          debug.print "Your criteria = " & strSQL
          '
          'now use the string in your code:
              intCount= DCount("[Click ID]", "Access Click log", strSQL)
          debug.print "Your Count= " & intCount
          '
          'Note: I didn't return the count within the IF...THEN...
          'once again because troubleshooting the code is not easily 
          'done when the function returns within another 
          'function/statement.
          '
          '
              if intCount = 0 then
                  'the remaining code
              end if
          '
          '
          If you will make these little changes and post back the resolved string we can help you tweak the code.

          When posting errors:
          Please post the EXACT title, number, and text of the error message. Please do not alter, omit, nor abreviate the information provided in the error message. ALSO indicate on which line of the code the error occurs and provide enough code so that we have the context of the commands used.
          Last edited by zmbd; Apr 18 '14, 10:29 PM.

          Comment

          • Vasago
            New Member
            • Mar 2014
            • 46

            #6
            ZMBD,

            I can't thank you enough for you time, education and knowledge. I have included the code below. My debug print for strSQL is Your criteria = Bookings ID = 2110. This is the correct Booking ID number.

            Error Code on line 9:
            Run-time error '2471':
            The expression you entered as a query parameter produced this error: '[Bookings ID]'

            Code:
            Private Sub Form_AfterInsert()
            Dim db As Recordset
            Dim strSQL As String
            Dim intCount As Integer
            
                strSQL = "[Bookings ID] = " & _
                    Forms![Roll Out CRM]![Royal Roll out Field Trips]![ID]
            Debug.Print "Your criteria = " & strSQL
                intCount = DCount("[Click ID]", "Access Click log", strSQL)
            Debug.Print "Your Count= " & intCount
            If intCount = 0 Then
            Set db = CurrentDb.OpenRecordset("SELECT * FROM [Access click Log]")
            db.AddNew
            db![Booking ID] = ID
            db.Update
            End If

            Comment

            • Vasago
              New Member
              • Mar 2014
              • 46

              #7
              Zmbd,

              I can't thank you enough for you time, lesson, and knowledge. My debug print for strSQL is Your criteria = [Bookings ID] = 2113. This is the correct Bookings ID.

              I am getting the error below on line 9.

              Error:
              Run-time error '2471':
              The expression you entered as a query parameter produced this error: '[Bookings ID]'

              Code:
              Private Sub Form_AfterInsert()
              Dim db As Recordset
              Dim strSQL As String
              Dim intCount As Integer
              
                  strSQL = "[Bookings ID] = " & _
                      Forms![Roll Out CRM]![Royal Roll out Field Trips]![ID]
              Debug.Print "Your criteria = " & strSQL
                  intCount = DCount("[Click ID]", "Access Click log", strSQL)
              Debug.Print "Your Count= " & intCount
              If intCount = 0 Then
              Set db = CurrentDb.OpenRecordset("SELECT * FROM [Access click Log]")
              db.AddNew
              db![Booking ID] = ID
              db.Update
              End If

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                Look at two things:

                -- Open the table "Access Click log" where[Bookings ID] is located.
                Check that the field name is spelled correctly between the code and the table.

                -- Check the type cast of the [Bookings ID] field, I suspect it is set to "text" not numeric as you are expecting.

                Comment

                • Vasago
                  New Member
                  • Mar 2014
                  • 46

                  #9
                  ZMBD,

                  Thank you again for your help. Problem is solved and I feel extremely embarrassed. [Bookings ID] is suppose to be [Booking ID].

                  Thank you,
                  Vasago

                  Comment

                  Working...