473,766 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Quer yDefs("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 4920
ba******@yahoo. com (Barn Yard) wrote in message news:<e0******* *************** ****@posting.go ogle.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.Quer yDefs("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(intC ounter)
or
qdf.fields(intC ounter)

just remember that the Fields collection in a querydef or a tabledef
is zero-based, so it will run from 0 to (tdf.fields.cou nt-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********@hotm ail.com (Pieter Linden) wrote in message news:<bf******* *************** ****@posting.go ogle.com>...
ba******@yahoo. com (Barn Yard) wrote in message news:<e0******* *************** ****@posting.go ogle.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.Quer yDefs("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(intC ounter)
or
qdf.fields(intC ounter)

just remember that the Fields collection in a querydef or a tabledef
is zero-based, so it will run from 0 to (tdf.fields.cou nt-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.go ogle.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).N ame and tdf.fields(intC ounter).Name Something like...

For intCounter = 1 to tdf.fields.coun t-1
strSQL = "INSERT INTO tblResponses(Qs tnID,Rspns) VALUES (SELECT
tblResponses.Qs tnID " & tdf.fields(intC ounter).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.Execu te 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(intF ieldNo))

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("t blFT")
for intCounter = 0 to tdf.fields.Coun t-1
debug.print intCounter, tdf.fields(intC ounter).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.Execu te 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(Qs tnID,Rspns) VALUES (SELECT
*here* tblResponses.Qs tnID " & tdf.fields(intC ounter).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(intC ounter).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("t blFT")
For intCounter = 0 To tdf.Fields.Coun t - 1
Debug.Print intCounter, tdf.Fields(intC ounter).Name
strSQL = "INSERT INTO tblResponses ( RspnsID, QstnID, Rspns )
SELECT tblFT." & tdf.Fields(intC ounter).Name & " FROM FT;"
db.Execute strSQL, dbFailOnError
Next intCounter

Set tdf = Nothing
Set db = Nothing

End Sub


pi********@hotm ail.com (Pieter Linden) wrote in message news:<bf******* *************** ***@posting.goo gle.com>...
ba******@yahoo. com (Barn Yard) wrote in message news:<e0******* *************** ****@posting.go ogle.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).N ame and tdf.fields(intC ounter).Name Something like...

For intCounter = 1 to tdf.fields.coun t-1
strSQL = "INSERT INTO tblResponses(Qs tnID,Rspns) VALUES (SELECT
tblResponses.Qs tnID " & tdf.fields(intC ounter).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.Execu te 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(intF ieldNo))

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("t blFT")
for intCounter = 0 to tdf.fields.Coun t-1
debug.print intCounter, tdf.fields(intC ounter).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.Execu te 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 AppendRecordsTo NormalizedTable ()
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("t blFT")

For intCounter = 1 To tdf.Fields.Coun t - 1
strSQL = "INSERT INTO tblResponses(Rs pnsID,Rspns,Qst nID)
SELECT ID, " & tdf.Fields(intC ounter).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(intC ounter).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
10575
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 spawn worker functions to execute sql queries and process the results. This works occasionally, but fails a lot taking python down with it. Sometimes it also loses connection to the database. Sometimes I get an error, "Commands out of sync; ...
1
3426
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; _____________________________________________________ SELECT "criteria"
0
8778
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 Append Queries) when the Database is first opened, and then again anytime I invoke the Macro (somehow) after the database is already open. The latter function is useful, because the queries must be rerun every time data is added, deleted, or...
3
12732
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 am displaying consists of a work order schedule(for the current date), work orders completed , work orders not completed, a past due schedule, past due completed, and past due not completed. These are grouped by workcenters. Therefore, one...
8
4615
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 like so... Table 1 Field1 Field2 Field3 E1 April 2006 AA, BB, CC E2 April 2006 AA, BB, CC,DD, EE E3 April 2006 AA, BB
4
7077
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 server OraTest. User: User1 Password: password and I am trying to append all records in table: tblTEST that are code: "abc"
1
3410
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. Is there a way to just set the query once and then run it across several tables or tell the query which table to run it on. Some of the queries are just select queries, some are append, and some are update. Thanks.
11
3677
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) and the 'employee name'. There is another table which assigns an ID to the Shifts, i.e. 1,2 and 3 for morn, eve & night shifts respectively. From the mother table, the incentive is calculated datewise for each employee as per his shift duty. In...
14
6684
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 samples for them to review the accounts. I was making a suggestion to my team that maybe if we only ran one Cognos query and pulled in all of the columns that everyone needs to pull samples for each of there tests that this would be better on the time...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.