Clear a field - Set focus

  • Thread starter Thread starter Richo
  • Start date Start date
R

Richo

I have not used Access for a few years and first time on 2007, however my
issue is that when I have saved some text fields on a screen I want to clear
the fields. If I use the below code

Me.num_meal_number.SetFocus
Me.num_meal_number.Text = " "
Me.txt_meal_name.SetFocus
Me.txt_meal_name.Text = ""

Or from a module

Forms!frm_create_meal!num_meal_number.SetFocus
Forms!frm_create_meal!num_meal_number.Text = " "
Forms!frm_create_meal!txt_meal_name.SetFocus
Forms!frm_create_meal!txt_meal_name.Text = " "

I get an error # 2110 saying that can not move the focus to the first control.
Must be a simple answer to this........
 
No need to SetFocus. Just set the Value of the control rather than its Text.
(VBA in Access is different from pure VB, since the controls are
data-centric.)

Also, you want to set the controls to Null, not to a zero-length string.
There's a significant difference.

So:
Me.num_meal_number = Null
Me.txt_meal_name.Text = Null

If this is a bound form, perhaps you are wanting to leave this record the
way it is, and go to a new record? If so, use:
If Me.Dirty Then Me.Dirty = False
If Not Me.NewRecord Then RunCommand acCmdRecordsGotoNew
 
Thanks for that, it works.
--
Regards

Richo


Allen Browne said:
No need to SetFocus. Just set the Value of the control rather than its Text.
(VBA in Access is different from pure VB, since the controls are
data-centric.)

Also, you want to set the controls to Null, not to a zero-length string.
There's a significant difference.

So:
Me.num_meal_number = Null
Me.txt_meal_name.Text = Null

If this is a bound form, perhaps you are wanting to leave this record the
way it is, and go to a new record? If so, use:
If Me.Dirty Then Me.Dirty = False
If Not Me.NewRecord Then RunCommand acCmdRecordsGotoNew
 
Back
Top