Error Message Handling (1 Viewer)

vbaInet

AWF VIP
Local time
Today, 19:53
Joined
Jan 22, 2010
Messages
26,374
Code:
Private Sub PatientGender_LostFocus()
    If Not Me.NewRecord Then
        If Len(Nz(Me!PatientGender[COLOR=Red][B].Text[/B][/COLOR], "")) = 0 Then
            Me.PatientGender.SetFocus
            MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
        End If
    End If
  
End Sub
 

Paul Cooke

Registered User.
Local time
Today, 19:53
Joined
Oct 12, 2001
Messages
288
sorry to say it is still not working as soon as I go to a new record and tab through the various fields and get to the gender box it allows me to tab past it without putting anything in. After completing all the other fields in the form and tabbing out of the last field the access default message box comes up again saying that I must enater a value in PatientDetails.PatientGender field?

On the plus side i am not getting any errors when I changed the code !!

Please please help me as I am running out of beer now !!
 

vbaInet

AWF VIP
Local time
Today, 19:53
Joined
Jan 22, 2010
Messages
26,374
Code:
Private Sub PatientGender_BeforeUpdate(Cancel As Integer)
    If Len(Nz(Me!PatientGender, "")) = 0 Then
        Cancel = True
        MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
    End If
End Sub
    
Private Sub PatientGender_LostFocus()
    If Nz(Me[COLOR=Red][B].[/B][B]ID[/B][/COLOR], -1) <> -1 Then
        If Len(Nz(Me!PatientGender[B][COLOR=Red].Text[/COLOR][/B], "")) = 0 Then
            Me.PatientGender.SetFocus
            MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
        End If
    End If
End Sub
There are subtle differences between the functions. The code for the AfterUpdate should have stayed the same.

There should be an ID field in your table which uniquely identifies each record. Substitute ID (highlighted above) with the name of the control that field is bound to.
 

Paul Cooke

Registered User.
Local time
Today, 19:53
Joined
Oct 12, 2001
Messages
288
Code:
Private Sub PatientGender_BeforeUpdate(Cancel As Integer)
    If Len(Nz(Me!PatientGender, "")) = 0 Then
        Cancel = True
        MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
    End If
End Sub
    
Private Sub PatientGender_LostFocus()
    If Nz(Me[COLOR=Red][B].[/B][B]ID[/B][/COLOR], -1) <> -1 Then
        If Len(Nz(Me!PatientGender[B][COLOR=Red].Text[/COLOR][/B], "")) = 0 Then
            Me.PatientGender.SetFocus
            MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
        End If
    End If
End Sub
There are subtle differences between the functions. The code for the AfterUpdate should have stayed the same.

There should be an ID field in your table which uniquely identifies each record. Substitute ID (highlighted above) with the name of the control that field is bound to.

Ok we seem to be getting closer here now when i enter a new record and get to the gender control if I don't enter a value the msgbox appears BUT if i just click ok to the msg and carry on it still lets me go thruogh the form until the end and the dread access default message appears again !!

Can I just check this code is in the BeforeUpdate event as in your reply you mentioned "afterupdate"

Many thanks
 

vbaInet

AWF VIP
Local time
Today, 19:53
Joined
Jan 22, 2010
Messages
26,374
I see what you mean:
Code:
Private Sub PatientGender_LostFocus()
    If Nz(Me.ID, -1) <> -1 Then
        If Len(Nz(Me!PatientGender.Text, "")) = 0 Then
            Me[COLOR=Red][B].ToAnotherControl[/B][/COLOR].SetFocus
            Me.PatientGender.SetFocus
            MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
        End If
    End If
End Sub
ToAnotherControl must be a control that is not required. A command button will do too.
 

Paul Cooke

Registered User.
Local time
Today, 19:53
Joined
Oct 12, 2001
Messages
288
Thank you so much that has done the trick

What I did was to put a cmd button on the form called it CmdHidden and put the code in LostFocus and voila it is working great now !!

I really appriciated all the time in resolving this.
 

Users who are viewing this thread

Top Bottom