SetFocus on a Text Box ...error

KingBroil

Registered User.
Local time
Today, 15:29
Joined
Oct 3, 2012
Messages
41
Hi,

On my form, I have a button that when clicked set a Text box visible and moves the focus to it. Here's the code:

Private Sub btnAddLocation_Click()

Me.txtGeoName.Visible = True
Me.txtGeoName.SetFocus

End Sub

I get this error:

Runtime error 2110, Access cannot move the focus to txtGeoName

I have tried to use the macro builder too, but similar error.. I have red many threads on this. I red something about the text box might not exist before any action is taken on it, or something like that. Nothing seems to do the trick.

What is it with Text boxes and SetFocus?

KingBroil
 
The field Enabled property is set to "No".
 
Thanks for the reply JHB,

The Enabled property is actually set to "Yes". Just to be sure that it really is when I use my form, I tried in the "on load" form event:

Me.txtGeoLoc.Enabled = True

What it did is turning the text box visible on form load, and I still get the exact same error when I push the button.
I tried setting txtGeoLoc Visible to "Yes" and still...:banghead:

So, visible or not, and Enabled or not doesn't change a thing.

Frustrating for a task that I think should be so simple. Any thoughts?

KingBroil
 
Hmm - one time you call the control Me.txtGeoLoc, another time you call the control Me.txtGeoName. Are you sure you are looking on the right control? :)
If I use the code below and not set the fields Enabled to "yes" I get the same error as you!
Code:
  Me.txtGeoName.Visible = True
  Me.txtGeoName.SetFocus
 
Your right, I misstyped the text box in my previous post, it should have been:

Me.txtGeoName

But, as I wrote, my Enabled property is set to "Yes" on [txtGeoName]

I need to mention that I am very new to VBA and Access, and I have to admit, I am having a hard time. A lot of this: :banghead:
But I am very stubborn so...

I did some other tests. Here's what I found: Until now, the text box that I want to move the focus to had an On Got Focus event: Go to New Record.

If I remove this event, my SetFocus is working!
But I really need this Text Box to go to New Record since it's purpose is to add a new record to a table.

Is there a work around for this?

KB
 
People writing programs are very stubborn, else ... :D
Go to a New Record, before you set the focus to the control.
Code:
  DoCmd.GoToRecord , , acNewRec
  Me.txtGeoName.Visible = True
  Me.txtGeoName.SetFocus
 
Ok, got it!

I do not really know why, but here's the working code:

Private Sub btnAddLocation_Click()

Me.txtGeoName.Visible = True
Me.txtGeoName.SetFocus
DoCmd.GoToRecord , , acNewRec

End Sub

Instead of setting the GoToRecord function in the On Get Focus event of the text box [txtGeoName], I put it in the On Click event of the button, as you see in the code above.

I feel so good right now!! :cool:

I still wonder what exactly went wrong though?

Thanks JHB for your support

KB


 

Users who are viewing this thread

Back
Top Bottom