By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

Retrieving a field from the current record from a subreport plus complication

P: 29
So the real problem is that I have a field named Print (reserved word). I can't change the field name because of reasons outside my control.

The goal of this code is to be able to click on a field and have that field populate a txtbox which can then be used as search criteria.

The code below gives an error because the subreport is not open. (Coincidentally this is the reason for the clunky code being used to populate the txtbox, so if you know a better way to do that please speak up.)

Expand|Select|Wrap|Line Numbers
  1. Private Sub Print_Click()
  3.     Dim strWorkOrder As String
  4.     strWorkOrder = Reports!rptsubIPACHistory!("Print")
  6.     Forms!frmIPACHistory!txtSearchWorkOrder.SetFocus
  7.     Forms!frmIPACHistory!txtSearchWorkOrder.Text = strWorkOrder
  10. End Sub
Dec 19 '16 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 634
You do not say what the error is, but I don't think you can access a report's (or form's) properties if it is closed.
But I will stand corrected if this is the case!!??

Also, if it is open, I don't think you can refer to a SubReport or SubForm without going through (referring to) its main From/Report.

You would need something like

Not very helpful, but if true, then you will need to come up with a different scenario to achieve what you want.

With more information on Form/Report/controls/field relationships and the process required someone may be able to help further.

Dec 20 '16 #2

P: 29
Okay that will not work because Print is reserved. How do I get around this?

But the error it gives is that "the report isn't open or does not exist" but it does exist, and it's open on the form.

Any other sugestions?
Dec 21 '16 #3

Expert 100+
P: 1,107
Mike is right, the Report needs to be opened first. That is also what your error is telling you.

Here is how you go about opening a Report: DoCmd.OpenReport Method (Access)

Once it's Open, you can use some syntax like this to get the value of a Control. Note this is the Report's Control Name, not the Field Name:
Expand|Select|Wrap|Line Numbers
  1. strWorkOrder = Reports("rptsubIPACHistory").Controls("Print")
Dec 21 '16 #4

P: 29
So I have to open the report in the background and then make sure both are closed when exiting the form. Seems like a lot of work.

So here's another question.. why does this code work with the report not being open?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Req_Num_Click()
  3.     Dim strReqNum As String
  4.     strReqNum = Req_Num
  5.     Forms!frmIPACHistory!txtSearchReqNum.SetFocus
  6.     Forms!frmIPACHistory!txtSearchReqNum.Text = strReqNum
  8. End Sub
Thanks for all the help so far guys! I'm new to VBA so I really appreciate it.
Dec 21 '16 #5

P: 29
So I made it work, though I'm still not certain I understand why the code in Post #1 needs the subreport launched and the code in Post #5 does not, but I think it's because Post #5 links to the txtBox and not to the actual control source (on the subreport). Like I said, I'm not sure.

To solve my problem I simply changed the name of the text box from Print to PrintNum so that I could use the exact same format as in Post #5 and not worry about the reserved word at all. That said I still appreciate that bit of code for getting the value of a control, I'm sure that will come up in the future!

Thanks again!
Dec 21 '16 #6

Post your reply

Sign in to post your reply or Sign up for a free account.