SetFocus not working.

neilhoop

Registered User.
Local time
Today, 12:38
Joined
Jun 10, 2009
Messages
45
Hi,

I have this code to validate input into a field. the problem is that the focus is not reset back to the field if there is a problem, it goes to the next field.


Code:
Private Sub FName_LostFocus()
mmsg = " is invalid. It can not contain ' and "
mmsg = mmsg & Chr(34)
mmsg = mmsg & " in the same line."
If IsNull(Me.FName) = False Then
    stpos = InStr(1, Me.FName, "'", vbTextCompare)
    stpos1 = InStr(1, Me.FName, Chr(34), vbTextCompare)
        If stpos <> 0 And stpos1 <> 0 Then
            xx = MsgBox(Me.FName & mmsg, vbCritical)
            Me.FName.SetFocus
        End If
End If
End Sub


----------
Neil
 
Don't use the Lost Focus event, use the BEFORE UPDATE event.
 
And you issue a CANCEL = TRUE if it fails validation.
 
Your part here:

If stpos <> 0 And stpos1 <> 0 Then
xx = MsgBox(Me.FName & mmsg, vbCritical)
Me.FName.SetFocus
End If


Which sends it back is accomplished with just the code line:

Cancel = True

if used in the control's BEFORE UPDATE event.
 
So I delete;

If stpos <> 0 And stpos1 <> 0 Then
xx = MsgBox(Me.FName & mmsg, vbCritical)
Me.FName.SetFocus
End If

and replace those lines with

Cancel = True

?

----------
Neil
 
So I delete;

If stpos <> 0 And stpos1 <> 0 Then
xx = MsgBox(Me.FName & mmsg, vbCritical)
Me.FName.SetFocus
End If

and replace those lines with

Cancel = True

?

----------
Neil
No, actually just the Me.FName.SetFocus line. The rest is still your validation.
 
Got it, done it, works just fine.

Thanks for your help.


----------
Neil
 
I just had a thought.

I'd like to use this over and over again on different fields.

Would I be right in saying that it could be written as a Public Function or Public Sub that can be called by passing the field name to it.

If so can you guide me to where I can find out how to do it?

----------
Neil
 
Well you'll have to modify to your use but a simple example is:

Code:
Function SamplePass(frm As Form, ctl As Control) As Boolean
   SamplePass = (IsNull(frm.Controls(ctl.Name)))
End Function

And then called by the before update event of your control:
Code:
If SamplePass(Me, txtName) = False Then
    MsgBox "You need to fill this out"
    Cancel = True
End If
 
I'm going to have a go, wish me luck !!!!

----------
Neil
 
I know it's not quite what you suggested but I came up with this.



Code:
Option Compare Database
Dim retz As Integer
Sub subChecktxt(fieldname)
Dim mmsg As String
retz = 0
mmsg = Chr(13) & "Is invalid."
mmsg = mmsg & Chr(13) & "It can not contain ' and "
mmsg = mmsg & Chr(34)
mmsg = mmsg & " in the same line."
If IsNull(fieldname) = False Then
    stpos = InStr(1, fieldname, "'", vbTextCompare)
    stpos1 = InStr(1, fieldname, Chr(34), vbTextCompare)
 
      If stpos <> 0 And stpos1 <> 0 Then
        xx = MsgBox(fieldname & mmsg, vbCritical)
        retz = 1
      End If
End If
End Sub


and is called and tested by this

Code:
Private Sub FName_BeforeUpdate(Cancel As Integer)
 
subChecktxt Me.FName
If retz = 1 Then Cancel = True
 
End Sub


I'd like to be able to lose the

If retz = 1 then Cancel = True

line but I can't get it to work.

It doesn't matter because it's a lot less code than before and I only have to change one field name to re use it.

----------
Neil
 

Users who are viewing this thread

Back
Top Bottom