Access 2010: Creating subreport with VBA with change in Query

Posted on 2013-08-27
Medium Priority
Last Modified: 2014-02-19
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 great...as 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?

Question by:GNOVAK
  • 4
  • 2
LVL 74

Accepted Solution

Jeffrey Coachman earned 1500 total points
ID: 39443258
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
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 1500 total points
ID: 39443283
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.

LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39443313
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...

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.


Author Comment

ID: 39443404
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....
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 1500 total points
ID: 39443583
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


Author Comment

ID: 39443745
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>

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
A Case Study of using the Windows API to provide RS232 communications capability in Access without the use of Active-X controls.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question