set focus not "setting focus"

M

Mark J Kubicki

the form has multiple controls, of which (1), txtType, should be required
prior to being allowed to enter data into any of the other control

I have added this code which effectively "validates the data" (although this
may not be the best placer to do this and your suggestions are
appreciated...); however, it does NOT return the focus to the control
(txtType)
(i.e. the data in txtType is evaluated, but if it fails, the focus
remains at the subsequently selected control)

I'm (as I often am) perplexed

Private Sub txtType_LostFocus()
Dim response
If IsNull(Me.txtType) = True Or Me.txtType = "" Then
Me.txtType.SetFocus
response = MsgBox("You MUST enter Type before moving on...", vbOKOnly)
End If


thanks in advance,
mark
 
B

Brendan Reynolds

Use the Exit event procedure instead, and set the Cancel argument to True
....

Private Sub txtType_Exit(Cancel As Integer)

Dim response
If IsNull(Me.txtType) = True Or Me.txtType = "" Then
Cancel = True
response = MsgBox("You MUST enter Type before moving on...",
vbOKOnly)
End If

End Sub
 
G

Guest

Hi Mark,

If you move that code to the controls BeforeUpdate event, you have the
option of using Cancel = True, to prevent the data being saved and the user
leaving that field eg;

Private Sub txtType_BeforeUpdate(Cancel As Integer)
Dim intResponse
If IsNull(Me.txtType) = True Or Me.txtType = "" Then
intResponse = MsgBox("You MUST enter Type before moving on...",
vbOKOnly)
Cancel = -1
End If
End Sub

PS I changed response to intResponse, as response in a vba reserved word
used at procedure level coding and should be avoided in general programming
such as this.

Hope this helped,

TonyT..
 
T

tina

move your code to the control's BeforeUpdate event. also, considering that
Response is a procedure level variable, and the value assigned to it is not
used in the procedure, the variable is unnecessary. suggest the following,
as

Private Sub txtType_BeforeUpdate(Cancel As Integer)

If Len(Me!txtType & "") = 0 Then
Cancel = True
MsgBox("You MUST enter Type before moving on...")
End If

End Sub

hth
 

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