Results 1 to 6 of 6
  1. #1
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501

    open query if result is not null

    When the user opens a form I would like to run a query. If that query returns any results I would like it to open and if it does not I dont want the window to open. I can open the query easy enough, i am just not sure how to get it to not run / open if the query doesn't return any records.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Users should not interact directly with tables and queries, only forms and reports. Use the query as source for a report.

    Review http://access.mvps.org/access/reports/rpt0006.htm
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    That works with reports and message boxes but i tried it with a form and it did not work. This is the code I have on the forms on open event
    Code:
      DoCmd.OpenForm "f_AlertResults", , , "RailcarID = " & Me.RailCarID
           If Err = 2501 Then Err.Clear

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You said you wanted to run a query and I assumed this to mean open a SELECT query, so I suggested a report bound to that query. What is this query you want to 'run'?

    Is user selecting this RailCarID in a combobox? Could limit the combobox list to only those RailCarID values that have related records.

    Otherwise, review http://allenbrowne.com/RecordCountError.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    In the form's open event:
    Code:
    If Dcount("*", "YourQuery") > 0 then
        docmd.openquery "YourQuery" 
    endif
    Is the query the recordsource for the form? In that case it makes no sense to me to open the query at all.
    If the query is the recordsource for the form, and your goal is to not open the form is the query is empty, you can check the dcount from whatever code opens the form.
    Last edited by davegri; 08-09-2017 at 10:47 PM. Reason: clarif

  6. #6
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    Thanks Devegri, that is working nicely for me

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

Similar Threads

  1. Open Query result want to hide
    By rmayur in forum Queries
    Replies: 12
    Last Post: 02-03-2015, 12:06 AM
  2. Replies: 1
    Last Post: 02-23-2012, 02:27 PM
  3. Open report from query result
    By shank in forum Programming
    Replies: 11
    Last Post: 10-04-2011, 12:59 PM
  4. Use query result to open form.
    By Playerpawn in forum Access
    Replies: 3
    Last Post: 05-12-2011, 11:18 PM
  5. If Query result Is Null...MsgBox..Okl
    By Bruce in forum Forms
    Replies: 28
    Last Post: 03-10-2010, 10:57 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