forms - How to use VBA to add new record in MS Access? -
i'm using bound forms user update information on new or existing customers. right i'm using add new record macro on submit button (because i'm not sure how add or save new record through vba). added before update event (using vba) have user confirm want save changes before exiting form. reason overriding add record button , users cannot add new record until exiting forms.
how can use vba add new customer information correct table? should done macros instead?
form beforeupdate code:
private sub form_beforeupdate(cancel integer) dim strmsg string strmsg = "data has been changed." strmsg = strmsg & " save record?" if msgbox(strmsg, vbyesno, "") = vbno docmd.runcommand accmdundo else end if end sub
add record button:
private sub btnaddrecord_click() dim tblcustomers dao.recordset set tblcustomers = currentdb.openrecordset("select * [tblcustomers]") tblcustomers.addnew tblcustomers![customer_id] = me.txtcustomerid.value tblcustomers![customername] = me.txtcustomername.value tblcustomers![customeraddressline1] = me.txtcustomeraddressline1.value tblcustomers![city] = me.txtcity.value tblcustomers![zip] = me.txtzip.value tblcustomers.update tblcustomers.close set tblcustomers = nothing docmd.close end sub
in order submit record using vba, create on click
event button, , in sub
run following command:
private sub submitbutton_click() 'all code validate user input. prompt user make sure want submit form, etc. docmd.runsql "insert tblcustomers (txtcustomerid.value, txtcustomername.value, txtcustomeraddressline1.value, txtcity.value, txtzip.value)" end sub
in sub, can add code want validate values user entered , choose whether or not want submit record. there's lot of control using vba submit forms, not need beforeupdate
event.
also, not use bound forms method. don't know repercussions wouldn't try it. access great starting off, want more complex things, easier use vba.
Comments
Post a Comment