使用记录集中的数据向记录集外部的表添加记录

时间:2015-05-28 16:13:12

标签: vba access-vba ms-access-2010 recordset

我对VBA很新。我的背景是Informix,所以我对SQL非常熟悉。我正在尝试开发每周更新过程,其中读入记录文件并与各种访问表进行比较。根据比较结果,必须发生不同的事情。这个过程将在后台运行(只有屏幕上的内容才会输出报告名称)。我一直在努力弄清楚记录集。

虽然我从未见过从多个表中提取记录集的示例,但我的记录集是。对于我编辑/更新的记录集中的每条记录,我还想使用记录集中的一段数据在记录集之外的表中创建注释记录。这是一个简单的添加子,还是会以任何方式搞乱我的记录集位置?另外,我确定记录集的各个部分是否正确?

请参阅以下代码:

Sub query_records_sql()

'Comments: Use the results of a SQL string and to update and add records as required

  Dim dbs As DAO.Database
  Dim rst As DAO.Recordset
  Dim strSql As String

  ' Open pointer to current database
  Set dbs = CurrentDb()

  strSql = ""

  'Create record set for tmp_peo records that have matches in main/prime/sub tables
    strSql = " Select * FROM ((tmp_peo INNER JOIN tbl_prime ON (tmp_peo.prime = tbl_prime.prime)) " & _
  " INNER JOIN tbl_sub ON (tmp_peo.sub = tbl_sub.sub)) " & _
  " INNER JOIN tbl_main ON (tmp_peo.ship = tbl_main.ship_id) AND " & _
  " (tbl_prime.prime_id = tbl_main.prime_id) AND (tbl_sub.sub_id = tbl_main.sub_id)"

  ' Create recordset based on SQL
  Set rst = dbs.OpenRecordset(strSql)

  Do While Not rst.EOF
    If (tmp_peo.prime_nomenclature = tbl_prime.nomenclature) Then
     If (tmp_peo.sub_nomenclature = tbl_sub_nomenclature) Then
      If (((tmp_peo.quantity = tbl_main.quantity) Or (tmp_peo.quantity Is Null And tbl_main.quantity Is Null)) And _
       ((tmp_peo.sked_a_mdb = tbl_main.sked_a_mbd) Or (tmp_peo.sked_a_mdb Is Null And tbl_main.sked_a_mdb Is Null)) And _
       ((tmp_peo.swbs = tbl_main.swbs) Or (tmp_peo.swbs Is Null And tbl_main.swbs Is Null)) And _
       ((tmp_peo.rdd_to_ptc = tbl_main.rdd_to_ptc) Or (tmp_peo.rdd_to_ptc Is Null And tbl_main.rdd_to_ptc Is Null)) And _
       ((tmp_peo.ptc_bedd = tbl_main.ptc_bedd) Or (tmp_peo.ptc_bedd Is Null And tbl_main.ptc_bedd Is Null)) And _
       ((tmp_peo.peo_ships_am_rdd = tbl_main.peo_ships_am_rdd) Or (tbl_peo.peo_ships_am_rdd Is Null And tbl_main.peo_ships_am_rdd Is Null)) And _
       ((tmp_peo.sked_a_date = tbl_main.sked_a_date) Or (tbl_peo.sked_a_date Is Null And tbl_main.sked_a_date Is Null)) And _
       ((tmp_peo.sypd = tbl_main.sypd) Or (tmp_peo.sypd Is Null And tbl_main.sypd Is Null)) And _
       ((tmp_peo.parm_bedd_date = tbl_main.parm_bedd_date) Or (tmp_peo.par_bedd_date Is Null And tbl_main.parm_bedd_date Is Null)) And _
       ((tmp_peo.yard_actual = tbl_main.yard_actual) Or (tmp_peo.yard_actual Is Null And tbl_main.yard.actual Is Null)) And _
       ((tmp_peo.delivered_quantity = tbl_main.delivered_quantity) Or (tmp_peo.delivered_quantity Is Null And tbl_main.delivered_quantity Is Null)) And _
       ((tmp_peo.shipment = tbl_main.shipment) Or (tmp_peo.shipment Is Null And tbl_main.shipment Is Null))) Then
        'Output to 'No Update Required' Report
      Else
        'Edit/Update main with tmp_peo data
        rst.Edit
        tbl_main.quantity = tmp_peo.quantity
        tbl_main.sked_a_mdb = tmp_peo.sked_a_main
        tbl_main.swbs = tmp_peo.swbs
        tbl_main.rdd_to_ptc = tmp_peo.rdd_to_ptc
        tbl_main.ptc_bedd = tmp_peo.ptc_bedd
        tbl_main.peo_ships_am_rdd = tmp_peo.peo_ships_am_rdd
        tbl_main.sked_a_date = tmp_peo.sked_a_date
        tbl_main.sypd = tmp_peo.sypd
        tbl_main.parm_bedd_date = tmp_peo.parm_bedd_date
        tbl_main.yard_actual = tmp_peo.yard_actual
        tbl_main.delivered_quantity = tmp_peo.delivered_quantity
        tbl_main.shipment = tmp_peo.shipment
        rst.Update

       Call add_main_comment(tbl_main.main_id, 'PRG', =DATE(), 'MAIN RECORD UPDATED DURING WEEKLY PEO SKED A UPDATE PROCESS')
       'Output to 'Main Record Updated' Report
      End If
     Else
       'Edit/Update sub nomen with tmp_peo.sub_nomen
       rst.Edit
       tbl_sub.sub_nomenclature = tmp_peo.sub_nomenclature
       rst.Update
       Call add_main_comment(tbl_main.main_id, 'PRG', =DATE(), 'SUB RECORD UPDATED DURING WEEKLY PEO SKED A UPDATE PROCESS')
       'Output to 'Sub Record Updated' report
     End If
    Else
        'Edit/Update prime nomen with tmp_peo.prime_nomen
        rst.Edit
        tbl_prime.prime_nomenclature = tmp_peo.prime_nomenclature
        rst.Update
        Call add_main_comment(tbl_main.main_id, 'PRG', =DATE(), 'PRIME RECORD UPDATED DURING WEEKLY PEO SKED A UPDATE PROCESS')
        'Output to "Prime Record Updated' report
    End If

    rst.MoveNext
  Loop

  'Close recordset
  rst.Close

  'Close Reports

0 个答案:

没有答案