setFocus, can it change?

G

Guest

i am using the VBA coding to enable a command button to clear all text boxes
when pressed... i am using setFocus to select a txtbox, and then setting the
text to "", but i get an error when it tries to setFocus to the second
textbox, when focus was already set to the first...

is there a way to move focus, remove focus, or change focus from one txtbox
to another?
 
R

Rick B

Why are you doing all that?

Just clear the items.


On click...
Me.SomeField=""
Me.SomeOtherField=""
Me.SomeOtherField2=""
..
..
..
end sub


Does not matter where your cursor is.
 
B

Bill Edwards

If you just want to set all textboxes to an empty string the following will
work (tested with Access 2000)

Private Sub cmdClear_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
ctl.Value = ""
End If
Next ctl
End Sub

or if you want to set the value to NULL:

Private Sub cmdClear_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
ctl.Value = NULL
End If
Next ctl
End Sub

If for some reason you want to set focus to a particular text box the
following does this:

Me.ControlName.Setfocus

or sticking this in the For each loop:

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
ctl.Setfocus
' do stuff to this control
End If
Next ctl
 
M

Marshall Barton

justin_vasko said:
i am using the VBA coding to enable a command button to clear all text boxes
when pressed... i am using setFocus to select a txtbox, and then setting the
text to "", but i get an error when it tries to setFocus to the second
textbox, when focus was already set to the first...

is there a way to move focus, remove focus, or change focus from one txtbox
to another?


You should post a Copy/Paste of your code when you have a
coding error question. Without that, all I can do iguess
what you might be doing wrong.

First, you should be setting the text boxes Value property,
not their Text property, so you would not have to mess with
setting the focus.

Another possible source of an error is that you might be
trying to set the focus to a control that does not accept
the focus. Generally, you need to check if you should be
(re)setting the control's value, but without seeing your
code, I can't say for sure whether this is a real problem or
just a bad guess.
 
G

Guest

thanks to all for your help, but right after i posted, i decided to try my
hand at a macro, and everything works, but thank you again for your tips,
they have helped me understand what my goal was!
 

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