Executing query from VBA code (1 Viewer)

JPFred

Registered User.
Local time
Today, 18:54
Joined
Oct 29, 2017
Messages
47
I have written several queries using SQL to display a datasheet with selected data acquired for a search.

I must be dumb as a rock but I can't figure how to execute from VBA code. I'm sure it is something very simple but it eludes me.

Little help please:banghead:
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:54
Joined
Sep 21, 2011
Messages
14,048
DoCmd.OpenQuery strName would be one way ? where strName is the name of the query?
 

JPFred

Registered User.
Local time
Today, 18:54
Joined
Oct 29, 2017
Messages
47
DoCmd.OpenQuery strName would be one way ? where strName is the name of the query?
Can this be added to a buttons parameters and if so which one. Would it possibly be on the onclick parameter? I think I've tried it and received an error stating that docmd.openquery was not valid.:banghead:
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:54
Joined
Sep 21, 2011
Messages
14,048
Yes, it would normally be in a click event of a button.

Here is some code from one of my form buttons.
The form is able to open various types of object.

Code:
Private Sub cmdOpen_Click()
' Either open a basic report/query, or open form for same with parameters
Dim strName As String, strForm As String, strWhere As String
Dim iPreview As Integer
strName = Me.cboObject.Column(2)
strForm = Me.cboObject.Column(3)
strWhere = Nz(cboObject.Column(4), "")

If Me.chkPreview Then
     iPreview = 2 'acPreview
Else
    iPreview = 0 ' aacNormal
End If

If Nz(strForm, "") = "" Then
    Select Case Me.txtObjectType
        Case "Report"
            If strWhere = "" Then
                DoCmd.OpenReport strName, iPreview
            Else
                DoCmd.OpenReport strName, iPreview, , strWhere
            End If
        Case "Query"
            DoCmd.OpenQuery strName
        Case "Form"
            DoCmd.OpenForm strName
        Case Else
            MsgBox "Object Type not catered for"
    End Select
Else
    DoCmd.OpenForm strForm, , , , , , strName
End If
End Sub
 

Users who are viewing this thread

Top Bottom