Tuesday, January 29, 2008

Running Access Queries From Code

Over the years I have had the need to run queries in Access. Imagine that, huh! Well, I need to run queries without the annoying warning popups and I do it so often that I created a subroutine to handle most of the load.

Here is the code:


Sub RunAQuery(ByVal pQueryToRun As String)

'Written by Stuart Smith

' Seguin Software

On Error GoTo Err_RunAQuery

DoCmd.SetWarnings False

DoCmd.OpenQuery pQueryToRun, acNormal, acEdit

Exit_RunAQuery:

DoCmd.SetWarnings True

Exit Sub

Err_RunAQuery:

MsgBox Err.Description

Resume Exit_RunAQuery

End Sub


I turn off the warnings and when the querie is done, turn them back on. I put the SetWarning True in the subroutine exit area to make sure they get turned back on in case of an error.