I am creating a personal Library Inventory system using an Access 2007 database. In code, whenever I reference the .Text property of a form control, whether it be changing the value, or simply checking the value in an IF statement, I get prompted with Run-time error '2185': You can't reference a property or method for a control unless the control has the focus.
Why is this?
For setting the .Text it's not a huge deal, but when I'm checking the value in an IF statement, I can't set the focus when I'm checking multiple conditions.
Use .Value instead - that doesn't require setting focus first. From the documentation, for example for the TextBox control (emphasis mine):
While the control has the focus, the Text property contains the text
data currently in the control; the Value property contains the last
saved data for the control. When you move the focus to another
control, the control's data is updated, and the Value property is set
to this new value. The Text property setting is then unavailable until
the control gets the focus again.
Related
All I want to do is set a textbox or label's text to something dynamic when the report is opened with a click of a button in another form. I've solved everything except actually changing the text.
This code gives run-time error 2478 on SetFocus:
Me.tFilial.SetFocus
Me.tFilial.Text = filialen
Without SetFocus I get a run-time error saying the text can't be changed without switching control to the control in question.
What is allowed where is always in question in Access, it seems. How do I solve this? Can I set the value on the buttonclick in the other form with
Reports![rptPressSchema]![tFilial].text="Hello"?
I would be happy to use a label instead, if that solves it. But the bottom line is I can try to do this every which way but I thought I'd ask you for advice as to best practice, as this must be a very common task indeed.
From the Access help:
While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again. If you use the Save Record command on the Records menu to save the data in the control without moving the focus, the Text property and Value property settings will be the same.
Basically, the .Text property serves no purpose in a Report because individual controls cannot receive the focus. However, as #Remou stated in his comment, you can simply replace .Text with .Value and your code should work fine (no need to set focus when updating the value).
spent a lot of time for searching and trying. Finally figure the things out...
To dynamically set the TextBox content, it is convenient to use
tbTest.Value = "hello"
but the trick is if you are using this On Open, it will be in trouble...
Run-time error '2448'
You can't assign a value to this object.
So you need set the value On Load
Private Sub Report_Load()
Me.tbTest.Value = "hello"
End Sub
Private Sub Report_Open(Cancel As Integer)
'Me.tbTest.Value = "hello"
End Sub
I see nowhere for any explanation of this, my guess is for Open event, the object is still not initiated (document for explaining Load and Open event)...
I think I'm looking directly at an Access 2003 bug. Here's what I did (I think):
I'm developing a Continuous Form (i.e. multiple records per page).
I have an unbound textbox in the background of the Details section. Under Data Properties, I have Enabled=No and Locked=Yes. (In other words, I don't want the user to select it. I only want it for the background color)
This textbox also has some conditional formatting to change the background color for that row. (e.g. If we haven't spoken to that customer in awhile, the background for that record is red)
If I choose a record where the conditional formatting is active (i.e. the background is red), I can actually click on this textbox. There's not much that I can do, of course, but this shouldn't happen and surely would be confusing to the end-users.
Has anyone else seen this? What's your workaround?
Enabled is a property that can be controlled via conditional formatting. By default when you add a new condition, the Enabled property is set to True. If you don't want your control to be enabled you must explicitly set Enabled = False within the conditional formatting rule.
The property is toggled via a button in the lower right corner of the conditional formatting dialog. It's easy to miss (I forget about it all the time).
Personally, I think the property should be three-way: True, False, or Inherit (with Inherit as the default). Unfortunately, Access forces you to be explicit when adding conditional formatting rules and state whether the rule should make the control Enabled or Disabled.
Here's my workaround (I was waiting to see if anyone else would suggest something better. Apparently not.)
In my conditionally formatted textbox, add an Event Procedure for GotFocus. Enter this code: Me.Txt_FocusHere.SetFocus.
Make a 2nd unbound text box somewhere on the form. Shrink it until it's just 1 line thick. Call it Txt_FocusHere.
So whenever the user clicks on the colored background, the focus is immediately taken to this dummy textbox which can't store any data and is so small that the user doesn't see the blinking cursor.
I successfully used the method on the following page: http://www.mvps.org/access/forms/frm0047.htm - "Changing the Background Color of the Current Record in a Continuous Form"
Working in both A2003 & A2007.
How do we ensure that a selected TextBox gets the focus when the form loads? If we put MyTextBox.SetFocus in the Form_Load then we get the error:
can't move the focus to the control
This form is designed for rapid data entry, and the form somewhat rearranges itself based on the last used settings. So there are several different textboxes any of which may need the focus depending on the user. We can't just fix it in design time by giving MyTextBox TabIndex=0.
The help says something about calling Repaint which just doesn't make any sense at all:
You can move the focus only to a
visible control or form. A form and
controls on a form aren't visible
until the form's Load event has
finished. Therefore, if you use the
SetFocus method in a form's Load event
to move the focus to that form, you
must use the Repaint method before the
SetFocus method.
The best bet in this case, is to ensure that the textbox to get focus is numbered 0 in the Tab Index property.
You cant set the focus as the controls don’t really exist yet, try putting the code in the OnActivate event instead
Or just put a DoCmd.Repaint in the OnLoad event before trying to set the focus. Both should work but I'm not near a computer to check
In my experience, I've always gotten that error when the control I was trying to set focus to was either 1)not visible or 2)not enabled. I assume you've already checked those, but it would be worth double checking at runtime when you get the error message (especially since you said you are shuffling the controls at runtime).
I use the .SetFocus method pretty regularly without trouble. I don't recall ever getting an error message when setting focus to a control that already has it as Remou stated in his answer.
I believe there is also a third case that occurs if you try to set focus to a control in the form header/footer of a bound form that has had all of its records filtered out. I know that situation causes "disappearing" contents in an unbound combo box, but I think it may also play havoc with the SetFocus method. If you are opening the form in Data Entry mode, though, that should not be an issue.
Move SetFocus to the form's On Current event. Should work then unless perhaps the form's record source contains no records and you've set the form's Allow Additions property to No. In that case your text box will not be available to SetFocus on, but in my testing it doesn't throw an error.
For instance, I have a query which takes as a parameter the text property of a drop-down box [DDB1] of Form1.
How do I reference the property?
My stumbling around google has suggested that it should be something along the lines of
Forms![Form1]![DDB1].text
But I have been unable to find a definitive syntax for such references.
Any ideas?
Semi- OT - the Access2003 help, which contains links to a lot of Microsoft web pages, returns a lot of 404s when trying to load them. Is this just coincidence? Or end-of-lifing by stealth?'
To have a query reference a combo box on a form, use this syntax:
Forms!MyForm!MyComboBox
This will retrieve the selected value property of the combo box (the value of the first column, if it is a multi-column combo box).
If you want the selected value of a different column in the combo box, it is:
Forms!MyForm!MyComboBox.column(n)
Unlike most numeric indexes in VBA, n is zero based (the second column is 1).
To reference the text property, the combo box must have the focus.
The help file is apparently suffering from link rot. Here are some links in MSDN to use:
Access 2003 Programming Reference
http://msdn.microsoft.com/en-us/library/aa167788(office.11).aspx
Access 2003 Language Reference
http://msdn.microsoft.com/en-us/library/aa663079(office.11).aspx
You have in fact multiple ways to refer to a control with MS-Access. In addition to #Robert Harvey proposal, you can also write:
forms(myFormName).controls(myControlName)
Despite what #Robert says, you can access most properties of a control without setting the focus on it. One important exception is the ".text" property, which refer to the text appearing in the control, and where the focus must be set to the corresponding control.
Most of the time, the .text property is equal to the .value property, that can be accessed without setting focus on the control.
Thus, this property is useful only for controls of the combobox or listbox type, where the bound column (that gives the .value property) differs from the displayed column (giving the .text property).
I have multiple combo and option boxes whose visibility is set true or false depending on user selection of other parent option boxes.
I am using the AfterUpdate() function for the parent option boxes. This method has been working fine.
However when I set the values of the option boxes in VBA code (Me.MyOptionBox = 1), there is no "update" to make the child option and combo boxes appear. It works when I manually click on the option, but if I set it in the code nothing changes except for the option box selection. It doesn't make sense to see the option box set correctly but the code responsible for checking which option is selected not work.
Does anyone have any ideas on how to get around this? I just want the form to be updated once I set the values of the option boxes. What is the "update" that AfterUpdate() is referring to anyway?
I have tried MyForm.Repaint and MyForm.Requery but these do not work either.
Any ideas?
You can call the OptionBox_AfterUpdate() method directly - after you set the values.
I'm pretty sure this is intentional. The problem is avoiding circular recursive updates, when you consider that a control can be changed by a user and by two forms of code (explicit and bound data).
For bound controls, changes in the data change the controls.
For user input, changes in the control change the data, plus whatever dependendencies you explicitly specify in events.
For changes caused by your code, the assumption is that you need to manually apply all the consequences of the state change you are coding.
If you are using bound controls, you can change the datasource and have the consequences applied automatically.
In VBA, most control events do not fire when the control is updated programmatically, rather than manually. You must specify the code that is to run after the programmatic update.
By "I am using the AfterUpdate() function," you mean you added an event handler for the AfterUpdate event, right?
If that handler is not firing when you expect it to, you could always call your handler directly (instead of letting the event fire it) where necessary. But that probably wouldn't be the best way of doing it; more likely there is another event that should be calling your handler instead of or in addition to AfterUpdate. I don't remember the names of combo box events, but isn't there something like ItemChanged or SelectionChanged or something?