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

x
?
Solved

Refer to query fields from code behind report

Posted on 2001-06-06
14
Medium Priority
?
1,049 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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
 
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 200 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

773 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