What is the magic code?

Benny Wong

Registered User.
Local time
Today, 13:12
Joined
Jun 19, 2002
Messages
65
Hello All,
I am working with Microsoft Access 2000. I am working on a data entry form. The form contains a listbox named ContactType and is set to extended which supports multi-select feature. The
ContactType field is on the many-side of the relationship. I have a button named cmdSave to save the user selection. My question is the code does work in saving the selected items into the ContactType table but if the user clicks on the button multiple of times it will also save the selected items multiple times. What will be the code to prevent the user from saving multiple times after they made their inital saved selection. Here is the present code:Private Sub CmdSave_Click()

DoCmd.RunCommand acCmdSaveRecord
Dim db As Database
Dim rs As Recordset
Dim ctl As Control
Dim itm As Variant

Set db = CurrentDb
Set rs = db.OpenRecordset("tblContactType")
Set ctl = Me.ContactID
For Each itm In ctl.ItemsSelected
rs.AddNew
rs!ContactType = ctl.ItemData(itm)
rs!Client_id = Me.Client_id
rs.Update
Next itm
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
rs.Close
Set rs = Nothing
Set db = Nothing
Set ctl =Nothing

End Sub

Thanks in advance for your help in this matter.
 
Just add this little bit of code to the end.

me.SomeOtherControl.Setfocus 'must take the focus from the cmd but
me.CmdSave.enabled = False

You will need to determine when it can be re-enabled though otherwise it will stay permanently disabled. Also, if this can only be done once per record, you may want to add similar code to the Form_Current event to enable/disable the button or enable/disable the listbox if it is a new record or not.
 

Users who are viewing this thread

Back
Top Bottom