Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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
 
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
 
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
 
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
 
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




Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a linked table in VBA Admiral_Hook Access 13 November 22nd, 2012 02: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 06:29 PM
Make Table query : table in Another Database marnik Access 1 March 19th, 2005 12:39 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.