Link to home
Start Free TrialLog in
Avatar of ca1358
ca1358

asked on

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"
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

If you are in Access 2003 or prior, no.

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

mx
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.
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
you can follow the logic from the vba  code posted to write the Condition in the macro
Never mind, I thought you had an Action query,which is what I posted works for.

mx
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()
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ca1358
ca1358

ASKER

Thank you