• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1053
  • Last Modified:

Refer to query fields from code behind report

I have a report that gets its data from a query.

I can refer to fields in the query from the report as [CustName] for exmaple.

How do I refer to fields in the query from the VBA code behind the report (for example from the OnFormat code of a section of the report)? [CustName] does not work!

Can someone please help? Thanks.
0
Annu
Asked:
Annu
  • 6
  • 4
  • 2
  • +1
1 Solution
 
kenspencerCommented:
Annu

Just use Me!CustName.

Ken
0
 
dovholukCommented:
what is the field name that uses CustName as a ControlSource? my guess is that it is not named CustName, but it's named something like Text2 or something like that. if you didn't rename the field, find the name of the field (txtCustomerName maybe?) and use that instead. By referencing the control on your report, you indirectly reference the field in the query.

clear?

dovholuk
0
 
etillotsonCommented:
The following function will return the value of the first record in the UnitPrice field in the Ten Most Expensive Products query.

Public Function GetQueryField() As Integer
Dim dbcurrent As Database
Dim rs As Recordset


Set dbcurrent = CurrentDb()
Set rs = dbcurrent.OpenRecordset _
("Ten Most Expensive Products", dbOpenDynaset)
GetQueryField = rs!UnitPrice

End Function
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dovholukCommented:
etillotson,

what???

is this related to the question or another question?

dovholuk
0
 
etillotsonCommented:
It's an example of VBA code referring to a field in a query.  I should have said you can change the name of the query and the name of the field to match yours.
0
 
AnnuAuthor Commented:
dovholuk, I am actually using the field CustBalance, but I do not need to display it on the report. I only need it for a calculation in the code.

So there is no control on the report with CustBalance as its control source.

Is there a way to get the value of CustBalance in the code without displaying it on the report (and without displaying a control source on the report and making the 'Visible' property as 'No' then using
Me!txtCustBalance).
0
 
kenspencerCommented:
Annu,

My testing shows that even though a report is based on a query, if the field in question is not referenced on a control on that report, you cannot refer to it in that report's code in the manner shown previously.  Adding a control and making the Visible property to No seems simplest to me.

Ken
0
 
dovholukCommented:
ok, so here's my next shot! :)

if you are looking to get the value from the query behind the report, dim a recordset and set the recordset to the me.recordsetclone method of the report. such as:

dim rs as recordset

set rs = me.recordsetclone
rs.bookemark = me.bookmark

msgbox rs!CustBalance

does this help you?

dovholuk
0
 
etillotsonCommented:


dovholuk,


Hmmm,  looks very similar to the code that had you wondering if it even related to this question.
0
 
dovholukCommented:
au contraire mon ami,

the code which i pondered over :

1  creates an entire new query as opposed to duplicating the report's recordset
2  doesn't move the recordset to the current record
3  executes an entire function on top of the On Format event
4  assumes the unit price is an integer

i don't see how the two are similar. yes, a recordset was used, but that's it... i'm also using access too.

sorry to have confused you.

dovholuk
0
 
AnnuAuthor Commented:
hi dovholuk

Could you please tell me what the code

rs.bookemark = me.bookmark

is for?

Thanks.
0
 
dovholukCommented:
annu,

first, let me apologize for the code i wrote above... evidently reports don't have the .recordsetclone OR the .bookmark property which forms have. i'd never used these methods and it was just a shot. i just tested it though, and the methods won't work.

having said that, the .bookmark property is used on FORMS to copy the source recordset's (the data the form runs off) position in the data that is being used. make sense? the bookmark is simply the position in the recordset. in a form, you need to move the bookmark otherwise you'll ALWAYS get the first record that is in the recordset. so if you don't move the bookmark, you won't get the right data.

anyway. my guess didn't work (mainly cause i didn't test it) i assumed that the report object had the .recordsetclone method... now i'll have to revisit how access uses the data to fill out a report...

after trying the method i posted, i would now reccommend you hide a text box (as you mentioned in a follow up question)

if i find a better way, i'll let you know. hopefully someone else will find "the silver bullet" for you.

dovholuk
0
 
etillotsonCommented:
The function that I wrote earlier declares a query in code as a recordset.  From there, you can call whatever fields you like.  Why doesn't this work for you?  What exactly are you trying to accomplish behind your report?
0
 
dovholukCommented:
annu is trying to use the report's recordset that is already declared and open without the need of binding and hiding a control on his report.

using a recordset might end up being the only way to do this, however it's certainly not the best way. every time a new section is formatted, the report will need to declare a recordset, run a query, set the recordset, and return the result. it'd be much faster to avoid all of that overhead, and simply bind a control to the field he wants in the report's recordset and then set the visible property of that control to false.

annu's answered the question by him/herself.

annu, i've looked all over access help and i don't think you can do what you're looking for. i've tried referencing the field in multiple ways, none of which worked. binding the control and hiding it will definatlely be your best option.

dovholuk
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now