Code to open a query

P

Pass-the-reality

The below code is tied to a command button. When I push the button, a new
form opens (frmMainForm) and sets to give me a blank record. I want to
expand this code to allow the frmMainForm to open, give me a blank record and
also open a query on top of that. The query is qrySANLookUp.


Private Sub Command41_Click()
On Error GoTo Err_Command41_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmMainForm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord , , acNewRec

Exit_Command41_Click:
Exit Sub

Err_Command41_Click:
MsgBox Err.Description
Resume Exit_Command41_Click

End Sub
 
D

Dirk Goldgar

Pass-the-reality said:
The below code is tied to a command button. When I push the button, a new
form opens (frmMainForm) and sets to give me a blank record. I want to
expand this code to allow the frmMainForm to open, give me a blank record
and
also open a query on top of that. The query is qrySANLookUp.


Private Sub Command41_Click()
On Error GoTo Err_Command41_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmMainForm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord , , acNewRec

Exit_Command41_Click:
Exit Sub

Err_Command41_Click:
MsgBox Err.Description
Resume Exit_Command41_Click

End Sub


Try this:

stDocName = "frmMainForm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord acDataForm, stDocName, acNewRec
DoCmd.OpenQuery "qrySANLookUp"

Is that what you wanted?

Note, by the way, that you could open the form directly to add a new record,
displaying no existing records, if you wanted to -- thereby avoiding the
need for the GoToRecord.
 
L

Larry Linson

When you say "open a query on top of that", do you want a separate query
just displayed over the form you opened; or do you mean you want the form
opened with the data from the query but positioned to a new record?

Larry Linson
Microsoft Office Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top