简体   繁体   中英

Insert MS Access form data into another Access table

I have 2 Access database files. Database no. 1 is in my computer and database no. 2 is in the network shared folder.

I create a form in database 1 (in my computer) that inserts form data to table "Tbl_Requests" with following VBA code:

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("Tbl_Requests")
With rst
.AddNew
.Fields("IDLeave") = Me.Text_IDLeave.Value
.Fields("PersonalCode") = Me.Text_CP.Value
.Fields("FullName") = Me.Text_FullName.Value
.Fields("RequestDate") = Me.Text_RequestDate.Value
.Fields("Section") = Me.Text_Section.Value
.Fields("SuperName") = Me.Text_SuperName.Value
.Fields("LeaveRemained") = Me.Text_LeaveRemained.Value
.Fields("Des") = Me.Text_Des.Value
.Fields("LeaveDate") = Me.Combo_LeaveDate.Value
.Fields("Email") = Me.Text_Email.Value
.Update
End With

Now I want save the same data form to another table in database 2 that is in the network. How can I do this?

问题的图片

Simply switch out the CurrentDb object and point to networked database file:

Dim db As Database
Dim rst As Recordset 

Set db = OpenDatabase("C:\Path\To\Database.accdb")
Set rst = db.OpenRecordset("Tbl_Requests") 

With rst 
   .AddNew 
   .Fields("IDLeave") = Me.Text_IDLeave.Value 
   .Fields("PersonalCode") = Me.Text_CP.Value 
   .Fields("FullName") = Me.Text_FullName.Value 
   .Fields("RequestDate") = Me.Text_RequestDate.Value 
   .Fields("Section") = Me.Text_Section.Value 
   .Fields("SuperName") = Me.Text_SuperName.Value 
   .Fields("LeaveRemained") = Me.Text_LeaveRemained.Value 
   .Fields("Des") = Me.Text_Des.Value 
   .Fields("LeaveDate") = Me.Combo_LeaveDate.Value 
   .Fields("Email") = Me.Text_Email.Value 
   .Update 
End With

rst.Close
db.Close

Set rst = Nothing
Set db = Nothing

Link TableB from DB2 into DB1.

The linked table can be used (for most use cases) like a local table, your code works without changes, except perhaps the table name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM