Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Access 2007 VBA ISBN: 978-0-470-04684-5
This is the forum to discuss the Wrox book Beginning Access 2007 VBA by Denise M. Gosnell; ISBN: 9780470046845
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Access 2007 VBA ISBN: 978-0-470-04684-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 26th, 2009, 05:08 PM
Registered User
 
Join Date: Jun 2008
Location: Evanston, IL, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Use VBA to run a make table query

I have a make table query that I would like to run from VBA. I want to be able to add or change the table name. What would be the sytax to do this via VBA?

Thanks,

Trebormac
Reply With Quote
  #2 (permalink)  
Old April 22nd, 2009, 12:36 PM
Authorized User
 
Join Date: Jul 2008
Location: Portland, OR, USA.
Posts: 38
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Trebormac View Post
I have a make table query that I would like to run from VBA. I want to be able to add or change the table name. What would be the sytax to do this via VBA?

Thanks,

Trebormac
This is almost a month old, but here goes:

1. Create your make table query in the QBE grid and then

2. take the SQL of that to the VBA Window

3. You can then modify the SQL code

for example if you have a query like this:
SELECT sbqryAdmitDeals.lt_name, sbqryAdmitDeals.PortfolioName, sbqryAdmitDeals.FullName, sbqryAdmitDeals.RoleLeadFK, sbqryTRFS.RepTypeName, sbqryTRFS.RepRecd, sbqryTRFS.Status, CDate(Format(Now(),"Short Date"))-[RepRecd] AS Days, 2 AS OrderID INTO mtblFSRcvd
FROM sbqryAdmitDeals INNER JOIN sbqryTRFS ON sbqryAdmitDeals.PropertyFK = sbqryTRFS.PropertyFK
WHERE (((sbqryAdmitDeals.RoleLeadFK)=[Forms]![frmReports]![cboPrimaryContact]) AND ((sbqryTRFS.RepTypeFK)=6));

You can use this SQL in code

Code:
Dim strSQL As String
 
strSQL = "SELECT sbqryAdmitDeals.lt_name, sbqryAdmitDeals.PortfolioName, " & _
"sbqryAdmitDeals.FullName, sbqryAdmitDeals.RoleLeadFK, sbqryTRFS.RepTypeName, " & _
"sbqryTRFS.RepRecd, sbqryTRFS.Status, CDate(Format(Now(),"Short Date"))-[RepRecd] AS Days, 2 AS OrderID " & _ 
"INTO " & YourVariableNameHere & _
"FROM sbqryAdmitDeals " & _
"INNER JOIN sbqryTRFS ON sbqryAdmitDeals.PropertyFK = sbqryTRFS.PropertyFK " & _
 
"WHERE (((sbqryAdmitDeals.RoleLeadFK)= " & [Forms]![frmReports]![cboPrimaryContact] & _
 ") AND ((sbqryTRFS.RepTypeFK)=6));"
__________________
Thanks,

Bob Larson
Access MVP (2008-2010, 2011)
Free Access tutorials, samples, tools
Reply With Quote
  #3 (permalink)  
Old April 22nd, 2009, 12:41 PM
Authorized User
 
Join Date: Jul 2008
Location: Portland, OR, USA.
Posts: 38
Thanks: 1
Thanked 2 Times in 2 Posts
Default

And I forgot to say how you run it...
Either like:
Code:
CurrentDb.Execute strSQL, dbFailOnError
or
Code:
On Error GoTo err_handler
 
... the strSQL part...
 
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
 
ExitMySubName:
   Exit Sub
 
err_handler:
    DoCmd.SetWarnings True
    MsgBox Err.Description, vbExclamation, "Error Number: " & Err.Number
    Resume ExitMySubName
 
End Sub
You always include an error handler with DoCmd.SetWarnings True as the first line if you turn them off. That way if something fails before it can reset then it will always get reset and you won't find yourself without any warnings at all (including save warnings).
__________________
Thanks,

Bob Larson
Access MVP (2008-2010, 2011)
Free Access tutorials, samples, tools
Reply With Quote
  #4 (permalink)  
Old April 23rd, 2009, 12:24 PM
Registered User
 
Join Date: Jun 2008
Location: Evanston, IL, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. This is helpful for future use. As it turns out I was using the make table query so I could use the TransferSpreadsheet method of the DoCmd. After doing a little home work on MSDN I came across the Output To method. This worked well since my end game was to export the results of a query to Excel.

Thanks again and best,

Trebormac
Reply With Quote
  #5 (permalink)  
Old April 23rd, 2009, 01:40 PM
Authorized User
 
Join Date: Jul 2008
Location: Portland, OR, USA.
Posts: 38
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Glad you got things to work for you. :) Thanks for letting me know that you had gotten it taken care of.
__________________
Thanks,

Bob Larson
Access MVP (2008-2010, 2011)
Free Access tutorials, samples, tools
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a linked table in VBA Admiral_Hook Access 13 November 22nd, 2012 01:15 AM
Can a Make Table Query produce a Linked table? kronik Access 5 May 16th, 2006 06:17 AM
Calling a stored make table query from VBA jas644 Access VBA 0 March 30th, 2005 05:29 PM
Make Table query : table in Another Database marnik Access 1 March 19th, 2005 11:39 AM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM



All times are GMT -4. The time now is 03:21 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.