Hi jpgalasso,
The thing is you cannot use runsql to execute select queries, only update/append/insert/alter
Kelly
Main Topics
Browse All TopicsI need to use VBA to create a one to one Query from "Table1" and "Table2". While the two tables do not have exactly the same Fields, they each share three Fields named "Field1", "Field2" and "Field3". Field1 is a "primary key". I need to capture in a Query all of the common records for "Field1" (i.e. those records for which Table1's Field1 equal Table2's Field1). For these common records, the Query needs to contain the corresponding values for four other columns (i.e. Table1:Field2, Table2:Field2, Table1:Field3, and Table2:Field3).
I have tried the following:
Dim SQL As String
SQL = "SELECT Table2.Field2, Table1. Field2, Table1.Field3, Table2.Field3 "& _
"FROM Table1 INNER JOIN Table2 ON Table1.Field1 = Table2.Field1"
DoCmd.RunSQL SQL
But I get the following error message: "A Run SQL action requires an argument of an SQL statement".
Actually even if the above worked, I am not sure what I would have. Don't I need to somehow name the Query so I can access the results?
Thanks much for any help.
Jim
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
RunSQL is only used for INSERT, DELETE and UPDATE query types, not for SELECT.
Depending on what you are doing with the results you could create a recordset and bind a form to the recordset so users could view the data on a form. If you want to view the results ina report you will need to create a querydef which is what you would need to do if you want to export (easiest way to export) the data also.
for a form ...
in declaration section of form's module
Private mrstForm As DAO.Recordset
in whatever event causes you to build the recordset ... perhaps Open
Private Sub Form_Open()
Set rst = CurrentDB.OpenRecordset(" your SQL goes here ")
Set Me.Recordset = mrstForm
End Sub
'don't forgetr to cleanup when the form closes
Private Sub Form_Close()
mrstForm.Close
Set mrstForm = Nothing
End Sub
if you want to open a report of the SQL you can save a query def
I would have a saved query that could be modified and your report uses the saved query as it's record source
CurrentDB.QueryDefs.SQL = " ... your sql here ..."
DoCmd.OpenReport "ReportNameHere", acPreview
Steve
If I understand your question, I need the entire process to be dynamic since I cannot require User intervention. The field names would be something like: "Field1", "Table1_Field2", "Table2_Field2", "Table1_Field3", "Table2_Field3" (the results could be captured by either a Query or a Table).
Thanks again.
Jim
bramsquad,
I don't know if you are still out there, but I tried your statement as follows:
Dim SQL As String
Dim qdef As QueryDef
SQL = "SELECT FROM Table1 INNER JOIN Table2 ON Table1.Field1 = Table2.Field1;"
Set qdef = CurrentDb.CreateQueryDef("
But I get the following error "The SELECT statement includes a reserved word or an argument name that is mispelled or missing, or the punctuation is incorrect."
I should note that Table1 has some Fields that are not in Table2. While I do not believe that is causing the error, I believe I need to somehow refer to the common fields in addition to Field1 (i.e. Field2 and Field3) of both Table1 and Table2. That is, I would like "qryTest" to include the following five fields: Field1, Table1:Field2, Table2:Field2, Table1:Field3, and Table2:Field3).
Thank you for any help.
Jim
Jim, have you built the basic SEELCT query in the Access QBE? After you get it showing you the results then change the query type to either Make Table (if you want to make a new table every time) or Append if you are going to add this information to the same table evey time) Once yu have this done you can call the saved query via code ...
CurrentDB.Execute "YourNewQueryName", dbFailOnError.
If you use the Access QBE window to build the SQL it may be much easier and you will also be able to see the SQL it writes for you. Add both tables you want to the query, if you have a relationship already defined between the 2 tables Access will create the join automatically. If Access does not make the join for you you can left mouse down and drag the field from one table to the other to create the relationship.
Steve
Business Accounts
Answer for Membership
by: stevbePosted on 2004-02-10 at 09:21:38ID: 10323958
what do you want to do with the results ... form, report, export?
steve