Save Button

M

Mavis

Hi All,

I have this button "Save" to let user to click on the "Save" button to save
the record. When the user clcik on the Save the below error appear.
The error message is:

Compile Error:
Variable Not Defined

When the error message prompt, the code " Cancel = " is highlight.

Below is my code.

Private Sub Save_Claim_Button_Click()
Dim strMsg As String
Dim iResponse As Integer

' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."

' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")

' Check the user's response.
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo

' Cancel the update.
Cancel = False
End If
 
J

Jeanette Cunningham

Hi Mavis,
yes, this-->
Cancel = False
can't be used in code for a button.

Delete the 2 lines-->
' Cancel the update.
Cancel = False

If the user chooses yes, you need to save the record.
Change the code like this-->

' Check the user's response.
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo

Else
'save the changes
If Me.Dirty = True Then
Me.Dirty = False
End If

End If

You will need error handling for the times when there is a reason why the
record can't be saved, such as a missing entry or an incorrect entry.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
K

ken

If you want to force the users to save the record only by clicking the
button you'll need to prevent it being saved by other means such as
moving to another record, closing the form etc. The following code for
the form's module should do it:

''''' module starts '''''
' updates can only be saved via command button
Option Compare Database
Option Explicit

Dim blnSaved As Boolean

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdSave_Click()

Const MESSAGETEXT = "Do you wish to save the changes?" & _
vbNewLine & "Click Yes to Save or No to Discard changes."


If Me.Dirty Then
' if user confirms set varaible to True and attempt to save
record
If MsgBox(MESSAGETEXT, vbQuestion + vbYesNo, "Save Record?") =
vbYes Then
blnSaved = True
On Error Resume Next
RunCommand acCmdSaveRecord
' if reccord cannot be saved set variable to false
If Err <> 0 Then
blnSaved = False
End If
Else
Me.Undo
blnSaved = False
End If
End If

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_AfterUpdate()

' reset variable to False
blnSaved = False

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_BeforeUpdate(Cancel As Integer)

' cancel update if variable is False,
' i.e. save button has not been clicked
If Not blnSaved Then
Cancel = True
End If

End Sub

Private Sub Form_Current()

' reset variable to False
blnSaved = False

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Const IS_DIRTY = 2169

' suppress system error message if form
' is closed while record is unsaved,
' NB: changes to current record will be lost
If DataErr = IS_DIRTY Then
Response = acDataErrContinue
End If

End Sub
''''' module ends '''''

Ken Sheridan
Stafford, England
 

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