Solved

Refer to query fields from code behind report

Posted on 2001-06-06
14
991 Views
Last Modified: 2012-06-21
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
Comment
Question by:Annu
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 3

Expert Comment

by:kenspencer
ID: 6159331
Annu

Just use Me!CustName.

Ken
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6159353
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
 

Expert Comment

by:etillotson
ID: 6159484
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
 
LVL 8

Expert Comment

by:dovholuk
ID: 6159668
etillotson,

what???

is this related to the question or another question?

dovholuk
0
 

Expert Comment

by:etillotson
ID: 6160032
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
 

Author Comment

by:Annu
ID: 6162576
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
 
LVL 3

Expert Comment

by:kenspencer
ID: 6163383
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:dovholuk
ID: 6163562
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
 

Expert Comment

by:etillotson
ID: 6164252


dovholuk,


Hmmm,  looks very similar to the code that had you wondering if it even related to this question.
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6164371
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
 

Author Comment

by:Annu
ID: 6166521
hi dovholuk

Could you please tell me what the code

rs.bookemark = me.bookmark

is for?

Thanks.
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6167203
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
 

Expert Comment

by:etillotson
ID: 6167419
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
 
LVL 8

Accepted Solution

by:
dovholuk earned 50 total points
ID: 6167517
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
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…
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…

706 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

14 Experts available now in Live!

Get 1:1 Help Now