want to check the values of my controls in the code behind the Submit button (1 Viewer)

sree0009

Registered User.
Local time
, 21:25
Joined
Nov 20, 2017
Messages
15
hi i have stuck in the forms update event where the event is not firing up.
i have to check or validate certain feilds and have to make sure they are not empty in the fields. example i have 8 fields.

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Form_Master.Primary_Key) = True Then
Me!Primary_Key.SetFocus
Cancel = True
Exit Sub
ElseIf IsNull(Form_Master.AddSuffix) = True Then
Me!AddSuffix.SetFocus
Cancel = True
Exit Sub
ElseIf CppValue.ListIndex = -1 Then
Me!CppValue.SetFocus
Cancel = True
Exit Sub
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
, 21:25
Joined
Aug 30, 2003
Messages
36,139
If the controls could hold a zero length string, try this test instead:

If Len(Me.Primary_Key & vbNullString) = 0 Then

Also, you're aware that the update events won't fire if nothing was changed (updated)?
 

sree0009

Registered User.
Local time
, 21:25
Joined
Nov 20, 2017
Messages
15
hi, Actually my main reason was the same issue you stated before. my form has many fields and if the user tries to press save button it saves the record. so it skips the validation written in the before update events. i tried with after update still no luck.
why is it that user has to press on certain textbox or combo box to get fire the event ?
 

pbaldy

Wino Moderator
Staff member
Local time
, 21:25
Joined
Aug 30, 2003
Messages
36,139
Theoretically if they have changed anything, there's nothing to save (update). Also, the form has to be bound to a table/query for the update events to fire. Is yours, or are you saving with code?
 

sree0009

Registered User.
Local time
, 21:25
Joined
Nov 20, 2017
Messages
15
sorry i didnt get you. but here is exactly
what's happening.

when i open the master form it has 35 fields where i require few fields to get validated as 8 of them. but if i dont press on any textboxes and just press on save button still my records saves as null values in the table. yes my form is bounded to Master table and Master is my forms name.

Whats expected
when i open the form and just press on save button it should check for the necessary fields and remain the cursor on that specific textbox and remain untill all fields are filled then should save.

Somewhere in the bottom of my forms update event i used this one to restrict the data to auto save
ElseIf Not blnSave Then
Cancel = True
Me.Undo
End If
 

pbaldy

Wino Moderator
Staff member
Local time
, 21:25
Joined
Aug 30, 2003
Messages
36,139
Have you considered making certain fields required in the table, so that Access won't allow a record to be saved if they're left Null? That's what I would do; the code in the before update event gives you a more user-friendly error and more control over what happens.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:25
Joined
Feb 19, 2002
Messages
43,603
If the form is BOUND, the BeforeUpdate event ALWAYS runs if the form is dirty. All your save button should do is to check the record to see if it is dirty and force it to save if it is.

Code:
If Me.Dirty Then
    DoCmd.RunCommand acCmdSaveRecord
End if

I use the following syntax to check for null or ZLS:

If Me.Primary_Key & "" = "" Then

I'm not sure where you got the reference style you are using. I'm assuming it compiles but is it working correctly? Put a stop in the code and when you press your save button, you should be taken to the Form's BeforeUpdate event so you can step through it and see what is happening.

PS - It's hard to read your code because it isn't properly indented. also, using ElseIF simply confuses the code. Each statement is independent so there is no reason to string all the tests together into a single If.

I also don't know what you are trying to do with the "Response" variable. It isn't defined so you must not have Option Explicit set - set this NOW and compile to find all the compile errors. FYI - Response is a VBA Reserved Word. Use it at your peril.
 

sree0009

Registered User.
Local time
, 21:25
Joined
Nov 20, 2017
Messages
15
hi Pat, can you be more detail. i cant get you what you meant about response ? Also i said before my formsbefore update event is not firing all the cases of condition
 

pbaldy

Wino Moderator
Staff member
Local time
, 21:25
Joined
Aug 30, 2003
Messages
36,139
I'll get out of the way.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:25
Joined
Feb 19, 2002
Messages
43,603
sree0009 - you modified your original post to remove the code I was questioning? Please don't do that. It makes other peoples comments make no sense. OR if you do need to correct something, at least post a reason. The changed code makes no sense either. Please post in a separate reply (Do NOT keep changing your original post) the EXACT code you are using.
Also i said before my formsbefore update event is not firing all the cases of condition
Did you do what I suggested and put a stop in the code?

Paul, Are you mad at me? I think we were posting at the same time. I jumped in because it is important to determine why the BeforeUpdate isn't running if that is in fact what is happening.
 

sree0009

Registered User.
Local time
, 21:25
Joined
Nov 20, 2017
Messages
15
im sorry for editing the original post. yes i kept stop in the code but couldnt figure out still. unfotunately i swapped the code into my save button and changed a bit now its working. thanks pat.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:25
Joined
Feb 19, 2002
Messages
43,603
The validation code DOES NOT belong in the save button. It belongs in the BeforeUpdate event of the Form. The BeforeUpdate event is the LAST event that runs before a record gets saved and it is never bypassed so it is the only event where you can be absolutely certain you have prevented bad data from being saved.

Your code may appear to work, but I would be willing to bet $100 for charity that I can make it save bad data and if I can do it, your users will also.
 

Users who are viewing this thread

Top Bottom