Gotfocus property not working on form load

G

Guest

Hi,
I have a form with fields that change color on gotfocus using:

Private Sub txtCertNo_GotFocus()
Screen.ActiveControl.BackColor = RGB(0, 255, 255)
End Sub

This works great, except when the form is closed, then opened again. A
runtime error msg 2474 states, "The expression requires the control to be in
the active window. The debug info takes me to the 2nd line of the above code.

This field is #1 in the tab order, so the cursor is there when I end the
debugging process.

Any ideas? Thanks, in advance for your help!
Karl
 
S

Steve Schapel

Karl,

Were you aware that you have 2 other posts in this thread with the same
question?

Why don't you try it like this?...
Private Sub txtCertNo_GotFocus()
Me.txtCertNo.BackColor = RGB(0, 255, 255)
End Sub
 
G

Guest

Hi Steve,
Yeah, too much coffee, too little OCD medication...sorry about that.
Your idea did work great. I'm not sure why I used the
"Screen.ActiveControl" language from the start, except that's what a text was
using. Yours worked fine on form loading. Do you happen to know why it
wouldn't work the other way?

Thanks again!
Karl
 
S

Steve Schapel

Karl,

Not precisely. But I guess it's some sort of timing issue, i.e. at what
point does the form being opened become the active window, and at what
point does the control on the form get the focus, and at what point does
the code run, and do the answers to these questions depend on how much
memory your computer has or how long since your last coffee.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top