Ryan-
The default design of a form in Access lets you easily edit and save data with absolutely no code. However, it's easy to modify this default behavior if you're willing to write a bit of code.
First, to deal with allowing / disallowing edits, you need a little procedure to lock / unlock all editable controls - something like this:
Private Sub Lockit(intLocked As Integer)
Dim ctl As Control
On Error Resume Next
' Move the focus to a control that won't be affected
Me.cmdSave.SetFocus
' Set Locked for all text boxes, combo boxes, check boxes, and subforms
' in Detail section based on paramter
For Each ctl In Me.Section(0).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acSubform
ctl.Locked = intLocked
Case acCommandButton
ctl.Enabled = Not (intLocked)
End Select
Next ctl
End Sub
Use the Current event of the form to control initial locking:
Private Sub Form_Current()
' If on a new record,
If Me.NewRecord Then
' Unlock all controls
Lockit False
Else
' Prevent editing of an existing record
Lockit True
End If
End Sub
Create a command button that has a caption of "Edit Record" called cmdEdit and add this code:
Private Sub cmdEdit_Click()
' User wants to edit - unlock everything
Lockit False
End Sub
Create another command button to force a save called cmdSave:
Private Sub cmdSave_Click()
' If changes have been made
If Me.Dirty Then
' Force a save
Me.Dirty = False
' .. and lock all controls
Lockit True
End If
End Sub
If a user wants to clear all edits without saving, provide a "Cancel" button called cmdCancel with code like this:
Private Sub cmdCancel_Click
' If changes have been made
If Me.Dirty Then
' Clear all edits
Me.Undo
End If
' Relock all controls
Lockit True
End Sub
And finally, to catch and verify all attempts to save, use the form's Before Update event:
Private Sub Form_BeforeUpdate(Cancel As Integer)
' Ask the user to confirm
If vbNo = MsgBox("Are you sure you want to save your " & _
"changes?", vbQuestion + vbYesNo + vbDefaultButton2) Then
' User clicked No, so back out changes
Me.Undo
' And tell Access NOT to save
Cancel = True
End If
End Sub
Try it, you'll like it!
John Viescas, Author
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications
SQL Queries for Mere Mortals
(Paris, France)
On Feb 8, 2014, at 6:50 AM, Ryan S. <ryan.paschal@gmail.com> wrote:
Hi,I am MS Access 2007's user. I hope experts here can help me with my problems. My problems perhaps too simple for experts here.
Here is my case:I need a form to add/edit employee data.
I have made the basic form using form wizard, but it saves everything into table anyway without confirmation because it's connected to employee table directly via RecordSource.
I want the form to:
- Have a button for saving the record; confirms whether or not user want to save the record
- Have cancel button to cancel all data entry & clean the form to start over
- In view mode, be uneditable unless clicked the edit button
Please help me.
Thank you in advance.Best regards,Ryan
__._,_.___
| Reply via web post | Reply to sender | Reply to group | Start a New Topic | Messages in this topic (2) |
.
__,_._,___
Tidak ada komentar:
Posting Komentar