Using a Condition in Access Query

In macro
Action OpenQuery is Select Query
Is there a way in the condiction to say
 "If no records, do not open query"
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
If you are in Access 2003 or prior, no.

You need to do this in VBA code ... which is better anyway.

Rey Obrero (Capricorn1)Commented:
in vba, you can do this

if dcount("*","NameOfQuery")=0 then
  'do nothing
 docmd.openquery "NameOfQuery"
end if

sorry i don't have A2007 right now to write the macro.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Consider this approach instead:

Public Function mExecuteActionQuery() As Boolean

    On Error GoTo mExecuteActionQuery_Error
    Dim recsAffected As Long
    With CurrentDb
        .Execute "YourActionQueryName", dbFailOnError
        recsAffected = .RecordsAffected
    End With
    If recsAffected = 0 Then
        MsgBox "No Records were processed"
    End If
    Exit Function

    MsgBox "Unexcepted Error " & Err.Number & "  " & Err.Description
    GoTo mExecuteActionQuery_Exit

End Function

Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Rey Obrero (Capricorn1)Commented:
you can follow the logic from the vba  code posted to write the Condition in the macro
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Never mind, I thought you had an Action query,which is what I posted works for.

Rey Obrero (Capricorn1)Commented:
better is to omit the else

if dcount("*","NameOfQuery")> 0 then
  docmd.openquery "NameOfQuery"
end if

And if you still want to call that through a macro, you could put the code above in a procedure then create your macro with Action RunCode, and call the procedure:

In a module put:

Function OpenMyQuery()

   ... put capricorn1's or databasemx's code here

End Function

Then create your macro with Action RunCode with the function name OpenMyQuery()
Rey Obrero (Capricorn1)Commented:
in a macro editor

Condition                                       Action                Arguments
DCount("*","QueryName")>0        OpenQuery        QueryName,DataSheet,Edit

thats it

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ca1358Author Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.