How do I refer to a report's controls explicitly? Referring implicitly works as expected:
Me.lblTest.Visible = True
However, all of my attempts at referencing the control explicitly meet with various nonsensical runtime errors. Access' favorite:
"The report name 'rptTest' you entered is misspelled (it's not) or refers to a report that isn't open (it is) or doesn't exist (it does)."
I've tried a dozen syntax variations from MSDN and everywhere else I could find, all producing errors:
Reports!rptTest.lblTest.Visible = True
' ^--expected syntax, like Forms!frmName.ctlName.Property
Reports!("rptTest").lblTest.Visible = True
Reports![rptTest].lblTest.Visible = True
Reports("rptTest").Controls("lblTest").Visible = True
etc. etc.
This should be no different from referring to forms' controls, right? There must be a way to refer explicitly to a report's controls.
Check the names of the reports Access thinks are open.
With your report open, go to the Immediate window. Ctrl+g will take you there.
Paste in for each rpt in reports : ? rpt.name : next as one line and press Enter. Access will tell you the names of the open reports as it sees them.
Here is an Immediate window example from my system with one report, rptFoo, open ...
for each rpt in reports : ? rpt.name : next
rptFoo
The problem you're facing is that rptTest is open, but Access disagrees. That discrepancy can be caused at least 3 ways:
rptTest is what you see when the report is open, but that is the report's Caption property instead of its Name
rptTest is a subreport contained in some other open report. The Reports collection, which is where Access looks to find open reports, contains only the "top-level" reports which are open. If you're dealing with a subreport, reference it via its parent report ... Reports!rptParent!SubreportControl!lblTest.Visible where SubreportControl is the name of the subreport control which contains rptTest. Be careful because the name of that subreport control may not be rptTest --- you need the name of the subreport control, not the name of the report it contains.
If rptTest is contained in a subform/subreport control within a form (instead of within a report), the situation is similar to #2, but you must reference it via its parent form ... Forms!YourFormName!SubcontrolName!lblTest.Visible
Related
I have a MS access data base which has a form from which I want to open a report. There is a choice of 26 reports which all have different name which is a numeric value eg 221.1, 221.2 113.3, etc etc. I have an unbound field in the form which adds the values of four other bound fields and returns a number which should match one of the report names. I have called this unbound field "report Name". I want to add a button that is pressed which opens a report that has the same name as the value in the "report name" field so that if the value is 221.1 it will open report named 221.1 and so on...
I am not used to coding in access and generally rely on the built in commands, macros, wizards etc but in this case I don't think there is a suitable "wizard" to use. Any ideas or help on how I can do this would be gratefully received. I am working in Access 2013 but the data base was written in 2007 and transferred over.
You can get away with just a tiny bit of code in your button's click handler:
Private Sub MyButton_Click()
DoCmd.OpenReport Me![report Name]
End Sub
Add the code just like adding an embedded macro, but by choosing Code Builder instead of Macro Builder. The first and last line should already be there.
Until now, I manually created a finite number (12) of controls on a report and my code, essentially, turned them on or off (successfully) based on how many the user chose.
Now my client wishes the user to have "no limit" to the number of controls on the report.
My strategy is to delete the controls and create the required number on the fly (via code).
However, when I try to create a control, I get the run-time error 2450 "cannot find the referenced form".
Snippet:
DoCmd.OpenReport "My_report", acViewDesign
Dim ctl As Control
Set ctl = CreateControl(FormName:="My_report", ControlType:=acTextBox, _
Section:=acDetail, left:=2880, top:=0, width:=967, height:=312)
' ... here lies code that adjusts attributes of the controls
DoCmd.OpenReport "My_report", acViewReport
Execution halts on the CreateControl line: ... cannot find the referenced form 'My_report'. (When working, the actual CreateControl code will be in a loop and geometry parameters replaced by variables.)
All discussion I've seen relates to forms but the documentation at CreateControl Method says the first parameter is the "name of the open form or report on which you want to create the control" so it should work for reports too.
Note that the report is open in design mode. Any idea what I'm doing wrong?
Online help / documentation is unfortunately wrong or at least misleading sometimes.
There is a separate method CreateReportControl() for this.
I have an Access report. The problem is that it always display an empty row at the end. In the query there are no empty values however the report always shows an empty row. Any ideas?
https://drive.google.com/file/d/0B5XFoGik1WSWUmRkRktla2VIYnM/view?usp=sharing
https://drive.google.com/file/d/0B5XFoGik1WSWcFF3SnZpekVwTlU/view?usp=sharing
On a form I would set "Allow additions" to false. However I don't know what I would do on a report.
A report has an OnNoData event that you could use to cancel the opening of the report. It is often used to popup a message with like "The report, with the specified parameters, would return no data." The code would popup the message, then run a DoCmd.Cancel, or Me.Close acReport, Me.Name.
You can also base the report on a query that has the AllowUpdates set to false (in the QBE, set the properties). Another idea would be to render the report in Preview Mode.
In Access 2007 (or 2010), I need to set the properties on a subform, embedded within the first subform, from the main form using VBA. To make it clear which form I'm talking about, I'll call the this form "subSubform" as I do in my code (well, subSubform is actually the name of the sub-subform control).
When the form loads I have a subform control on my first subform that's blank (does not contain a form source object). If needed, I fill out the SourceObject property of this control with the name of a valid form, as well as the LinkMasterFields and LinkChildFields properties. This all appears to work fine because I do get an expandable plus sign that otherwise wouldn't be there.
Next I try to set the RecordSource for the subSubform and I get an error:
2455 You entered an expression that has an invalid reference to the property Form/Report.
Here's the code I'm using:
Me!subform1.Form!subSubform.Form.RecordSource = sSubformSQL
'and I've tried this with the same bad results
Me.subform1.Form.subSubform.Form.RecordSource = sSubformSQL
'and this too
Forms("frmQuery").subform1.Form.subSubform!Form.RecordSource = sSubformSQL
'And I've tried this. It fails too, on the last line with the same error:
Dim frm As Form
Set frm = Forms("frmQuery").subform1.Form
Dim frm2 As Form
Set frm2 = frm.subSubform.Form
I've tried setting a timer and waiting one second to run the above code, just in case it takes some time for the form to load. But this makes no difference.
I've tried running code from the first subform instead of from the main form, but that doesn't help either. Still fails with the same error.
FWIW, both my subform and my subSubform are DataSheet views. I don't think that makes any different on what I'm trying to do here (although I'm well aware of the performance problems involved so don't scream at me).
I recognize that it's probably a very odd request. But I'm creating a dynamic query interface that needs this.
Any ideas how to set form properties on a subSubform?
The problem is not that you setting properites of a sub form. The format of:
me.subform1.form.subform2.form.RecordSource = sql
s+Should work just fine.
The problem is you are trying to nest a continues form (or datasheet) in side of a continues for (or a datasheet). This is NOT permitted.
The workaround is to simply place the two sub forms side by side and don’t' nest.
You can still get the second (child child) form to follow by using the link master settings.
In the link child/master settings for child 1, you place:
linkChildFields main_id (whatever is the name of the field in
this sub-form that is used to relate back to the parent table)
LinkMasterFields [ID]
In the link child/master settings for child 2 form you place
linkChildFields main_id (whatever is the name of the field in
this sub-form that is used to relate back to the parent table)
LinkMasterFields [child1].[form].[ID] ("masterForm" is the name of
the control you used to hold the master form.
So you can model a many to many, but you cannot nest, but as noted the above gives you the same result anyway. So a form will look like this one:
You also likly in the on-current event of the left side (child 1), will want to place a requery of the right side when you navagate - as it often will not update automatic for you.
So:
me.parent.Child2.Requery
Generally the Form property of a subform control gets a "valid reference" only after the subform is shown. Thus, if you want to execute a subSubform.Form access as you do in
Me.subform1.Form.subSubform.Form.RecordSource = ...
you have to make sure that subSubform is visible before the access is executed. The easiest way to do it is to let the subSubform set it's source on his own while it loads (in it's Form_Load sub).
For further details and explanations see my answer in Microsoft Access runtime error 2455 when trying to access grandchildren forms from child form where the problem is the same.
I have to build an MS Access form that acts as a registration form for delegates of a conference. I have to print out a receipt acknowledging the delegate's details and payment and other details. I think I have to make a report for this. So I made a rough report containing the required fields and placed a button on the form that prints the report out. When I clicked on it for the first record, it displayed the details faithfully. However, when I navigated to the second record and did the same, it displayed the details of the first record again. What do I do? Also, how can I customise it to mimic a receipt's format? Like so:
"Received with thanks from Mr./Ms. , a sum of Rs." and so on and so forth?
You've probably attached the record source for the Report to the same table/query that you are using for the Form. When you open the Report, you'll actually have receipts for each record in the source table/query.
What I would do in this case, is add a filter to the Report, and filter the records to show the same record as is currently displayed on the form. You can do this when you open the Report. e.g.,
docmd.OpenReport "foo", acViewNormal, , "fooID = " & me.fooID
(note: you may have explicitly turn on filters in the report's parameters)
You can display a customised line like that by building up a string in the default valude for an unbound text box.
="Text" & "foo" & me.value & ""
Or you can set the text box's value on the fly, probably during the OnFormat event.
Or you can do that as part of the query you use to provided data to the report, and use a bound text box.