[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ACCESS 2010 DYNAMIC FIELDS DAO

Posted on 2013-01-23
14
Medium Priority
?
327 Views
Last Modified: 2013-01-30
Hi All.
I need to dyamically assign source-data fields (in RecordSource) to report (detail) fields (DAO) upon report load. (need to use one report-template for multiple queries of unknown field-names or number of fields.
(I have done the labels but cannot locate the properties the data fields)
Does anyone have a one-line assignment statement / code snippet that assigns source fields to report fields?
e.g. report.fields(2).ControlSource = Me.Recordsource.Fields(F2)

I can do the looping etc. just need the core properties.
0
Comment
Question by:COACHMAN99
  • 7
  • 7
14 Comments
 
LVL 85
ID: 38812429
Report.Controls("YourControlname").ControlSource = SomeFieldname

or

Report.Controls(2).controlsource = SomeFieldname

Better yet, build your report based on a query that outputs generic field names:

SELECT fieldA AS Field1, FieldB AS Field2, FieldC AS Field3 FROM MyTable

Now when you need to open the report, just change the SQL of that query:

Dim qdf As DAO.QueryDef
Set qdf = Currentdb.QueryDefs("YourQueryname")
qdf.SQL = "SELECT CustomerName As Field1, CustomerPhone As Field2, '' As Field3 FROM Customertable"
Set qdf = nothing

If your report is based on the query named "YourQueryName", you could then open the report and it would show you data from Customertable.
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 38812632
Thanks for attempt.
the missing part is your 'somefieldname' e.g. Me.Recordsource.Fields(F2)
(which doesn't work).
What is the syntax for referencing the datasource field / data?
I've tried recordsource and recordsetcloe but haven't quite got it.

cheers
0
 
LVL 85
ID: 38812642
"FieldName" is just the name of a field defined in your recordsource. I'm not sure how you'd do this; you might try

Me.Recordset.Fields(1).Name

However, review the QueryDef method I suggest in my earlier comment.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 7

Author Comment

by:COACHMAN99
ID: 38812695
thats one of the things I tried before posting (see original post above).
I also did a partial imlementation of the querydef before posting.
For my requirement I really need the syntax for addressing fields in the datasource of the report.
0
 
LVL 85
ID: 38812760
One of the issues with this is that you're dealing with a Report, which sets the Recordsource and such much earlier than a Form.

hats one of the things I tried before posting (see original post above).
I don't see where you use the RECORDSET ... but I could be mistaken. Regardless, the best method to use to set field names dynamically on a report is to use the QueryDef method described above.

You could also use a temp table. Build a temp table that has fields named like "Field1", "Field2", etc etc, and base your report on that. Now fill the temp table using VBA before opening the report.
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 38812996
While I was waiting for input from EE I also implemented a temp table/vba solution although, for my requirement I really need the syntax for referencing fields in the datasource of the report.
I will wait a while to see if anyone has the required object-model/syntax.
thanks for the attempt.
0
 
LVL 85
ID: 38813808
Have you tried doing this in the OPEN event of the report instead of the LOAD event? The OPEN event might give you a better handle to the datasource.
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 38815025
II tried both, the (data) objects are limited (better in adp).
0
 
LVL 85
ID: 38815700
I'm just not sure you can do this (i.e. set the ControlSource of a report's controls at runtime). I could be wrong, but I recall trying this long ago and abandoning it in favor of the query approach suggested earlier.
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 38816655
you can. I need to reference the datasource properties/fields.
0
 
LVL 85
ID: 38816808
If you can't get it from the report's RecordSource or from the report's Recordset then you're out of luck. There are no other "datasource" properties of the Report object (other than RecordsetClone, which is just a clone of the Recordset).
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 38836666
the fact that the (datasource) properties are accessible in the designer implies they are referenceable at runtime. Maybe the parent object isn't ME.?
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1500 total points
ID: 38837102
There are many design-time properties that are not available during runtime (and vice-versa). Some may be accessible, but in a read-only state (for example).

Maybe the parent object isn't ME.?
The ME qualifier just refers to the currently active object, whatever that may be. In  your case, that's the Report object. If you were working with a Form, then ME would refer to the active form ...

This seems to work:

 Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM rv_prov_addinfo")
   
    Me.RecordSource = rs.Name
   
    Me.txSequence.ControlSource = rs.Fields(0).Name
    Me.txType.ControlSource = rs.Fields(0).Name


Notice the rs.NAME value - that seems to be the critical part. I did this in the Open event of the report.
0
 
LVL 7

Author Closing Comment

by:COACHMAN99
ID: 38837548
That is my point: If one can assign (and reference) the recordset at runtime, why not at design time? (the object obviously knows which data fields are applied to which report fields).
I have implemented another solution, so will not pursue this any further. Points awarded for effort - thanks.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…
Suggested Courses

873 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