Ryan-
When you set AllowEdits to False, *all* of your controls become locked. If, for example, you have an unbound combo box in the form header to help search for records, the user won't be able to select a value in the combo. Note that my code locks the controls only in the Detail section of the form. It therefore leaves any combo boxes, text boxes, or command buttons still enabled in the form header or footer.
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 10, 2014, at 10:28 AM, 易健福 <ryan.paschal@gmail.com> wrote:
Dear John,After reviewing your codes, what do you think if I use allowedits property to false instead of locking each controls?
Is there other implications & side effect?RyanOn Mon, Feb 10, 2014 at 4:13 PM, 易健福 <ryan.paschal@gmail.com> wrote:
Thank you for your answer.Let me digest the codes first.RyanOn Sat, Feb 8, 2014 at 5:01 PM, John Viescas <JohnV@msn.com> wrote:
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 ControlOn Error Resume Next' Move the focus to a control that won't be affectedMe.cmdSave.SetFocus' Set Locked for all text boxes, combo boxes, check boxes, and subforms' in Detail section based on paramterFor Each ctl In Me.Section(0).ControlsSelect Case ctl.ControlTypeCase acTextBox, acComboBox, acSubformctl.Locked = intLockedCase acCommandButtonctl.Enabled = Not (intLocked)End SelectNext ctlEnd SubUse 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 controlsLockit FalseElse' Prevent editing of an existing recordLockit TrueEnd IfEnd SubCreate 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 everythingLockit FalseEnd SubCreate another command button to force a save called cmdSave:Private Sub cmdSave_Click()' If changes have been madeIf Me.Dirty Then' Force a saveMe.Dirty = False' .. and lock all controlsLockit TrueEnd IfEnd SubIf 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 madeIf Me.Dirty Then' Clear all editsMe.UndoEnd If' Relock all controlsLockit TrueEnd SubAnd 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 confirmIf vbNo = MsgBox("Are you sure you want to save your " & _"changes?", vbQuestion + vbYesNo + vbDefaultButton2) Then' User clicked No, so back out changesMe.Undo' And tell Access NOT to saveCancel = TrueEnd IfEnd SubTry it, you'll like it!John Viescas, AuthorMicrosoft Access 2010 Inside OutMicrosoft Access 2007 Inside OutMicrosoft Access 2003 Inside OutBuilding Microsoft Access ApplicationsSQL 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 (5) |
.
__,_._,___
Tidak ada komentar:
Posting Komentar