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"
ca1358Asked:
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.

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

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


sorry i don't have A2007 right now to write the macro.
0
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
   
mExecuteActionQuery_Exit:
    Err.Clear
    Exit Function

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

End Function


mx
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rey Obrero (Capricorn1)Commented:
you can follow the logic from the vba  code posted to write the Condition in the macro
0
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.

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


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

0
answer_dudeCommented:
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()
0
Rey Obrero (Capricorn1)Commented:
in a macro editor


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



thats it
0

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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.