Go Back   Access World Forums > Microsoft Access Discussion > Forms

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 10-29-2017, 05:11 AM   #1
JPFred
Newly Registered User
 
JPFred's Avatar
 
Join Date: Oct 2017
Location: Richmond, VA USA
Posts: 28
Thanks: 11
Thanked 1 Time in 1 Post
JPFred is on a distinguished road
Question 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

JPFred is offline   Reply With Quote
Old 10-29-2017, 05:20 AM   #2
Gasman
Enthusiastic Amateur
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 3,672
Thanks: 398
Thanked 637 Times in 618 Posts
Gasman has a spectacular aura about Gasman has a spectacular aura about Gasman has a spectacular aura about
Re: Executing query from VBA code

DoCmd.OpenQuery strName would be one way ? where strName is the name of the query?
__________________
Access novice. Sometimes trying to give something back.
Access 2007


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
Old 10-29-2017, 05:54 AM   #3
NauticalGent
Pristine Curmudgeon
Gold Supporter
 
NauticalGent's Avatar
 
Join Date: Apr 2015
Location: Naples, Italy (EXPAT from Virginia)
Posts: 1,780
Thanks: 386
Thanked 240 Times in 210 Posts
NauticalGent has a spectacular aura about NauticalGent has a spectacular aura about
Re: Executing query from VBA code

One of the best online sources (I think so anyway):

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

__________________
“I had the RIGHT to remain silent...but I didn’t have the ABILITY.” - Ron White
NauticalGent is offline   Reply With Quote
Old 10-30-2017, 08:52 AM   #4
JPFred
Newly Registered User
 
JPFred's Avatar
 
Join Date: Oct 2017
Location: Richmond, VA USA
Posts: 28
Thanks: 11
Thanked 1 Time in 1 Post
JPFred is on a distinguished road
Thumbs down Re: Executing query from VBA code

Quote:
Originally Posted by Gasman View Post
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.
JPFred is offline   Reply With Quote
Old 10-30-2017, 08:58 AM   #5
Gasman
Enthusiastic Amateur
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 3,672
Thanks: 398
Thanked 637 Times in 618 Posts
Gasman has a spectacular aura about Gasman has a spectacular aura about Gasman has a spectacular aura about
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

__________________
Access novice. Sometimes trying to give something back.
Access 2007


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing the EXE from code Drunkenneo Modules & VBA 8 09-03-2013 03:21 AM
Question Executing statement from VBA Code itsmedd General 23 11-03-2009 11:46 AM
code in a query disappear after executing some objects dai_lo Queries 1 10-19-2008 10:17 AM
Problems executing code Dachande11 Modules & VBA 8 06-12-2003 04:42 AM
code exists but not executing arage Modules & VBA 4 01-30-2001 07:18 AM




All times are GMT -8. The time now is 07:11 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


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