Results 1 to 9 of 9
  1. #1
    jhko is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Feb 2013
    Posts
    23

    Can't get DCount statement to work

    ORIGINAL POST:

    "Hi There:

    I'm trying to teach myself Access VBA and am a relative newbie.

    I'm trying to get a message box to pop up when a form loads. The code is supposed to look at all the calculated values in a specific query field, and pop up if it finds a value above a certain test value. The Query is called "Alkaline Cleaner Graph Query", the calculated field is "%BV". I keep getting a "Run time error 2465, can't find the field referred to in your expression". I must be doing something very basically wrong, but I can't figure it out. Any help would be greatly appreciated.


    Private Sub Form_Load()
    If (DCount("[%BV]", "[Alkaline Cleaner Graph Query]", [%BV] > 6)) > 0 Then
    MsgBox ("Got it!")
    End If


    End Sub"
    -----------------------
    UPDATE:

    First off, thanks VERY MUCH to everyone who responded to my post above, and helped me get it working.

    After getting it to work, I realized it is just what I asked for, but not what I want! Awk! I realized I can't search for any %BV field greater than 6, I have to look at only THE LAST ENTRY and see if it is greater than 6 (or whatever). And I need to do this at form load, and also just when the user has typed in a new value. The problem is that I have chemists doing titrations and entering the ml of titrant into a field on a form every few hours. From that entry, a %BV field is calculated from the ml of titrant field and displayed. If the value in the %BV field is too high, I need to give the chemist a message to add more chemistry to the tank. Ideally, the chemist would add more chemical right away and then do another titration. Hopefully the new titration would be within range and everything would be good. But sometimes the chemist will forget to add the chemical and needs to be reminded when they come back and reload the form later on. So my thought is, warn them both when they enter the most recent value, and also warn them at form load if the most recent record is above the limit.

    So anyway, following a suggestion on a MS web page, I created a totals query sorted in natural order and returning just the last record of the chemical analysis table. Then I attempted to use this query to pop up the message box, unfortunately without success.

    Private Sub Form_Load()


    If Queries.LastBVEntered.LastOfBV > 6 Then
    MsgBox ("Add more chemical!")
    End If

    I haven't attempted yet to pop up the message when the user has just typed in a new value.

    This query method seems awfully round-o-bout. Is there a function that just returns the value of a selected field in the last record of a table? That might make things much easier.

    Anyway, any more advice for this brain-drained newbie would be greatly appreciated.
    -------------
    UPDATE #2:

    Solved it myself! The DLast function seems to do what I want. I don't know why the MS Dev website says "You can use the DLast function to return a random record from a particular field in a table or query when you simply need any value from that field." That definition confused the heck out of me. Isn't it the *Last* record from a particular field?

    Anyway, thanks again
    Last edited by jhko; 02-10-2013 at 11:05 AM. Reason: Update to Just what I asked for and not what I want!

  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,641
    The criteria also needs to be I enclosed in quotes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jhko is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Feb 2013
    Posts
    23
    Thanks so much. Finally got it to work. After fixing the quotes, it still gave me one additional error that it couldn't find the query listed. I copied and pasted the Query name into the code, overwriting my previous hand typed query name, and that seemed to fix that error, although the names look the same to me (below vs what I originally posted).
    I'm curious why everything needs to be in quotes.

    Wow, this little piece of code took me 3 hours of work! Hopefully this gets faster with time ;-). Thanks again.

    Private Sub Form_Load()
    If (DCount("[%BV]", "[Akaline Cleaner Graph Query]", "[%BV]>5")) > 0 Then
    MsgBox ("Got it!")
    End If


    End Sub

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Here's a link you may want to review: http://support.microsoft.com/?id=826763

  5. #5
    jhko is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Feb 2013
    Posts
    23
    Quote Originally Posted by RuralGuy View Post
    Here's a link you may want to review: http://support.microsoft.com/?id=826763

    Thanks much. I think I"ll go back and change my object and field names to be safe.

  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,641
    Glad we could help, and welcome to the site!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by jhko View Post

    ...the names look the same to me (below vs what I originally posted)...
    "[Alkaline Cleaner Graph Query]"
    "[Akaline Cleaner Graph Query]"


    As my signature says: The Devil's in the Details!

    In 'WHERE' Clauses, the use of Single Quotes, Double Quotes and Octothorps (aka 'pound signs', 'hash marks') can be very confusing, and depends on the Datatype of the Field being used and whether you're using literal values or values held in a Control on a Form.

    Here's an excellent little primer on the subject:

    http://www.mvps.org/access/general/gen0018.htm

    Linq ;0)>

  8. #8
    jhko is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Feb 2013
    Posts
    23
    Wow, great catch! I looked at those two names for 10 minutes and didn't see the difference (awk!). Thanks very much.

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Are you ready to use the Thread Tools at the top of the thread and mark this thread as Solved?

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

Similar Threads

  1. Replies: 2
    Last Post: 02-22-2012, 07:14 AM
  2. DCount
    By nsteenhaut in forum Queries
    Replies: 2
    Last Post: 10-04-2011, 05:00 PM
  3. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  4. DCOUNT function with IN statement
    By dwilson in forum Access
    Replies: 2
    Last Post: 08-12-2011, 09:54 AM
  5. Replies: 2
    Last Post: 07-20-2011, 02:01 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