Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2015
    Posts
    10

    Trouble with DCount Using a Variable


    Hello all! I was hoping one of the experts here might be able to help me.

    I am trying to create a number generator that counts the number of entries in a table where the criteria in the form of entries in a textbox are met. This is the code that I have so far:

    Code:
    Dim Ent As StringDim Modl As String
    Dim Typ As String
    Dim Root As String
    Dim RICEWID As Integer
    
    
    'Check to see if there is any missing information
    If Not IsNull(Me![Entity]) And Not IsNull(Me![Module]) And Not IsNull(Me![Type]) Then
        
    'Stick field values into the variables
        Modl = Me![ModuleTB]
        Typ = Me![TypeTB]
        Ent = Me![EntityTB]
        Root = [Modl] & "-" & [Typ] & "-" & [Ent]
        
        RICEWID = DCount("[Entity]", "RICEW_ID_GEN", "[Entity]=" & Ent)
        'RICEWID = DCount("[Module]", "RICEW_ID_GEN", "[Entity] = 'HH'")
        
        MsgBox ([Root])
        MsgBox ([RICEWID])
        
    Else
    
    
    'Missing information escape message
        MsgBox ("You done screwed up.")
        
    End If
    End Sub
    On the line that the RICEWID variable is defined, the DCount function works fine as long as I pass it a string "[Entity] = 'HH'" but as soon as I remove the constant and insert the variable, I get Runtime Error 2471, despite the fact that the variable ='HH'. To me it looks like I'm passing the same info into the DCount criteria but clearly I'm missing something. Any help would be greatly appreciated.

    Justin

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    you need to include single quotes

    RICEWID = DCount("[Entity]", "RICEW_ID_GEN", "[Entity]='" & Ent & "'")

  3. #3
    Join Date
    Sep 2015
    Posts
    10
    That worked! Excellent, thank you. I understand the use of single and double quotes however I don't understand what it did in this instance. Would you mind giving me a brief explanation?

    Also, how would I set it up to check the number of records with a combination of Modl, Typ & Ent? I assumed it would be something like:

    Code:
    "[Entity]=' " & Ent & " ' " & "[Module]=' " & Modl & " ' " & "[Type]=' " & Typ & " '"
    ...but this doesn't work. Nor does it work if I replace the "&" with "+".

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    if you are comparing text values, you need to use quotes. [Entity] is a text field, Ent is a text value. you don't use anything for number values and you use # for dates
    with regards this

    "[Entity]=' " & Ent & " ' " & "[Module]=' " & Modl & " ' " & "[Type]=' " & Typ & " '"

    there are a number of issues

    if you enter a line of code

    debug.print "[Entity]=' " & Ent & " ' " & "[Module]=' " & Modl & " ' " & "[Type]=' " & Typ & " '"

    you will see in the immediate window how that is interpreted. Then compare that with what a sql criteria looks like and you should see what is missing. Assuming all your fields are text it should look like

    "[Entity]='" & Ent & "' AND [Module]='" & Modl & "' AND [Type]='" & Typ & "'"

    Also note that you are using spaces between the quotes so if Ent=abc then this

    "[Entity]=' " & Ent & " ' " &

    will be interpreted as

    [Entity]=' abc '

    which is probably not what you want.

    Finally Type is a reserved word - using it can cause unexpected errors as it can be misinterpreted to mean something else. Google 'access reserved words' to find out more

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I marked this thread as *Solved* for you using the *Thread Tools* at the top of the thread.
    https://www.accessforums.net/showthread.php?t=1828

  6. #6
    Join Date
    Sep 2015
    Posts
    10

    Thumbs up

    Thanks for all of the help and sorry for the delay getting back to this.

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

Similar Threads

  1. Dcount on variable Columns
    By Wagon in forum Forms
    Replies: 1
    Last Post: 04-26-2016, 05:48 AM
  2. Using DCount, how to use a variable as the criteria
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 02-17-2015, 05:18 PM
  3. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  4. Trouble with DCount syntax.
    By jessica.ann.meade in forum Database Design
    Replies: 9
    Last Post: 02-18-2011, 10:09 AM
  5. VBA DCount() with variable field name
    By drh in forum Programming
    Replies: 2
    Last Post: 07-13-2010, 12:36 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