Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open Access Report using varying columns

Posted on 2012-04-10
2
Medium Priority
?
268 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 49

Accepted Solution

by:
Dale Fye earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

927 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