Solved

Open Access Report using varying columns

Posted on 2012-04-10
2
263 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

808 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