I use the following code to give custom messages for fields that are not
filled out.
However, I am at a loss about the code deleting a record without prompting
the user, or even telling the user that you deleting a record?
It seems strange that ms-access automatically saves data for you, but you
now want to prompt the user to save, but actually delete records without any
kind of prompts? (sounds like you got this thinking backwards????). Access
usually prompts you when you are about to delete data..but always saved for
you automatically.
I would suggest that you put a delete button on your form, or at least in
the menu bar (if you are making a custom one).
If the user opens a form with a new record, that record is NOT created until
they type some data, so if you close the form, then you don't need to delete
the record...as none will have been crated.
However, you seem to be telling me that a user can enter a whole bunch of
data..and then if they close the form, you just go ahead and delete this
form, and they loose all their changes? Huh? What happens when they are in a
hurry, and hit the X button to close the form, and forget to hit the save
button? Surely you can't tell me you now want to delete all this data? You
mean they fill out all data except for ONE required field..and the whole
mess gets thrown out? You are kidding..right? What about editing a existing
form? You mean if they remove data from one field...now if they close and
don't hit save...you delete the data? You will have NOTHING but constant
complaints from your users about data being lost, or deleted if you do this!
Anyway, I will let you figure out some kind of delete button stuff..as the
above don't quite make sense. If the user wants to exit a form with required
fields, the either force them to enter those required fields, or give them a
delete button to throw the whole thing out.
The code below is a great way to verify fields that you want to be requited.
If a user does not correctly fill out all the required fields..they can't
save, or exit the form. And, if the user does not want the data..then they
should be forced to use a delete button.
The code is used as folows:
in the forms bedore update event..you go:
Cancel = MyVerify.
And, then the two follwing rouintes need be put into the forms module. You
can see how in the first example, you just put in the list of contorls that
you want requited..and also the text "error" message to dispaly. Note
carefully how the full string is enclosed in quotes..
Private Function MyVerify() As Boolean
Dim colFields As New Collection
MyVerify = False
colFields.Add "TourDate,Tour date"
colFields.Add "Description,Description"
colFields.Add "City,City"
colFields.Add "cboProvince,Province"
colFields.Add "StartDate,Start date"
colFields.Add "EndDate,end date"
MyVerify = vfields(colFields)
End Function
Private Function vfields(colFields As Collection) As Boolean
Dim strErrorText As String
Dim strControl As String
Dim i As Integer
vfields = False
For i = 1 To colFields.Count
strControl = Split(colFields(i), ",")(0)
strErrorText = Split(colFields(i), ",")(1)
If IsNull(Me(strControl)) = True Then
MsgBox strErrorText & " is required", vbExclamation, AppName
Me(strControl).SetFocus
vfields = True
Exit Function
End If
Next i
End Function