Form validation and new record? (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Hi all,

How does one handle form validation and a new record?
I have the following code for validation on a form.

As you can see I am trying to handle a new record and validate if not a new record, (commented out for now) however I still need to validate if it is new record?
So how does one handle this situation?

At present as soon as I add the new record, I get the error messages below. Ok'ing through them eventually allows me to enter data, but I do not want to see them when adding a new record unless I have missed a control, but this is happening before I have the chance to enter any data.?

TIA
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error Resume Next
'If Not Me.NewRecord Then
    If IsNull(Me.cboFeeTypeID) Then
        Cancel = True
        'Me.Undo
        Me.Dirty = False
        MsgBox "Fee Type is mandatory"
    End If

    If Nz(Me.FeeAmount, 0) = 0 Then
        Cancel = True
        'Me.Undo
        Me.Dirty = False
        MsgBox "Fee amount must be > £0"
    End If
'End If
End Sub
 

missinglinq

AWF VIP
Local time
Today, 18:21
Joined
Jun 20, 2003
Messages
6,423
As moke123 suggested, you can't use

Me.Dirty = False

in the Form_BeforeUpdate event, as this interferes with what the Access Gnomes are already in the middle of doing, which is saving the Record...and will always pop an error!

Linq ;0)>
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Thank you both,

I'll give it a go when I next have to create a record.
I've already commented out the lines with that statement.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
OK,
I've commented out the Me.dirty code but as soon as I add a new record using the new record icon at the bottom of the form I get the messages twice.
I do not want to show the messages until I get to close the form?

TIA
 

moke123

AWF VIP
Local time
Today, 18:21
Joined
Jan 11, 2013
Messages
3,925
you get 2 messages because your calling it twice with 2 If/thens.

i often write mine differently along the lines of

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

     dim IntFlg as integer
     dim strMsg as string

     intFlg = 0

    If IsNull(Me.cboFeeTypeID) Then
          intflg = 1
          strMsg = strMsg & "Fee Type is mandatory" & vbnewline
    end if
        
    If Nz(Me.FeeAmount, 0) = 0 Then
          intflg = 1
          strMsg = strMsg &  "Fee amount must be > £0" & vbnewline
     End If

     If intFlg = 1 then
          Cancel = True
          MsgBox  strMsg
     End If

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Ah I see now.:eek:
Thank you Moke, I'll give that a go on Monday.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Moke,
Just to clarify both messages get displayed twice.?
The event is being called twice for some reason?
 

isladogs

MVP / VIP
Local time
Today, 23:21
Joined
Jan 14, 2017
Messages
18,243
I'm guessing that as each If clause causes an update the code loops/runs twice.
Try disabling one of the If sections.
Am I correct in saying there will only be one message ONCE?

If so try putting this in each If clause instead of what you have now

Code:
 If intflg=0 Then intflg=1

Does that fix it?
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Hi Colin,

Yes, I only want to see each message once.

It's in work, so cannot try until Monday.
I would also want both messages to be displayed, but I can do that easily enough using Mokes' logic.

I'm guessing that as each If clause causes an update the code loops/runs twice.
Try disabling one of the If sections.
Am I correct in saying there will only be one message ONCE?

If so try putting this in each If clause instead of what you have now

Code:
 If intflg=0 Then intflg=1

Does that fix it?
 

isladogs

MVP / VIP
Local time
Today, 23:21
Joined
Jan 14, 2017
Messages
18,243
What I'm suggesting won't stop each message being displayed
However, if I'm correct, it will stop the repeat message
 

moke123

AWF VIP
Local time
Today, 18:21
Joined
Jan 11, 2013
Messages
3,925
Just to clarify both messages get displayed twice.?

No. The first 2 if/then statements just check for null values and if they are,
concatenates a message string and sets the flag to 1.

the last if/then checks the flag and if it = 1 displays a single concatenated message and cancels the update.

Colin: I think he's talking about his code repeating, not mine. I just tested his as i thought it me be the On Error Resume Next but it wasnt. I think he may have some other code on the form causing his problem.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 23:21
Joined
Jan 14, 2017
Messages
18,243
Hi moke
Sorry - I got the wrong end of the stick....
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:21
Joined
Feb 19, 2002
Messages
43,316
Your original question is about new record validation. Generally new and existing records should be validated the same way. If not, put the New Record specific validation inside the If Me.NewRecord = True statement.

Unless you have some need to report all errors at once rather than one at a time, it is far easier for people to deal with fixing each error as it is encountered so I would use the following code. It sets focus to the control with the problem and exits immediately after canceling the update. I also change the null checks to include ZLS. If a user backspaces a control to "clear" it, you end up with a ZLS and those shouldn't be saved. In fact, in most cases, I set the Allow ZLS property to No so Access will prevent them from being saved.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    Dim strMsg as string

    If Me.cboFeeTypeID & "" = "" Then
          strMsg = strMsg & "Fee Type is mandatory" & vbnewline
          msgbox strMsg, vbOKOnly
          Cancel = True
          Me.cboFeeTypeID.SetFocus
          Exit Sub
    end if
        
    If Nz(Me.FeeAmount, 0) = 0  OR Me.FeeAmount & "" = "" Then
          strMsg = strMsg &  "Fee amount must be > £0" & vbnewline
          msgbox strMsg, vbOKOnly
          Cancel = True
          Me.FeeAmount.SetFocus
          Exit Sub
    End If

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Moke,

I have not had a chance to implement your code, as the DB is in work.

Yes my code currently shows each message twice before I can get to put any data in a control after I click the New record icon.

I will try both suggestions on Monday.

No. The first 2 if/then statements just check for null values and if they are,
concatenates a message string and sets the flag to 1.

the last if/then checks the flag and if it = 1 displays a single concatenated message and cancels the update.

Colin: I think he's talking about his code repeating, not mine. I just tested his as i thought it me be the On Error Resume Next but it wasnt. I think he may have some other code on the form causing his problem.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Hi Pat,

I want to validate the data, whether a new record or not. What I was trying to say, was that my code did not even give me the chance to enter data when adding a new record, before it was complaining that the data was missing.

Your original question is about new record validation. Generally new and existing records should be validated the same way. If not, put the New Record specific validation inside the If Me.NewRecord = True statement.

Unless you have some need to report all errors at once rather than one at a time, it is far easier for people to deal with fixing each error as it is encountered so I would use the following code. It sets focus to the control with the problem and exits immediately after canceling the update. I also change the null checks to include ZLS. If a user backspaces a control to "clear" it, you end up with a ZLS and those shouldn't be saved. In fact, in most cases, I set the Allow ZLS property to No so Access will prevent them from being saved.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    Dim strMsg as string

    If Me.cboFeeTypeID & "" = "" Then
          strMsg = strMsg & "Fee Type is mandatory" & vbnewline
          msgbox strMsg, vbOKOnly
          Cancel = True
          Me.cboFeeTypeID.SetFocus
          Exit Sub
    end if
        
    If Nz(Me.FeeAmount, 0) = 0  OR Me.FeeAmount & "" = "" Then
          strMsg = strMsg &  "Fee amount must be > £0" & vbnewline
          msgbox strMsg, vbOKOnly
          Cancel = True
          Me.FeeAmount.SetFocus
          Exit Sub
    End If

End Sub
 

moke123

AWF VIP
Local time
Today, 18:21
Joined
Jan 11, 2013
Messages
3,925
Do you have any code that runs that maybe sets default values or something?
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:21
Joined
Sep 21, 2011
Messages
14,334
Do you have any code that runs that maybe sets default values or something?
I only have some code to set the FK for the record, as the form is opened from another form and I pass in the ID of the parent.

Something along the lines of
Code:
If me.newrecord then
    Me.cboClientID = Me.openargs
end if

Best leave it until Monday and then I can answer with certainty.

TIA
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:21
Joined
Feb 19, 2002
Messages
43,316
I want to validate the data, whether a new record or not. What I was trying to say, was that my code did not even give me the chance to enter data when adding a new record, before it was complaining that the data was missing.
You have code that is causing the record to be saved. Please post the entire code module.

Also, I prefer to NOT dirty a record before the user does. This leads to confusion for the user. He thinks he just opened the form but didn't type anything and doesn't understand why he is getting a complaint about missing data. Or, even worse, you have no validation code and the empty (except for the FK) record just gets saved.

Use the BeforeInsert event to populate the foreign key. That event runs as soon as someone types the first character into ANY control. Once that happens, the user should recognize that HE dirtied the record and will not be confused by any missing data messages.
 

moke123

AWF VIP
Local time
Today, 18:21
Joined
Jan 11, 2013
Messages
3,925
I only have some code to set the FK for the record,
That's what I suspected. As Pat points out your dirtying the record.
Another option would be to set the default value along the lines of

Code:
If me.newrecord then
    Me.cboClientID.DefaultValue = Me.openargs
end if
 

Users who are viewing this thread

Top Bottom