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"
Action OpenQuery is Select Query
Is there a way in the condiction to say
"If no records, do not open query"
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.
if dcount("*","NameOfQuery")=
'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
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
mx
better is to omit the else
if dcount("*","NameOfQuery")> 0 then
docmd.openquery "NameOfQuery"
end if
if dcount("*","NameOfQuery")>
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()
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
You need to do this in VBA code ... which is better anyway.
mx