具有广泛Informix SQL背景的Access VBA新手。我有一个包含4个表的记录的记录集。
根据不同的情况,我想用第四个表中的数据更新其中三个表中的记录。
我还想将记录添加到评论表并输出到报告。可以以及如何执行此操作:代码如下:
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
' Update main with tmp_peo data
Call update_main
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
'Update sub nomen with tmp_peo.sub_nomen
Call update_sub
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
'Update prime nomen with tmp_peo.prime_nomen
Call update_prime
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
'Open action required report
strSql = ""
'Create record set for tmp_peo records that have no matches in main/prime/sub tables
strSql = " Select tmp_peo.* WHERE tmp_peo.id Not In " & _
" (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)"
dbs.Close
End Sub