Executing query from VBA code (1 Viewer)

JPFred

Registered User
Joined
Oct 29, 2017
Messages
34
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
Joined
Sep 21, 2011
Messages
5,015
DoCmd.OpenQuery strName would be one way ? where strName is the name of the query?
 

JPFred

Registered User
Joined
Oct 29, 2017
Messages
34
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
Joined
Sep 21, 2011
Messages
5,015
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 (Users: 0, Guests: 1)

Top Bottom