Access World Forums

Access World Forums (https://www.access-programmers.co.uk/forums/index.php)
-   Forms (https://www.access-programmers.co.uk/forums/forumdisplay.php?f=9)
-   -   [SOLVED] Executing query from VBA code (https://www.access-programmers.co.uk/forums/showthread.php?t=296640)

JPFred 10-29-2017 05:11 AM

Executing query from VBA code
 
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 10-29-2017 05:20 AM

Re: Executing query from VBA code
 
DoCmd.OpenQuery strName would be one way ? where strName is the name of the query?

NauticalGent 10-29-2017 05:54 AM

Re: Executing query from VBA code
 
One of the best online sources (I think so anyway):

http://fmsinc.com/MicrosoftAccess/qu...lp-center.html

JPFred 10-30-2017 08:52 AM

Re: Executing query from VBA code
 
Quote:

Originally Posted by Gasman (Post 1552111)
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 10-30-2017 08:58 AM

Re: Executing query from VBA code
 
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



All times are GMT -8. The time now is 09:05 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World