Error 2185: You can't reference a property or method for a control unless the control has focus

I have updated a tiny Event procedure to automatically fill in values of one textbox when another has been filled in, however, I am getting an error
...
Option Compare Database

Private Sub ConsumptionPer100KMs_AfterUpdate()
    [ConsumptionPer100KMs].SetFocus
    [ConsumptionInMilesPerGallon].[Text] = [ConsumptionPer100KMs] / 282
End Sub
...
Run-Time Error 2185: You can't reference a property or method for a control unless the control has focus.

Database is available at 
https://skydrive.live.com/?cid=B712073B3513EB8E&id=B712073B3513EB8E!1552, with the name Transport Malta.

Answer
Answer

As it says in the Help file, "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." 

Change your code to

Private Sub ConsumptionPer100KMs_AfterUpdate()

     Me![ConsumptionInMilesPerGallon] = Me![ConsumptionPer100KMs] / 282
End Sub
 
If you're really concerned, you can use
 
Private Sub ConsumptionPer100KMs_AfterUpdate()
     Me![ConsumptionInMilesPerGallon].Value = Me![ConsumptionPer100KMs].Value / 282
End Sub
 
(Note that the line [ConsumptionPer100KMs].SetFocus is unnecessary, becuase the control will automatically have focus if the AfterUpdate event is firing: the AfterUpdate event only fires when data is keyed into the control.)
Doug Steele, Microsoft Access MVP
www.AccessMVP.com/djsteele

4 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 17, 2023 Views 18,760 Applies to: