Solved

how to loop through table1 to select from table2, insert into table3

Posted on 2004-08-29
9
557 Views
Last Modified: 2012-05-05
I have three tables in Access.
One is the original data (tblOriginal), containing 4 fields: StID,
Name, Pre, Type. One contains multiple records of selection criteria
(tblCriteria), three of which are used to match those in tblOriginal:
Name, Pre, and Type. The third table is the destination table
(tblAltNames), into which I want to insert matching records with all
four fields from tblOriginal.

I would like to loop through tblCriteria, building an SQL query for
each record that SELECTs FROM TblOriginal and INSERTs INTO
tblAltNames.

FYI - I'm using DAO so far because I figured it would be an easier
learning curve but it doesn't seem to be helping... Ultimately it
should be ADO anyway, so I'd appreciate advice for either. **Bonus
points** for ADO sol'n.

Here's what I have:

Public Sub BuildAltNamesTable()

Dim db As Database
Dim rec As Recordset
Dim strSELECT As String
Dim strFROM As String
Dim strINTO As String
Dim strWHERE As String
Dim strSQL As String

strSELECT = "StID, Name, Pre, Type"
strFROM = "tblOriginal"
strINTO = "tblAltNames"

Set db = CurrentDb()
Set rec = db.OpenRecordset(tblCriteria, dbOpenSnapshot)

rec.MoveFirst
Do While Not rec.EOF

 If IsNull(tblCriteria.Name) Then
   strWHERE = strWHERE & " AND tblCriteria.Name = tblOriginal.Name"
 End If

 If IsNull(tblCriteria.Pre) Then
   strWHERE = strWHERE & " AND tblCriteria.Pre = tblOriginal.Pre"
 End If

 If IsNull(tblCriteria.Type) Then
   strWHERE = strWHERE & " AND tblCriteria.Type = tblOriginal.Type"
 End If

 strSQL = "SELECT " & strSELECT
 strSQL = strSQL & "FROM " & strFROM
 strSQL = strSQL & "INTO " & strINTO
 If strWHERE <> "" Then
   strSQL = strSQL & "WHERE " & Mid$(strWHERE, 6)
 End If

[execute strSQL? help needed]

 rec.MoveNext
Loop

rec.Close
Set rec = Nothing

End Sub
0
Comment
Question by:molvo
  • 5
  • 2
  • 2
9 Comments
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 11927861
To execute your SQL, just do:

CurrentDb.Execute strSQL

No real need to use ADO unless your back-end is something other than Jet.
0
 
LVL 7

Expert Comment

by:rockmuncher
ID: 11927864
>> [execute strSQL? help needed]  <<

docmd.runSQL strSQL


0
 
LVL 41

Assisted Solution

by:shanesuebsahakarn
shanesuebsahakarn earned 200 total points
ID: 11927873
A couple of points in favour of the Execute method over RunSQL - it doesn't pop up the confirmation dialogs, and can be around 25% faster. Both will work though.
0
 

Author Comment

by:molvo
ID: 11928634
Thanks. The execute syntax was the hole I knew I was missing, but I was hoping for some help getting the rest to run as well. Mind if I ask for a bit more? After filling in the execute SQL line, I get a variable not defined error on tblCriteria in the If IsNull statements, e.g.,

If IsNull(tblCriteria.Name) Then   '****this is the line where the error occurs
   strWHERE = strWHERE & " AND tblCriteria.Name = tblOriginal.Name"
 End If

Any ideas?
And does the rest of the logic, structure, etc. look right?

BTW, eventually it will be shared by people with SQL Server DBs, so that's why I was thinking ADO, but I'm okay taking it slowly in DAO for now.

Thanks again.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Assisted Solution

by:rockmuncher
rockmuncher earned 50 total points
ID: 11928646
You haven't set tblCriteria to be anything.  You should be using the rec recordset variable instead, like this

If IsNull(Rec!Name) Then
   strWHERE = strWHERE & " AND tblCriteria.Name = tblOriginal.Name"
 End If
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 11928655
Hmm, without knowing what data your criteria table should hold, I couldn't say if the logic is right or not. However, some corrections you need to make to the code:

Set rec = db.OpenRecordset(tblCriteria, dbOpenSnapshot)
should read:
Set rec = db.OpenRecordset("tblCriteria", dbOpenSnapshot)

This opens a recordset based on the table called tblCriteria. The name has to be passed to OpenRecordset as a string, or a string literal (as I have done here). Now, whereever you have:
tblCriteria.
replace with:
rec!

So in your line:
If IsNull(tblCriteria.Name) Then
this will now read:
If IsNull(rec!Name) Then

Basically, now that you have opened a recordset called rec that is based on the table tblCriteria, rec contains all of the fields (and data) from tblCriteria. So you reference the recordset, not the table. Also note that you should never use "Name" as the name of a field - this can confuse Access, since "Name" is a reserved word. If you do use it, always enclose it between [] in code. So the above line should really read:
If IsNull(rec![Name]) Then
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 11928666
Also, I suspect lines like this:
strWHERE = strWHERE & " AND tblCriteria.Name = tblOriginal.Name"

should read:
strWHERE = strWHERE & " AND tblOriginal.Name=" & rec![Name]

since you are trying to select from tblOriginal where the fields are equal to whatever data you have stored in tblCriteria (which you have just picked out in your recordset).
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 200 total points
ID: 11928668
Sorry, if those are text fields, they should read:
strWHERE = strWHERE & " AND tblOriginal.Name=""" & rec![Name] & """"
0
 

Author Comment

by:molvo
ID: 11932521
Ah, much better. Thanks. I also realized that my If IsNull...Then statements were backward; I needed an If Not IsNull for that to execute correctly.

A belated clarification, everything is text fields except the ID field in tblOriginal.

Thanks for the reminder about Name being a reserved word -- no problems in my actual table because the real fieldname is longer. I simplified the field names for use here.

So now I'm focusing on the SQL statement itself. It still seems wrong. I'm thinking I need to build something like:
INSERT INTO tblAltNames SELECT StID, Name, Pre, Type FROM tblOriginal WHERE tblOriginal.Name = ...

I'll post this as a separate question here for more points. Thanks for all your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now