Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA - Problems setting the focus back to the active control 1

Status
Not open for further replies.

Custom24

Programmer
Joined
Nov 27, 2001
Messages
591
Location
GB
Hello
I have a form in Access 97 and one of the events I have handled is the form_current event (when the current record changes)

One thing I did was to check the value of some bound text boxes to do some stuff, and for this I had to set the focus to each text box before I could check its value (or so Access said, anyway)

I had intended to reset the focus back to what it was before I did this using this procedure.

Private Sub Form_Current()
Dim ctl As Control
set ctl = Me.ActiveControl
...
'do stuff which means moving the focus
...
'reset the focus
ctl.SetFocus
End Sub

The problem I have is that Access complains that

"Run Time Error 2474
The expression you entered requires the control to be in the active window"

on the line "set ctl = me.ActiveControl"

Why is it having a problem with this. I could understand this happening when the form is opened, and I can just ignore the error with on error resume next, but it happens every time I move the current record.

Any ideas?
Thanks
Mark Mark [openup]
 
Hi Mark,

You wouldn't happen to be in the code module, perhaps stepping through the code, when this error comes up? This would create this error because obviously the form with the control you are trying to set the variable ctl to would not have focus, the code module would have focus. If you have a watch set before this code runs delete it and then try it. Regards,
gkprogrammer
 
Nope - no watches, no stepping thru. It happens when the form is being used normally.
Thanks anyway
Mark Mark [openup]
 
Hi!

I assume your are doing something like this:

Call Me!FirstTextBox.SetFocus
Me!FirstTextBox.Text = "MyValue"
Call Me!SecondTextBox.SetFocus
etc.

Change this to:
Me!FirstTextBox.Value = "MyValue"
Me!SecondTextBox.Value = "NextValue"
etc.

Using the .Value, you will not need to set focus first.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Jeff - thank you - that worked a treat!

Still puzzled that I could not set a variable to the active control, but I don't need to now.
Mark Mark [openup]
 
Hi!

I forgot to explain the problem there. The control doesn't technically get the focus until after the form's current event. According to Access help the event order is:

BeforeUpdate (form) Þ AfterUpdate (form) Þ Exit (control) Þ LostFocus (control) Þ Current (form) Þ Enter (control) Þ GotFocus (control) Þ BeforeUpdate (control) Þ AfterUpdate (control) etc.

hth



Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top