View Full Version : How do you add a field to an existing query using vba?


chillwa
06-16-2008, 01:09 PM
How do you add a field to an existing query using vba? Using MS Access module


I need to add/append a table field to an existing select query and save the query using vba code.

1. I will specify the name of the field to be added
2. I need to specify the table it is added from
3. I need to specify in the totals row that it is "SUM"
4. I also need to specify the caption for the newly added field

Then I will save the query


This query is tricky because I am using the "SUM" totals for most of my existing query fields except for 2.


I have been trying to figure this out for a day and a half and I am getting nowhere, ANY help would be appreciated.

I cannot even figure out the code to alter the query.

petehilljnr
06-16-2008, 01:21 PM
Check out the QueryDef in the Help.

Quickly:

Dim qry as DAO.QueryDef

set qry = Currentdb.QueryDef("MyQuery")

Debug.Print qry.SQL ' shows the SQL from MyQuery

qry.SQL = "SELECT * FROM AnotherTable"

'MyQuery is now changed

qry.close

chillwa
06-16-2008, 03:23 PM
Thanks, it worked. I really appreciate the help