setfocus in MS Access (VBA)

I am finding it quite difficult to do a simple task in VBA within Access. Here is the code I am trying:

Me.ChangeFlag.Visible = False
Me.histdate.setfocus

The error I get on the first line is "you can't hide a control that has the focus". So, when I switch the lines around and try this:
Me.histdate.setfocus
Me.ChangeFlag.Visible = False

I get "Access can't move the focus to the control HistDate". HistDate is a text box. Any ideas?

Glenn
tkecowboyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
snavebelacConnect With a Mentor Commented:
I have created a simple form here and use dthe code an it works just fine.  I did manage to generate the same error by trying to move the focus to an option button.

Can you check the properties of the texbox (if you have not done so already) to make sure there is nothing strange going on there.

This seems a little odd to me...

C
0
 
snavebelacCommented:
Is Histdate disabled or locked ?  That would explain why the focus cannot move to it.

In your event to hide your first control, if you can move the focus to something else first - an enabled and unloacked control, it should work.

HTH

C
0
 
tkecowboyAuthor Commented:
well, I changed the code to look like this:

me.histdate.enabled = true
me.histdate.locked = false
Me.histdate.setfocus
Me.ChangeFlag.Visible = False

and I still get the error. In fact, I can't set the focus to any control on the form, which leads me to believe that there is sometihng in particular I must do within Access before setting focus on a control.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
snavebelacCommented:
Are the controls part of a locked or disabled tab control or options group ?

The code you have should work just fine.  Is HistDate definitely a textbox ?

C
0
 
tkecowboyAuthor Commented:
The controls look like they are on a standard form. I inherited this app, but I'm pretty sure there's nothing wierd going on. histdate is definitely a textbox.
0
 
shanesuebsahakarnConnect With a Mentor Commented:
Are you absolutely sure that histdate is the name of the *textbox* and not the field within the textbox? Check the textbox's Name property in the properties window.
0
 
Mr_AhmadConnect With a Mentor Commented:

Hi

Make sure the initial properties on the controls are set to visible = true, locked = false, the code will work afte that, else it will give you that error message.

Reg
Zaf
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.