Error Message Handling (1 Viewer)

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
Hi Not sure i this is an easy one or not but I have no idea how get around it

I have for form with a required control "Gender"

The following code is under the BeforeUpdate event

Code:
Private Sub PatientGender_BeforeUpdate(Cancel As Integer)
    Dim strMsg As String, strTitle As String
    Dim intStyle As Integer


    
    If IsNull(Me!PatientGender) Or Me!PatientGender = "" Then
    strMsg = "Please select a gender"
    strTitle = "Choose Gender"
    iniStyle = vbOKOnly
    MsgBox strMsg, iniStyle, strTitle
    Cancel = True
End If

End Sub


What is happening is that when the user inputting details innto the form gts to the last field (without entering a gender) i get the default error message "you must enter a value...."

Is it possible to stop the user from going past the gender control until something is input into it? And how do I change / stop the default message
from popping up?

Many thanks for any advice or solutions you can offer

Paul
 

Kryst51

Singin' in the Hou. Rain
Local time
Today, 13:10
Joined
Jun 29, 2009
Messages
1,898
Hi Not sure i this is an easy one or not but I have no idea how get around it

I have for form with a required control "Gender"

The following code is under the BeforeUpdate event

Code:
Private Sub PatientGender_BeforeUpdate(Cancel As Integer)
    Dim strMsg As String, strTitle As String
    Dim intStyle As Integer
 
 
 
    If IsNull(Me!PatientGender) Or Me!PatientGender = "" Then
    strMsg = "Please select a gender"
    strTitle = "Choose Gender"
    iniStyle = vbOKOnly
    MsgBox strMsg, iniStyle, strTitle
    Cancel = True
End If
 
End Sub


What is happening is that when the user inputting details innto the form gts to the last field (without entering a gender) i get the default error message "you must enter a value...."

Is it possible to stop the user from going past the gender control until something is input into it? And how do I change / stop the default message
from popping up?

Many thanks for any advice or solutions you can offer

Paul

You are getting this error with your current code? Have you tried stepping through the code using F8 in break mode?

The only thing I can think of is to do the following:

Code:
Private Sub PatientGender_BeforeUpdate(Cancel As Integer)
    Dim strMsg As String, strTitle As String
    Dim intStyle As Integer
 
 
 
    If [COLOR=red][B]Len(Nz(Me!PatientGender,0) = 0[/B][/COLOR] Then
    strMsg = "Please select a gender"
    strTitle = "Choose Gender"
    iniStyle = vbOKOnly
    MsgBox strMsg, iniStyle, strTitle
    Cancel = True
End If
 
End Sub
 

vbaInet

AWF VIP
Local time
Today, 19:10
Joined
Jan 22, 2010
Messages
26,374
Just a slight adjustment:
Code:
Private Sub PatientGender_BeforeUpdate(Cancel As Integer)
    If Len(Nz(Me!PatientGender, [COLOR=Red][B]""[/B][/COLOR]) = 0 Then
        Cancel = True
        MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
    End If
End Sub
Remember to set the Allow Zero Length property to No as well in the table.
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
Thanks guys for the replys but it seems not to working fully

If I open the form and go to and old record then click in the gender control the msgbox will pop up (if gender is empty that is) and it won't let me move on until I choose a gender.

If I go to another old record where gender control field is empty and tab through the controls on the record it will go straight past it without a msg (even access default msg)

If I go to a new record it again allows me to tab straight past it without a msg but when i tab past the last control in this new record the default Access msg You must Enter a Value ... comes up again


The customs message only seems to trigger if I actually go int to and old record delete the gender that is in there and then ty to leave it blank.

Hopefully this all makes sense ??

thanks guys
 

Kryst51

Singin' in the Hou. Rain
Local time
Today, 13:10
Joined
Jun 29, 2009
Messages
1,898
How about putting the code in the form's before update event instead of the control?
 

vbaInet

AWF VIP
Local time
Today, 19:10
Joined
Jan 22, 2010
Messages
26,374
Well, putting it in the form's Before Update event won't have any effect on existing records.

Any interruption would have to be done when moving through records. However, there' are no Before Next or Before Previous events of the form, so, it's either you:

* Create your own custom navigation buttons and perform the check in there

OR

* Create a separate form that will draw all records where Gender has not been entered (for old records) and get someone to fill it in.
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
sorry guys I may of confused the issue with my reply.

The DB is in a design testing stage so I don't really mind if the old records work or not.. what I am trying to do is "finalise" this form so that when a user ends up on the form from navigation or buttons whatever it is and begins to enter a new record they cannot go past the gender control without something being entered into it. It currently has a value list of Male Female & unknown. So logic tells me that if they have to enter something then the default access msg would never be triggered on this control anyway

for information the table properties for the control have been set as follows

Required - Yes
Allow Zero Length - No

Does this make more sense now?

Thanks for your time in replying

Paul
 

vbaInet

AWF VIP
Local time
Today, 19:10
Joined
Jan 22, 2010
Messages
26,374
You mentioned -->
If I go to a new record it again allows me to tab straight past it without a msg but when i tab past the last control in this new record the default Access msg You must Enter a Value ... comes up again
You didn't mentioned whether the last control is the Gender control or not. I suspect it isn't so I don't see why you expect an event for related to Gender to fire. That default message is for another field, not the Gender field. Look at your table for all the other REQUIRED fields and code them accordingly.

Perform the same check on the LostFocus event of the Gender control and set focus back to it if it's "blank".
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
again thanks for you reply and your patience !

I am sorry if i am confusing everyone !

So take it back to basics

I have a new table with no records
I have a form with 5 controls

FirstName
LastName
Gender
Date
Occupation

when I open the formm to enter the first ever record its focus will be on FirstName, I enter that the focus moves to LastName, I enter that the focus moves to Gender...

This is where I want to force the user to choose a value from the list. If they do not choose a value then they cannot move on to the next control (Date) until they do. If they try to leave the gender control empty the msg "Choose gender" pops up and returns the focus to the Gender Control unril they do choose a value from the list.

Does that make more sense

I am really sorry to be such a pain but I am trying to "fix" the forms controls in a logical order before moving on with the rest of the design

Thanks again
 

vbaInet

AWF VIP
Local time
Today, 19:10
Joined
Jan 22, 2010
Messages
26,374
This is where I want to force the user to choose a value from the list. If they do not choose a value then they cannot move on to the next control (Date) until they do. If they try to leave the gender control empty the msg "Choose gender" pops up and returns the focus to the Gender Control unril they do choose a value from the list.
No probs Paul, we understand. I answered it here:
Perform the same check on the LostFocus event of the Gender control and set focus back to it if it's "blank".
That is, using the same code and setting focus back to the control if necessary.
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
Thanks so much for your understanding..

So just to confirm I copy the code in the BeforeUpdate Event to The OnLostFocus Event (I can actually understand how that will work !)

would you mind telling me how I add the "Set.Focus" into the code please I would assume it would be Docmd somewhere in the line?

also if I have a StrConv code already in the OnlostFocus event of other controls how do I add this in as well?

thanks you soo much for your time, If I could buy you a beer I would !! maybe a cyber one !

Paul
 

Kryst51

Singin' in the Hou. Rain
Local time
Today, 13:10
Joined
Jun 29, 2009
Messages
1,898
To use vbaInet's code, I believe it is:

Code:
    If Len(Nz(Me!PatientGender, [B][COLOR=black]""[/COLOR][/B]) = 0 Then
    Me!PatientGender.SetFocus
    'You could also add your code for the message box if you wanted
    Else
    Exit Sub
    End If
 

vbaInet

AWF VIP
Local time
Today, 19:10
Joined
Jan 22, 2010
Messages
26,374
Like so:
Code:
If Not Me.NewRecord Then
    If Len(Nz(Me!PatientGender, "") = 0 Then
        Me.PatientGender.SetFocus
        MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
    End If
End If
What is this other code that you have?

Oh, you can send the beer through the post via special delivery:D

Edit: Don't forget to buy Kryst51 one too
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
Its Red Stripe - maybe if I wasn't drinking it now I wouldn't be getting so confused !!

Thanks for the reply and solution so far.

I have two other controls where entry is required FirstName & LastName

The code for the gender will work pretty much the same for these as well but in the OnlostFocus of the "name" controls I have

Code:
FirstName.Value = StrConv(FirstName, vbProperCase)

( I do know there are issues with this on names such as Mc, MacDonald etc.. but to be honest for the amount of times we enter names like this I am happy for a simple Proper Case aspect)

thanks again

Paul
 

vbaInet

AWF VIP
Local time
Today, 19:10
Joined
Jan 22, 2010
Messages
26,374
That code should be in the AfterUpdate event of the control.

If you have existing data that needs converting then UPDATE the records. I believe I'm mentioned this before.

By the way, some people have problems with vbProperCase and I think the Integer equivalent is 3, so replace vbProperCase with 3.
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
ok maany thanks for the tip on the Case issue

So I would assume it would be

Code:
FirstName.Value = StrConv(FirstName, 3)

Many thanks again.

I will get this all input and post back the results
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
sorry but should there be a second ) at the end of

Code:
 If Len(Nz(Me!PatientGender, "") = 0 Then

As I am getting a compile errr - syntax error and this line is red?
 

vbaInet

AWF VIP
Local time
Today, 19:10
Joined
Jan 22, 2010
Messages
26,374
Oh yes, there should:

If Len(Nz(Me!PatientGender, "")) = 0 Then
 

Paul Cooke

Registered User.
Local time
Today, 19:10
Joined
Oct 12, 2001
Messages
288
Oh yes, there should:

If Len(Nz(Me!PatientGender, "")) = 0 Then

I think I will have to send you guys a crate of beer ... it's not working !!

I have the following

Code:
Private Sub PatientGender_BeforeUpdate(Cancel As Integer)
    Dim strMsg As String, strTitle As String
    Dim intStyle As Integer
  
  If Not Me.NewRecord Then
    If Len(Nz(Me!PatientGender, "")) = 0 Then
        Me.PatientGender.SetFocus
        MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
    End If
End If
  
    End Sub
    
Private Sub PatientGender_LostFocus()
    Dim strMsg As String, strTitle As String
    Dim intStyle As Integer
  
  If Not Me.NewRecord Then
    If Len(Nz(Me!PatientGender, "")) = 0 Then
        Me.PatientGender.SetFocus
        MsgBox "Please select a gender", vbOKOnly, "Choose Gender"
    End If
End If
  
End Sub

I have checked the properties for the Gender field in the table it is set to required and not to allow zero lenght - ... any ides what I can do next?


Thanks you both again !
 

Users who are viewing this thread

Top Bottom