Google Answers Logo
View Question
 
Q: VBA to loop through table to select and update new table ( No Answer,   2 Comments )
Question  
Subject: VBA to loop through table to select and update new table
Category: Computers > Programming
Asked by: polkadot-ga
List Price: $17.50
Posted: 26 Aug 2004 17:07 PDT
Expires: 30 Aug 2004 16:17 PDT
Question ID: 393164
Semi-beginner question for a VBA module in Access 2002 (we all have to
start somewhere!):

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.

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
Answer  
There is no answer at this time.

Comments  
Subject: Re: VBA to loop through table to select and update new table
From: mightymingy-ga on 28 Aug 2004 02:47 PDT
 
Hi polkadot-ga,

Replace [execute strSQL? help needed] with the following lines of code:

db.Execute strSQL    ' Execute the sql statement
strWHERE = ""        ' Ready for next iteration

Hope this helps.
Subject: Re: VBA to loop through table to select and update new table
From: polkadot-ga on 30 Aug 2004 16:16 PDT
 
thanks for the suggestion -- this was what I was looking for, and I
managed to fix some other trouble myself (and with some help at
experts-exchange.com)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy