For the life of me I can't get my error-trapping and record-saving to work. I've been searching the forum and the 'net and just failing to get it.
There are two relevant forms, a Client Data form and a Data form. The Client Data form includes a subform that shows an abbreviated version of the data entered on the Data form and a button calling the Data form. The process of calling the Data form from the Client form enters the primary key of the client in the Data form. (I removed all the other fields on the Data form, and set up default data entries to focus on this error/saving problem.)
The Data form has a Cancel button, a Save button, and a Client Data button, which closes the form and returns to the Client form. There are several required fields in the table for the Data form, so I have a fnValidateForm (in Module1) that ensures that the required fields all have entries. There is also a unique index on the ClientID and the Date to prevent duplicate entries. (I'm about to make a second entry to trouble-shoot the error-trapping for this, as well.) The purposes of the buttons are self-explanatory, I think. Cancel causes Me.Undo, Save runs DoCmd.RunCommand acCmdSaveRecord, and Client Data is supposed to open the other form, updated with the Data entry just created. But something is going wrong and often the record is not actually saved.
The problem is in BeforeUpdate and has to do with the "acCmdSaveRecord command not being available now" and I just can't figure it out. Here's the code in BeforeUpdate:
When this code is activated by the Client Data button, I get error 2115 - The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Access from saving the data in the field. Only one field has a validation rule, and that's getting set properly (strTypeOfVisit). The highlighted code in Before Update is the DoCmd.RunCommand acCmdSaveRecord text.
I've tried just leaving the code under If vbYes blank, since I found one of Pat Hartman's entries talking about how good Access is at saving records when forms are closed, but that wasn't reliable. I tried putting a "your record was saved" msgbox under the form's AfterUpdate event, and that seemed to work for some reason - obviously I am absolutely missing the relevant variable here.
Oh, before you ask, they really really want all those nutty prompts about "do you really want to save???".
Can anyone help me figure this out??
There are two relevant forms, a Client Data form and a Data form. The Client Data form includes a subform that shows an abbreviated version of the data entered on the Data form and a button calling the Data form. The process of calling the Data form from the Client form enters the primary key of the client in the Data form. (I removed all the other fields on the Data form, and set up default data entries to focus on this error/saving problem.)
The Data form has a Cancel button, a Save button, and a Client Data button, which closes the form and returns to the Client form. There are several required fields in the table for the Data form, so I have a fnValidateForm (in Module1) that ensures that the required fields all have entries. There is also a unique index on the ClientID and the Date to prevent duplicate entries. (I'm about to make a second entry to trouble-shoot the error-trapping for this, as well.) The purposes of the buttons are self-explanatory, I think. Cancel causes Me.Undo, Save runs DoCmd.RunCommand acCmdSaveRecord, and Client Data is supposed to open the other form, updated with the Data entry just created. But something is going wrong and often the record is not actually saved.
The problem is in BeforeUpdate and has to do with the "acCmdSaveRecord command not being available now" and I just can't figure it out. Here's the code in BeforeUpdate:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim response As Variant
'Verify that data are entered in all Required fields
If Me.Dirty Then
If Not fnValidateForm(Me) Then Exit Sub
End If
Select Case MsgBox("This record contains changes. Do you want to save your changes to the current record?" & vbCrLf & " Yes: Saves Changes" & vbCrLf & " No: Undo Changes" & vbCrLf, vbYesNo + vbQuestion, "Save Your Changes?")
Case vbYes: 'Save the changes
response = MsgBox("Are you certain that you want to save your changes to this record? If you choose 'No' your changes will be erased.", vbYesNo + vbQuestion, "Verify Changes")
If response = vbYes Then
If Me.Dirty Then DoCmd.RunCommand acCmdSaveRecord 'error 2115 on the docmd statement
MsgBox "Your changes have been saved.", vbOKOnly + vbInformation, "Changes Saved"
Else
Me.Undo
If Me.Dirty = False Then MsgBox "Your changes have been erased.", vbOKOnly + vbInformation, "Changes Erased"
End If
Case vbNo: 'Undo changes
Me.Undo
If Me.Dirty = False Then MsgBox "Your changes have been erased.", vbOKOnly + vbInformation, "Changes Erased"
Case Else: 'Default case to trap any errors
'Do nothing
End Select
End Sub
When this code is activated by the Client Data button, I get error 2115 - The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Access from saving the data in the field. Only one field has a validation rule, and that's getting set properly (strTypeOfVisit). The highlighted code in Before Update is the DoCmd.RunCommand acCmdSaveRecord text.
I've tried just leaving the code under If vbYes blank, since I found one of Pat Hartman's entries talking about how good Access is at saving records when forms are closed, but that wasn't reliable. I tried putting a "your record was saved" msgbox under the form's AfterUpdate event, and that seemed to work for some reason - obviously I am absolutely missing the relevant variable here.
Oh, before you ask, they really really want all those nutty prompts about "do you really want to save???".
Can anyone help me figure this out??