what is wrong in this code

G

Guest

i have this code in before update of the form. but if i make changes to
patient name , and confirm the changes , i will get error in the code

Private Sub patient_BeforeUpdate(Cancel As Integer)
If Not Me.NewRecord Then
If MsgBox("Changes have been made to the Patient Name" _
& vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes have been Made...") = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
Else
Cancel = True
Me!PATIENT.Undo
End If
End If
End Sub

it is showing error in this line
DoCmd.RunCommand acCmdSaveRecord

so what is wrong with this . any body can help
 
K

Ken Snell [MVP]

You cannot save the data changes during the control's BeforeUpdate event. If
the control is bound to the field that holds the patient name, you don't
need that line of code at all.

Private Sub patient_BeforeUpdate(Cancel As Integer)
If Not Me.NewRecord Then
If MsgBox("Changes have been made to the Patient Name" _
& vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes have been Made...") = vbNo Then
Cancel = True
Me!PATIENT.Undo
End If
End If
End Sub
 
G

Guest

Ken Snell said:
You cannot save the data changes during the control's BeforeUpdate event. If
the control is bound to the field that holds the patient name, you don't
need that line of code at all.

Private Sub patient_BeforeUpdate(Cancel As Integer)
If Not Me.NewRecord Then
If MsgBox("Changes have been made to the Patient Name" _
& vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes have been Made...") = vbNo Then
Cancel = True
Me!PATIENT.Undo
End If
End If
End Sub
thanks mr ken
i tries this and now it is ok. except one problem which is :it will promt me
even in case of new record. any idea how to solve this. thanks again
 
K

Ken Snell [MVP]

Define what "new record" means to you... to ACCESS, it means a record that
is being created but has not been saved to the underlying recordsource yet.
As soon as the data are saved the first time, the record no longer is a new
record. I'm guessing that you're saving the data at some point but you still
consider the record to be "new".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top