set focus not "setting focus"

  • Thread starter Thread starter Mark J Kubicki
  • Start date Start date
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
 
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
 
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..
 
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
 
Back
Top