Required fields upon saving

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with 5 required fields. They are not set to required in the
table properties. I want to be able to have a code run when I press the
"Save Button" that I made to check these fields and make sure they have data
entered into them. Anyone have any ideas? Any help would be deeply
appreciated.
 
Use the BeforeUpdate event of the form to perform the validation. That way
the code runs no matter how the save takes place.

This kind of thing:

Private Sub cmdSave_Click()
RunCommand acCmdSaveRecord
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String

If IsNull(Me.Surname) Then
Cancel = True
strMsg = strMsg & "Surname required." & vbCrLf
End If
If IsNull(Me.City) Then
Cancel = True
strMsg = strMsg & "City required." & vbCrLf
End If
'etc.
If Cancel Then
strMsg = strMsg & vbCrLf & "Correct the entry, or press <Esc> to
undo."
MsgBox strMsg, vbExclamation, "Invalid data"
End If
End Sub
 
Thanks. That worked like a charm.

Lori

Allen Browne said:
Use the BeforeUpdate event of the form to perform the validation. That way
the code runs no matter how the save takes place.

This kind of thing:

Private Sub cmdSave_Click()
RunCommand acCmdSaveRecord
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String

If IsNull(Me.Surname) Then
Cancel = True
strMsg = strMsg & "Surname required." & vbCrLf
End If
If IsNull(Me.City) Then
Cancel = True
strMsg = strMsg & "City required." & vbCrLf
End If
'etc.
If Cancel Then
strMsg = strMsg & vbCrLf & "Correct the entry, or press <Esc> to
undo."
MsgBox strMsg, vbExclamation, "Invalid data"
End If
End Sub
 
It works great but when I test it and click the ok button. I get the message
"No current record". How can I fix this?

Lori
 
Private Sub cmdSave_Click()
If Me.Dirty Then
RunCommand acCmdSaveRecord
End If
End Sub
 
Well I'm not sure what I'm doing wrong but it still gives me the error
message. What I did was put the last code you sent be on the save button I
created.

Lori
 
I thought this might help also. This is just the basic code that it uses
when you create a save button. This is what I'm using to save the record.

Private Sub cmdSaveRec_Click()
On Error GoTo Err_cmdSaveRec_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_cmdSaveRec_Click:
Exit Sub

Err_cmdSaveRec_Click:
MsgBox Err.Description
Resume Exit_cmdSaveRec_Click

End Sub
 
Lori, did you try the suggested code instead?

Just replace the DoMenuItem line with the 3 lines I suggested, and leave the
error handler in place.
 
Ok. I did that and it still tells me no current record. I can click ok and
it will let me enter the missing data.
 
There has to be something else going on here.

Above those 3 lines, add the word:
Stop
and change the MsgBox line to:
MsgBox "Error " & Err.Number & ": " & Err.Description, ,
"cmdSaveRec_Click"

Then choose Compile on the Debug menu.
Fix any errors, and repeat until it compiles okay.

Does it still fail?
Does it stop and highlight the Stop line in yellow?
Do you get the MsgBox? What Number is the error?

Also, Uncheck the boxes under:
Tools | Options | General | Name AutoCorrect
Then compact the database to get rid of this junk:
Tools | Database Utilities | Compact/Repair
Explanation of why:
http://allenbrowne.com/bug-03.html

It could be an unrelated issue such as something in another routine or
validation rule, a decompile issue. What is the source for this form (a
table or a query?)
 
Ok. I wasn't sure where to put "Stop" so I put it above the Msgbox "Error"
line. If this isn't right, let me know. I ran the compile and fixed all
problems. Had some stray code that should have been deleted. I unchecked
the boxes and C&R the database. When I click on the button now, the error
code is 3021: no current record. I click ok and it keeps going; doesn't show
debug dialog.

Lori
 
Okay, if the Stop never happened, and the MsgBox doesn't show, you have
demonstrated that the problem has nothing to do with the code.

The error is caused by something else, perhaps related to the control that
has the focus, a bad validation rule or default value, or a problem with the
source query for the form.
 
The only other thing that I can think that might be messing with it is I have
the drawing number automatically incrementing using dmax and there is also
coding behind the form to check and make sure that no other user is using the
same number.
 
Well I probably should have checked this when another using is in the
database. If I try to click on the save button from another user then I get
error 2046 Can'g save record.

I have coding that handles duplicate drawing numbers that worked fine until
I added your coding. Here it is:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
On Error GoTo Err_Form_Error
Response = IncrementField(DataErr)

Exit_Form_Error:
Exit Sub

Err_Form_Error:
MsgBox Err.Description
Resume Exit_Form_Error
End Sub

Function IncrementField(DataErr)
If DataErr = 3022 Then
Me.TXTDRAWING = DMax("drawing", "tbldrawings") + 1
IncrementField = acDataErrContinue
End If
End Function


Got any suggestions on how I can get all this to work for multi-user
environment?

Lori
 
As a half-baked fix, move the code that assigns the drawing number into
Form_BeforeUpdate so that it occus at the last possible instant (after any
MsgBox you use in that event.) That reduces the chance of a clash.

For a true solution, you need to create a small table to hold the highest
number so far. Then write code that locks the table, assigns the value,
saves the record, and only then releases the lock. The code needs to test if
the code is locked, and if so perform random waits up to a maximum number of
tries.
 
Mr. Browne
I used your validation procedure in this post and the msg box works fine but
I too am getting the "No Current Record" error even after using the Me.Dirty
line of code. I am using the save command within a form with tab control.
The main form is based on one table (tblJobLog). This table has and
Autonumber primary key I am using as the JobID. There is one subform on one
of the tabs. The subform is based on a seperate table (tblJobFeatures). It
is linked to the mainform using the JobID field.

No data validation is set on the either of the tables properties and I have
turned off the Name Autocorrect option. I have set up one relationship
between the two tables using the JobID field. The tblJobFeatures table is
also a subdatasheet within the tblJobLog table.

Do you think the the Autonumber JobID field causing the problem. Let me
know if you want to see my code.

Please advise.

Thanks
 
Back
Top