Problem with Report Not Recognising Field (1 Viewer)

WelshJohn

New member
Local time
Today, 11:01
Joined
Jan 16, 2013
Messages
9
Hi,
I've been having this problem for a while now so I really had to seek help for it.

Basically I have a Report based on a query. The query has about 40 fields across 2 tables. The query has a criteria limitation based on the Job No on one of the tables.
Everything else works fine. The Report displays the correct data, and the field I want is displayed fine on there.

However, when I open the Report Headers Print event, and simply print out the field name. I get a runtime error '2465' Microsoft Access Cannot Find the field referred to in your expression.

Code:
Private Sub ReportHeader_Print(Cancel As Integer, PrintCount As Integer)

    Debug.Print Me.UkasJob
    
End Sub
I really hope someone can help out, pulling my hair out with this. It works if I try to print out numerous of the other variables. I'm hoping its not corrupt because a while back I was getting a memory error and I had to rebuild.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:01
Joined
Feb 19, 2002
Messages
43,273
Welcome aboard:)
This is a case of Access thinking it is smarter than we are. For reports, Access rewrites your RecordSource query and includes only columns that are bound to controls. It doesn't take into consideration that you might actually use the field in code but not need to display it. The only solution is to create a bound control for the UkasJob field. You can set its visible property to No so it doesn't show and you can also remove it's caption and make it very small. I make the background of hidden controls some bright color like yellow so I can see them easily in design view and know they don't print.
 

WelshJohn

New member
Local time
Today, 11:01
Joined
Jan 16, 2013
Messages
9
Right, I just got home and tried that and It didnt work. I made a bound text box to UkasJob, and then tried in the report header print sub to print Me.UkasJob. It didnt work.

Thought we had it aswell then. Any other ideas? Any way I can trick it?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:01
Joined
Feb 19, 2002
Messages
43,273
When you typed the Me. did you get intellisense? Did it include UkasJob in the list?

When the Access wizard builds forms and reports, it uses the bound column name as the Name property of the control. When you drag a control from the toolbox (or ribbon) and bind it later, Access names it text1234 or something like that depending on the type of control. Most experienced programmers rename all the Name properties to include a prefix that indicates the control type so you would see Me.txtUkasJob or Me.cboUkasJob, etc. The reason for this is to disambiguate your code. When you refer to Me.UkasJob, Access knows you are referring to the bound column from the form's RecordSource whereas when you refer to Me.txtUkasJob, Access knows you are referring to the control whose Name property is txtUkasJob. In earlier versions of Access, you would only see certain control properties in the intellisense list if you referenced the Name property of the control. If you referenced the column name, you would not get intellisence on control properties.
 

WelshJohn

New member
Local time
Today, 11:01
Joined
Jan 16, 2013
Messages
9
Yes, intellisense links to the field when I type Me. And when linking the control source on the report, I type Uk it picks up on the field from the query. I have successfully linked about 30 other fields on the report in a similar way.

When I type Me.JobNo it works like expected. I'm starting to think that the report is corrupt somehow? I tried repair and compact and it still didn't work. Also tried on access 2003 and 2007.

Thanks for the help.
 

WelshJohn

New member
Local time
Today, 11:01
Joined
Jan 16, 2013
Messages
9
Pat, I just got it working. You were right about the Report needing a the field to be bound somewhere. What I was doing wrong was I was putting a the bound field in the Report header section, as it was the ReportHeader_Print sub where I needed the field. As soon as I bound the field in the Detail section of the Report it worked, even though that's not where I was using it.

Thanks for your persistence, we got there eventually!
 

Users who are viewing this thread

Top Bottom