Dim mblnIsDirty As Boolean
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
If mblnIsDirty Then
Select Case MsgBox("Do you want to save the changes you have made to this record ?" & vbLf _
& "Click Yes to Save and Close" & vbLf _
& "Click No to close without saving" & vbLf _
& "Click Cancel to return to the form." _
, vbYesNoCancel _
, "Data on the form has changed")
Case Is = vbYes
Call SaveRecord(Nz(TestId, 0))
Case Is = vbNo
' don't save
Case Else
Exit Sub
End Select
End If
DoCmd.Close
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
MsgBox Err.Description, , Err.Number
Resume Exit_cmdClose_Click
End Sub
Private Sub Form_Activate()
DoCmd.Maximize
End Sub
Private Sub Form_Load()
On Error GoTo Err_Form_Load
Dim rstMy As ADODB.Recordset
mblnIsDirty = False
cmdSaveChanges.Enabled = False
If IsNull(OpenArgs) Then
' Add new record mode
Else
Set rstMy = New ADODB.Recordset
rstMy.Open "SELECT * FROM tblTest WHERE TestId = " & OpenArgs, MyConn
If rstMy.EOF Then
Else
Call SetAllControls(rstMy)
End If
rstMy.Close
Set rstMy = Nothing
End If
Exit_Form_Load:
Exit Sub
Err_Form_Load:
If Err.Number = 3709 Then ' MyConn = nothing
Call SetMyConn
Resume
Else
MsgBox Err.Description, , Err.Number
Resume Exit_Form_Load
End If
End Sub
‘ Control Procedures
‘ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
‘ General Procedures
Private Function NowDirty()
mblnIsDirty = True
cmdSaveChanges.Enabled = True
End Function
Private Sub SaveRecord(RecId As Long)
On Error GoTo Err_SaveRecord
Dim rstMy As ADODB.Recordset
Set rstMy = New ADODB.Recordset
rstMy.CursorType = adOpenKeyset
rstMy.LockType = adLockOptimistic
rstMy.Open "SELECT * FROM tblTest WHERE TestId = " & RecId, MyConn
If rstMy.EOF Then
rstMy.AddNew
End If
rstMy!TestText = TestText
' ADD ALL FIELDS HERE
rstMy.Update
' If this is saving a new record then update the screen display of the TPReqId
If IsNull(TestId) Then
rstMy.Close
rstMy.Open "SELECT Last_Insert_Id() As TestId"
TestId = rstMy!TestId
RecId = TestId ' Returns the new Id to the calling proc. _
( To verify okay completion )
End If
rstMy.Close
Set rstMy = Nothing
Exit_SaveRecord:
Exit Sub
Err_SaveRecord:
If Err.Number = 3709 Then ' MyConn = nothing
Call SetMyConn
Resume
Else
MsgBox Err.Description, , Err.Number
Resume Exit_SaveRecord
End If
End Sub
Private Sub SetAllControls(rst As ADODB.Recordset)
TestId = rst!TestId
TestText = rst!TestText
' ADD ALL FIELDS HERE
End Sub