Solved

Open Access Report using varying columns

Posted on 2012-04-10
2
261 Views
Last Modified: 2012-04-10
I have a table with 25 field names like this:
slot_1
slot_2
slot_3
... slot_25

I have a report that will only need one of the fields at a time.  The report should be formatted like this:
Date:              Slot_(x)
4/10/12           .011
4/10/12           .010
4/10/12           .012

The data in the slot column will vary depending on which slot is being used.

I have the field name from a non visible text box on the form that opens the report.
 
The report is opened like this:
strWhere = "Select Burn_In_Date, " & strSlot
strWhere = strWhere & " FROM Part_Voltage"
strWhere = strWhere & " WHERE (((Part_Voltage.BurnIn_Date) Between [Forms]![frm_rpt_Report1]![txtBurn_In_Start] And DateAdd("
strWhere = strWhere & "'h'"
strWhere = strWhere & ",20,[Forms]![frm_rpt_Report1]![txtBurn_In_Start])));"
DoCmd.OpenReport "rpt_PartVoltage", acViewPreview, strWhere


Can I use a variable for a field name?
Some other way?

Thanks,
Brooks
0
Comment
Question by:gbnorton
2 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 37829566
1.  Not like the way you have it in your example.  The strWHERE clause (which is actually the 4th argument, not the 3rd in the OpenReport method does not accept an entire SQL string, like you have provided.  All it woud accept is the part of the SQL string to the right of the word "WHERE".  So what you need to do is create the SELECT portion of your query, and save that as the SQL string of a query.  To do this,
    a.  Start out by creating a query by using the query grid. When you are done, save the query as qry_rpt_PartVoltage
    b.  Then modify your code above so that after you have completed building the SQL string, you update the SQL property of the query.  Something like:

strSQL = "Select Burn_In_Date, " & strSlot & " as Slot_x " _
           & "FROM Part_Voltage "
           & "WHERE (((Part_Voltage.BurnIn_Date) Between #" & [Forms]![frm_rpt_Report1]![txtBurn_In_Start] & "# " _
           & "And DateAdd('h',20,#" & [Forms]![frm_rpt_Report1]![txtBurn_In_Start] & "#)));"
currentdb.querydefs("qry_rpt_PartVoltage").SQL = strSQL

    c.  Then you don't need to apply the WHERE argument to the report, you just open the report, which has this as your query.

Once you do that, the field in your report will always be referred to as "Slot_x".  So, if I were you, I would also add something to the report which would identify the actual field that was used to generate that report.
0
 

Author Closing Comment

by:gbnorton
ID: 37829682
Thanks a bunch for the prompt solution.
Brooks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now