【问题标题】:Insert MS Access form data into another Access table将 MS Access 表单数据插入另一个 Access 表
【发布时间】:2017-03-30 15:34:37
【问题描述】:

我有 2 个 Access 数据库文件。数据库编号1 在我的计算机和数据库中。 2 在网络共享文件夹中。

我在数据库 1(在我的计算机中)中创建了一个表单,它使用以下 VBA 代码将表单数据插入到表“Tbl_Requests”中:

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

现在我想将相同的数据表单保存到网络中数据库 2 中的另一个表中。我该怎么做?

【问题讨论】:

    标签: database ms-access vba


    【解决方案1】:

    只需切换出 CurrentDb 对象并指向网络数据库文件:

    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
    

    【讨论】:

      【解决方案2】:

      LinkTableB 从 DB2 到 DB1。

      链接表可以像本地表一样使用(对于大多数用例),您的代码无需更改即可运行,除了表名。

      【讨论】:

      • 我可以用vba代码吗?比如上面的代码插入记录到表B?
      猜你喜欢
      • 2016-09-14
      • 2023-03-12
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      相关资源
      最近更新 更多