473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

multiple append queries using VBA

good morning,

im still kind of new to VBA but I have learned some interesting things
so far. Right now I am normalizing my survey database so I'm having
to run an append query for each question(300 in some surveys).
WARNING: I've very new at this so this may not look right. i hope it
gives the idea of what I'm trying to accomplish. Before I was just
chaning the 1's to 2's then to 3's in the Design view of my query.
hope this can save me some time. thanks in advance!

Sub RunAppQuery()

Dim db As DAO.Database
Dim qryTest As DAO.QueryDef
Dim IntCnt As Integer

Set db = CurrentDb()
Set qryTest = dbsCurrent.QueryDefs("Query1")

For IntCnt = 1 to 300

qryTest.SQL = "INSERT INTO tblResponses ( RspnsID, Rspns, QstnID )
SELECT FT.ID, FT.FT" & IntCnt & ", "FT" & IntCnt & "" AS Expr1
FROM FT;"

Next IntCnt

End Sub
Nov 12 '05 #1
5 4880
ba******@yahoo.com (Barn Yard) wrote in message news:<e0**************************@posting.google. com>...
good morning,

im still kind of new to VBA but I have learned some interesting things
so far. Right now I am normalizing my survey database so I'm having
to run an append query for each question(300 in some surveys).
WARNING: I've very new at this so this may not look right. i hope it
gives the idea of what I'm trying to accomplish. Before I was just
chaning the 1's to 2's then to 3's in the Design view of my query.
hope this can save me some time. thanks in advance!

Sub RunAppQuery()

Dim db As DAO.Database
Dim qryTest As DAO.QueryDef
Dim IntCnt As Integer

Set db = CurrentDb()
Set qryTest = dbsCurrent.QueryDefs("Query1")

For IntCnt = 1 to 300

qryTest.SQL = "INSERT INTO tblResponses ( RspnsID, Rspns, QstnID )
SELECT FT.ID, FT.FT" & IntCnt & ", "FT" & IntCnt & "" AS Expr1
FROM FT;"

Next IntCnt

End Sub


Do everyone that could help you a favor... post ABBREVIATED versions
of the structures of your original table and final table. If you have
the question number as a field, you could loop through the fields
collection of the table/query and use those for your append...

Something like
tdf.Fields(intCounter)
or
qdf.fields(intCounter)

just remember that the Fields collection in a querydef or a tabledef
is zero-based, so it will run from 0 to (tdf.fields.count-1)

Also, you could just use a recordset for this. Probably faster than
executing queries all over the place....
Nov 12 '05 #2
Pieter,

Thank you for replying and sorry about the lack of table structures.
I'll remember to put those in from now on. :) The FT1, FT2, and so on
are the question responses. I'm using append queries for each
question in order to arrive to tblResponses, I was looking for a
faster was to get there. I'm unsure how to use the recordset
property. Can you please show me a short example of how i might use
it? Thanks so much! -B

Here are the table structures:

----
tblFT (non-relational design)

ID Number
FT1 Text
FT2 Text
FT3 Text
FT4 Text
FT5 Text
FT6 Text
and so on....
-----
tblResponses' (normalized)

ID Number
QstnID Text
Respns Text
-----
pi********@hotmail.com (Pieter Linden) wrote in message news:<bf**************************@posting.google. com>...
ba******@yahoo.com (Barn Yard) wrote in message news:<e0**************************@posting.google. com>...
good morning,

im still kind of new to VBA but I have learned some interesting things
so far. Right now I am normalizing my survey database so I'm having
to run an append query for each question(300 in some surveys).
WARNING: I've very new at this so this may not look right. i hope it
gives the idea of what I'm trying to accomplish. Before I was just
chaning the 1's to 2's then to 3's in the Design view of my query.
hope this can save me some time. thanks in advance!

Sub RunAppQuery()

Dim db As DAO.Database
Dim qryTest As DAO.QueryDef
Dim IntCnt As Integer

Set db = CurrentDb()
Set qryTest = dbsCurrent.QueryDefs("Query1")

For IntCnt = 1 to 300

qryTest.SQL = "INSERT INTO tblResponses ( RspnsID, Rspns, QstnID )
SELECT FT.ID, FT.FT" & IntCnt & ", "FT" & IntCnt & "" AS Expr1
FROM FT;"

Next IntCnt

End Sub


Do everyone that could help you a favor... post ABBREVIATED versions
of the structures of your original table and final table. If you have
the question number as a field, you could loop through the fields
collection of the table/query and use those for your append...

Something like
tdf.Fields(intCounter)
or
qdf.fields(intCounter)

just remember that the Fields collection in a querydef or a tabledef
is zero-based, so it will run from 0 to (tdf.fields.count-1)

Also, you could just use a recordset for this. Probably faster than
executing queries all over the place....

Nov 12 '05 #3
ba******@yahoo.com (Barn Yard) wrote in message news:<e0**************************@posting.google. com>...
Pieter,

Thank you for replying and sorry about the lack of table structures.
I'll remember to put those in from now on. :) The FT1, FT2, and so on
are the question responses. I'm using append queries for each
question in order to arrive to tblResponses, I was looking for a
faster was to get there. I'm unsure how to use the recordset
property. Can you please show me a short example of how i might use
it? Thanks so much! -B

Here are the table structures:

----
tblFT (non-relational design)

ID Number
FT1 Text
FT2 Text
FT3 Text
FT4 Text
FT5 Text
FT6 Text
and so on....
-----
tblResponses' (normalized)

ID Number
QstnID Text
Respns Text
-----

So FT# is the question, and the response goes in that field. OK...

I think I need some amyl nitrate... LOL... so basically what you want
to do is loop through your denormalized table and grab the ID and FT#
(where # is some number corresponding to a field in the table), right?
If so, you could create your SQL statement using

tdf.fields(0).Name and tdf.fields(intCounter).Name Something like...

For intCounter = 1 to tdf.fields.count-1
strSQL = "INSERT INTO tblResponses(QstnID,Rspns) VALUES (SELECT
tblResponses.QstnID " & tdf.fields(intCounter).Name & " FROM
tblResponses)"
db.Execute strSQL, dbFailOnError
Next intCounter

Confused yet? Basically, the way I would go about it is to loop
through the fields collection of your denormalized table, and use a
CurrentDB.Execute strSQL kind of thing, where you build the SQL
statement on the fly. Have a look at the fields collection in help.
(A table consists of a zero-based collection of fields, so you can
refer to a field as tdf.fields(intFieldNo))

So then you can see automate most of this.

If this is all just way over your head, try something simple first to
get an idea of what's going on... something like

dim db as dao.database
dim tdf as dao.tabledef
dim fld as dao.field

set db=currentdb
set tdf = db.Tabledefs("tblFT")
for intCounter = 0 to tdf.fields.Count-1
debug.print intCounter, tdf.fields(intCounter).Name
next intCounter
set tdf=nothing
set db=nothing

you'll get something like this:
0 ID
1 FT1
2 FT2
3 FT3
....

then you can use that to build your SQL statement on the fly, and use
currentdb.Execute strSQL to execute the SQL statement for you.

Confused? Great! Mission accomplished. <g>
Nov 12 '05 #4
Pieter,

I think I got it but I'm stuck on building strSQL. The one you
provided showed up red in the VB editor and showed it getting stuck
at:

strSQL = "INSERT INTO tblResponses(QstnID,Rspns) VALUES (SELECT
*here* tblResponses.QstnID " & tdf.fields(intCounter).Name & " FROM
tblResponses)"

I tried to fiddle with it and tried putting it on one line but
couldn't get it to work. The one I posted below I know is wrong
because I get a run-time error 3346 'Number of query fields and
destination fields are not the same'. Thanks for all your help. But
I'm understanding how the looping works as I see the value of
tdf.fields(intCounter).Name changes as I push F8 in the editor. This
is what I am currently have:

Thanks for all your help again!

Option Compare Database

Sub LoopFields()

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim intCounter As Integer
Dim strSQL As String

Set db = CurrentDb
Set tdf = db.Tabledefs("tblFT")
For intCounter = 0 To tdf.Fields.Count - 1
Debug.Print intCounter, tdf.Fields(intCounter).Name
strSQL = "INSERT INTO tblResponses ( RspnsID, QstnID, Rspns )
SELECT tblFT." & tdf.Fields(intCounter).Name & " FROM FT;"
db.Execute strSQL, dbFailOnError
Next intCounter

Set tdf = Nothing
Set db = Nothing

End Sub


pi********@hotmail.com (Pieter Linden) wrote in message news:<bf*************************@posting.google.c om>...
ba******@yahoo.com (Barn Yard) wrote in message news:<e0**************************@posting.google. com>...
Pieter,

Thank you for replying and sorry about the lack of table structures.
I'll remember to put those in from now on. :) The FT1, FT2, and so on
are the question responses. I'm using append queries for each
question in order to arrive to tblResponses, I was looking for a
faster was to get there. I'm unsure how to use the recordset
property. Can you please show me a short example of how i might use
it? Thanks so much! -B

Here are the table structures:

----
tblFT (non-relational design)

ID Number
FT1 Text
FT2 Text
FT3 Text
FT4 Text
FT5 Text
FT6 Text
and so on....
-----
tblResponses' (normalized)

ID Number
QstnID Text
Respns Text
-----

So FT# is the question, and the response goes in that field. OK...

I think I need some amyl nitrate... LOL... so basically what you want
to do is loop through your denormalized table and grab the ID and FT#
(where # is some number corresponding to a field in the table), right?
If so, you could create your SQL statement using

tdf.fields(0).Name and tdf.fields(intCounter).Name Something like...

For intCounter = 1 to tdf.fields.count-1
strSQL = "INSERT INTO tblResponses(QstnID,Rspns) VALUES (SELECT
tblResponses.QstnID " & tdf.fields(intCounter).Name & " FROM
tblResponses)"
db.Execute strSQL, dbFailOnError
Next intCounter

Confused yet? Basically, the way I would go about it is to loop
through the fields collection of your denormalized table, and use a
CurrentDB.Execute strSQL kind of thing, where you build the SQL
statement on the fly. Have a look at the fields collection in help.
(A table consists of a zero-based collection of fields, so you can
refer to a field as tdf.fields(intFieldNo))

So then you can see automate most of this.

If this is all just way over your head, try something simple first to
get an idea of what's going on... something like

dim db as dao.database
dim tdf as dao.tabledef
dim fld as dao.field

set db=currentdb
set tdf = db.Tabledefs("tblFT")
for intCounter = 0 to tdf.fields.Count-1
debug.print intCounter, tdf.fields(intCounter).Name
next intCounter
set tdf=nothing
set db=nothing

you'll get something like this:
0 ID
1 FT1
2 FT2
3 FT3
...

then you can use that to build your SQL statement on the fly, and use
currentdb.Execute strSQL to execute the SQL statement for you.

Confused? Great! Mission accomplished. <g>

Nov 12 '05 #5
Barn,

This *might* be what you're after. I made the code write all the SQLs
to the debug window so you could look at them before executing them.
Just comment out the "db.Execute strSQL, dbFailOnError" line and run
the code.
Sub AppendRecordsToNormalizedTable()
Dim db As DAO.Database
Dim tdf As DAO.TableDef

Dim intCounter As Integer
Dim strSQL As String

Set db = CurrentDb
Set tdf = db.TableDefs("tblFT")

For intCounter = 1 To tdf.Fields.Count - 1
strSQL = "INSERT INTO tblResponses(RspnsID,Rspns,QstnID)
SELECT ID, " & tdf.Fields(intCounter).Name & ", " & intCounter & " AS
QuestID FROM tblFT;"
Debug.Print strSQL
'db.Execute strSQL, dbFailOnError '---Remove leading ' to
run...
Debug.Print
Next intCounter

Set tdf = Nothing
Set db = Nothing
End Sub
Okay, the tdf.Fields(intCounter).Name thing is just looping through
the fields in the table (I'm skipping 0,the ID field on purpose). So
I'm going across the table and building the SQL statements on the fly
(in the strSQL variable) and then executing them. Hope this is closer
than before... and DON'T run this on real data... well, not yet
anyway...

Hope this works for ya,
Pieter
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Brian Kelley | last post by:
I am trying to use threads and mysqldb to retrieve data from multiple asynchronous queries. My basic strategy is as follows, create two cursors, attach them to the appropriate databases and then...
1
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; ...
0
by: MHenry | last post by:
Hi, I know virtually nothing about creating Macros in Access. I would appreciate some help in creating a Macro or Macros that automatically run(s) 14 Queries (three Make Table Queries, and 11...
3
by: matthewemiclea | last post by:
I am trying to display information in Access on a form that takes information from many different queries and puts them together on a spreadsheet or chart. Some specific info: The information I...
8
by: Chris A via AccessMonster.com | last post by:
I have an interesting problem that I have yet to come accross that I can't change data structure on because it is an export from filemaker I am reformatting for another dept. anyway. I have a table...
4
by: robboll | last post by:
When I try to use an append query from an oracle link it takes forever. I am exploring the idea of doing an append action using a pass-through query. If I have an Oracle ODBC connection to...
1
by: fong.yang | last post by:
I've got a database with about 300,000 records. There are several different tables that are set up the same way with identical fields. I have the same queries individually set up for each table. ...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
14
by: Supermansteel | last post by:
My team at work uses Cognos to run multiple queries to pull in data. Then they take that data and import into Access and then usually run an Append Query to run a RND function to pull out a few...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.