Access 2010: Creating subreport with VBA with change in Query

I'm using Access 2010 and giving the user a method of selecting records to view AND the fields to view.  
I create a query on the fly, always naming it the same.  The field names can change as can the where clause.  I handle all this through VBA quite smoothly.

Now the tough part - I wish to display the results in a data view subform. This will allow the user to size and sort.
Two questions:
1) When I set up the subreport and tell it to base it off of a query, it uses the fields that exist AT THE TIME of creation.  My routine changes the query and the fields. I would like the subreport to use the same named query but the fields have changed.  I'm thinking I can place fields in the detail section of the subreport on the fly, requery and everything should be good.
Does anyone know how to do something like this?  I'm not talking about new records - that's handled by by where clause in the query and it's working a query.  Problem is that the fields have changed as well.

2) is there a way for the autosize to use column headers as well as data to autosize the columns?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
You can do this but it is fraught with pitfalls
Won't work in MDE files
Will decompile your application
The VBA syntax is different from what you may be used to

You can look into the CerateControl Method here:

Jeffrey Coachman

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeffrey CoachmanMIS LiasonCommented:
There are quite a few "Grid" Controls out there...
Many can be used as subforms.

I have only worked with the one in VB6, in Access. (Not sure if it was a data Grid, or a Flex Grid)

In any event here are a few links for you to investigate:

I believe you can read in the number of Columns/Fields form your query and produce the grid with the required number of columns.

Jeffrey CoachmanMIS LiasonCommented:
I know it seems like a nice thing to do to give the users this much flexibility...

But if they need this much control, then why not just teach them to build queries on their own?

What I try to do is create systems that will encompass all of the fields a use might need.
Having controls generate at runtime creates too much complexity to the App.
(You have to worry about "overflow" errors (where the size of your shiny new control might exceed the limits of the form, and generate an error) , or this control may cover/overlap other controls.
Not for the faint of heart...
Most Access developers will warn against doing this for production databases...

The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

GNOVAKAuthor Commented:
Is it possible to size a column in the data view via dba?
I'm thinking perhaps of loading all columns, setting the default size to 0 and only correct sizing the fields selected....
Jeffrey CoachmanMIS LiasonCommented:
You can resize the columns of a datasheet view subform, ...from the main form, by using code like this:

Me.YoursubFormName!ControlName.ColumnWidth = 1440

Note that the 1440 is in Twips,
1440 Twips =1 inch

GNOVAKAuthor Commented:
So I could create a form with all potential fields,and, as I make the query, I could set those field widths and zero the others... That might work.
Ill have to name all the text boxes on the form. How do I loop through and change them?
I'm thinking something like this in the form itself
For count = 1 to me. Columns.count
   Me.columns.item(count) =<some name>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.