Results 1 to 5 of 5
  1. #1
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169

    Dcount question

    Simple syntax question (for the expert that is). I have a form with a check box that makes a page on a tab appear if it is checked. The page contains a subform with records related to the record from the main form (related by QuestID). I am trying to add code to the after update event of the checkbox that will give a message that the checkbox cannot be unchecked if records exist on the subform. The query for the subform is qryParameters. I am trying to use the Dcount function to count the records in the subform but having some problems getting it to work correctly. My problem is with the criteria portion of the statement.

    Attached is the different codes I have tried:
    Code:
    Dim intHolder As Integer
    intHolder = DCount("*", "qryParameters", "QuestID = Me.QuestID") 'added
    If intHolder > 0 Then
    MsgBox "Parameters exist for this question. Please delete parameters first", vbInformation, "Parameters Exist"
    Getting a run-time error '2001' "You cancelled the previous operation"

    What am I doing wrong with the criteria? Thanks, Jim

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    there are two things. first, put a field in the first arg:
    Code:
    intHolder = DCount("FIELD NAME", "qryParameters", "QuestID = Me.QuestID")
    second, always concatenate out field names in criteria statements (they are treated just like vars):
    Code:
    intHolder = DCount("FIELD NAME", "qryParameters", "QuestID = " & Me.QuestID)
    and actually, people get this confused all the time. Remember, when you type in "me." in code, the intellisense should always drop down (unless it's shut off, which it never should be). If it doesn't, that's what tells you that the syntax is wrong.

    Is your example, you would probably not have gotten a dropdown if you were typing it inside of quotes.

    also remember, concat syntax is different for numbers/dates vs. strings. those are the two primary categories in Access.

  3. #3
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Thanks Adam...you are right, I did not get the autocomplete after typying Me. That is how I knew the problem existed there. I changed the syntax and that seems to be working now.

    As for the "*", I did have a field name in there but in looking at other posts, I saw where Pbaldy suggested that it was more efficient to use the "*" instead of a field name. See post: https://www.accessforums.net/access/...oad-10844.html post #8. I tried it and it worked so not sure what the best approach is on that.

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    it doesn't really matter, Jim. Unless you're doing sophisticated stuff that involve NULLS and other infrequencies and/or complex calculations, it's a matter of theory, which is what I'm sure Paul's post is mostly about.

    For your purposes, I would guess what you have is fine.

  5. #5
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Adam, thanks once again for your help!

    Just as a note of clarification, I changed the code to the Before Update event on this with additional code to undo the edit which is making this work much more like intended.
    Code:
    Dim intHolder As Integer
            intHolder = DCount("*", "qryParameters", "QuestID = " & Me.QuestID)
            If intHolder > 0 Then
            MsgBox "Parameters exist for this question. Please delete parameters first", vbInformation, "Parameters Exist"
                 Cancel = True
                 Me.ParameterBasedQuestion.Undo
                Else
           End If

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

Similar Threads

  1. DCount for Multiples
    By Huddle in forum Access
    Replies: 2
    Last Post: 12-21-2010, 08:57 AM
  2. DCount and SQL
    By DSTR3 in forum Queries
    Replies: 3
    Last Post: 12-06-2010, 03:07 PM
  3. Help with Dcount
    By tozey in forum Programming
    Replies: 1
    Last Post: 08-10-2010, 10:53 AM
  4. DCount realtime
    By Patience in forum Access
    Replies: 2
    Last Post: 06-24-2010, 06:20 AM
  5. Using count or Dcount
    By ddcook in forum Access
    Replies: 0
    Last Post: 07-22-2009, 03:35 PM

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