View Full Version : Using control names in SQL


AaronC
07-14-2005, 09:44 AM
I am creating a query that retrieves data from one table and creates a second table. I want to name the columns in the new table using the contents of the controls from a form within the same mdb. The form will be open when this query is run, but I am having trouble creating and saving the query.

Here is a sample of what I am trying to do...Any help is greatly appreciated!!

Thanks

SELECT IndividualHolidays.SU, IndividualHolidays.SSN, IndividualHolidays.Init, IndividualHolidays.LName,
Max(IndividualHolidays.NewYearsDay) AS Forms!HolidayDatesEntry!NewYearsDay,
Max(IndividualHolidays.AftNewYears) AS Forms!HolidayDatesEntry!AftNewYears,
Max(IndividualHolidays.PresDay) AS Forms!HolidayDatesEntry!PresDay,
Max(IndividualHolidays.AftPresDay) AS Forms!HolidayDatesEntry!AftPresDay
…<etc>
INTO HolidaysByEmp
FROM IndividualHolidays
GROUP BY IndividualHolidays.SU, IndividualHolidays.SSN, IndividualHolidays.Init, IndividualHolidays.LName;

Pat Hartman
07-14-2005, 10:10 AM
Parameters can only substitute for data. They cannot substitute for anything else. If you want to do this, you will need to build the SQL in code since it is dynamic.

AaronC
07-14-2005, 10:26 AM
Thanks, I was hoping for an alternative. I run into memory problems when I build the SQL statement in VB...that's why I was trying to do it in a stored query. There must be a workaround... :(

Pat Hartman
07-14-2005, 10:43 AM
Why must there be a workaround? Dynamic SQL is dynamic and Dynamic SQL must be built in VBA. Only Static SQL may be saved as a querydef. Static SQL is compiled. That means that it doesn't change. Static SQL may take parameters to supply values for criteria but if you change the field list or predicate, the query becomes dynamic.

AaronC
07-14-2005, 01:29 PM
Thanks! I created to SQL and the form parameters into the querydef and it works great.