Toggle
a Form between View-Only and Edit Mode
There
are four steps to creating this function.
1.
First paste the following code into a new module:
Option
Compare Database
Option
Explicit
Global
Const cTeal As Long = 8421440
Global
Const cGrey As Long = 12632256
Function
ToggleEdit(frm As Form, bEdit As Boolean)
On
Error GoTo err_ToggleEdit
' toggle edit and backcolor
If
bEdit Then
With
frm
.AllowAdditions
= True
.AllowDeletions
= True
.AllowEdits
= True
.Detail.BackColor
= cTeal
End
With
Else
With
frm
.AllowAdditions
= False
.AllowDeletions
= False
.AllowEdits
= False
.Detail.BackColor
= cGrey
End
With
End If
exit_ToggleEdit:
Exit
Function
err_ToggleEdit:
MsgBox
"Err Number " & Str(err) & ": " &
err.Description
Resume
exit_ToggleEdit
End
Function
2.
Second paste the following in the Form Properites Declaration:
Option
Compare Database
Option
Explicit
Dim bEdit As Boolean
Dim
x As Integer
3.
Paste the following code in the Form Open event:
Private
Sub Form_Open(Cancel As Integer)
bEdit = False
x
= ToggleEdit(Me, bEdit)
bEdit
= Not bEdit
End
Sub
4.
Create a command button called: cmdModify.
Place the following code in the OnClick event of the button:
Private
Sub cmdModify_Click()
On
Error GoTo Err_cmdModify_Click
If bEdit Then
cmdModify.Caption
= "View All Records"
x
= ToggleEdit(Me, bEdit)
'filter
records
DoCmd.ApplyFilter
, "PK = " & Me!PK
Else
cmdModify.Caption
= "Modify Current Record"
x
= ToggleEdit(Me, bEdit)
'unfilter
records
DoCmd.ShowAllRecords
End
If
bEdit
= Not bEdit
Exit_cmdModify_Click:
Exit
Sub
Err_cmdModify_Click:
MsgBox
err.Description
Resume
Exit_cmdModify_Click
End
Sub
Return to Top
|