Results 1 to 8 of 8
  1. #1
    stanley721 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    32

    Create Message Box based on query results

    Seems easy, but Im not quite gettng it. I have a form that has a button on it. I want anyone who clicks on the button to be able to enter a year and month that queries the total number of records in the table during that time frame. Then I would like a message box to popup and say "There were X amount of records entered this month." I do not want the resulting query table to be displayed, just a message box.
    My sql query is:


    SELECT Count(RecordList.TrackingNumber) AS CountOfTrackingNumber
    FROM RecordList
    GROUP BY Year([EntryDate]), Month([EntryDate])
    HAVING (((Year([EntryDate]))=[Enter Year]) AND ((Month([EntryDate]))=[Enter the Month #]))

    So now how can I run this query off of clicking a button and then display my count in a message box?

  2. #2
    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
    Why not use a DCount() instead? You don't need the GroupBy clause since the whole table will be processed anyway.

  3. #3
    stanley721 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    32
    So, use DCount instead of count in the sql query?
    Then how do I get that to the Message Box?

  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
    DCount() is a stand alone function and does not need to be in a query. It returns an answer all by itself. http://access.mvps.org/access/general/gen0018.htm

  5. #5
    stanley721 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    32
    OK, so how do I get that into my message box? I want the user to input which month they need a count of records. I guess I'm getting lost here.

  6. #6
    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
    You would construct the DCount() very similar to how you have your SQL but as a WhereCondition clause without the word WHERE. Something like:
    Code:
    SomeVariable = MsgBox("There are " & DCount("[TrackingNumber]","RecordList", & _
    "(Year([EntryDate])=[Enter Year]) AND (Month([EntryDate])=[Enter the Month #]))") & _
    " records that match that criteria."
    WARNING!! Air Code - Not tested!

  7. #7
    stanley721 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    32
    Got an error.
    Click image for larger version. 

Name:	Capture1.JPG 
Views:	7 
Size:	17.4 KB 
ID:	11181

    Private Sub Command78_Click()
    MsgBox("There are " & DCount("[TrackingNumber]","RecordList", & "(Year([EntryDate])=[Enter Year]) AND (Month([EntryDate])=[Enter the Month #]))") & " records that match that criteria."
    End Sub

  8. #8
    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
    That MsgBox returns a value (which you do not care about but need to allow for. Try:
    JunkVariable = Msgbox(...

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

Similar Threads

  1. Replies: 7
    Last Post: 09-21-2012, 03:30 PM
  2. Replies: 1
    Last Post: 04-30-2012, 05:10 PM
  3. Message Box based on Query Value
    By animaimmortale in forum Access
    Replies: 2
    Last Post: 12-07-2011, 04:15 PM
  4. Replies: 12
    Last Post: 12-17-2010, 05:35 PM
  5. Open form based on query results
    By RobbertH in forum Queries
    Replies: 1
    Last Post: 02-10-2010, 08:13 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