Solved

Refer to query fields from code behind report

Posted on 2001-06-06
14
999 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now