How to update BegAtt & EndAtt values by MDB query?

半腔热情 提交于 2019-12-25 01:49:00

问题


I want to update the BegAtt & EndAtt values based on the Control Start, Control Stop, Application, and Record Type fields in MDB by query. Below is the record set of my MDB database where I want to update BegAtt and EndAtt values based on the parent record which has Record Type = Email:

Application             Record Type         Control Start       Control Stop        BegAtt  EndAtt
Outlook Mail Document   Email               3rd-Party_00000040  3rd-Party_00000040      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000041  3rd-Party_00000044      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000045  3rd-Party_00000045      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000046  3rd-Party_00000049      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000050  3rd-Party_00000181      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000182  3rd-Party_00000223      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000224  3rd-Party_00000243      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000244  3rd-Party_00000250      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000251  3rd-Party_00000460      
Outlook Mail Document   Email               3rd-Party_00000461  3rd-Party_00000461      
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000462  3rd-Party_00000611      

The BegAtt value should be first value of main record (Record Type = Email) and EndAtt value should be last value of family (record Type = Email-Attachment). Below is the desired result:

Application             Record Type         Control Start       Control Stop        BegAtt              EndAtt
Outlook Mail Document   Email               3rd-Party_00000040  3rd-Party_00000040  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000041  3rd-Party_00000044  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000045  3rd-Party_00000045  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000046  3rd-Party_00000049  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000050  3rd-Party_00000181  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000182  3rd-Party_00000223  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000224  3rd-Party_00000243  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000244  3rd-Party_00000250  3rd-Party_00000040  3rd-Party_00000460
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000251  3rd-Party_00000460  3rd-Party_00000040  3rd-Party_00000460
Outlook Mail Document   Email               3rd-Party_00000461  3rd-Party_00000461  3rd-Party_00000461  3rd-Party_00000611
Adobe Acrobat Document  Email-Attachment    3rd-Party_00000462  3rd-Party_00000611  3rd-Party_00000461  3rd-Party_00000611

I tried below code which gets incorrect result.

SELECT [3rd-Party001_Main].[Record Type], Min([3rd-Party001_Main].[Control Start]) AS [MinOfControl Start], Max([3rd-Party001_Main].[Control Stop]) AS [MaxOfControl Stop]
FROM [3rd-Party001_Main]
GROUP BY [3rd-Party001_Main].[Record Type];

回答1:


Since there is no 'family' identifier, a query object alone will not accomplish what you want. Need VBA code looping through recordset checking when values change to determine start of 'family' group, set value of StartAtt field, and run an UPDATE action SQL to set EndAtt field. Based on data sample, consider:

Sub SetRange()
Dim rs As DAO.Recordset, strStart As String, strEnd As String, strApp As String
Set rs = CurrentDb.OpenRecordset("SELECT * FROM [3rd-Party001_Main] WHERE BegAtt Is Null ORDER BY ControlStart;")
Do
    If rs!Application Like "Outlook*" Then
        strStart = rs!ControlStart
    End If
    strEnd = rs!ControlStop
    rs.Edit
    rs!StartAtt = strStart
    rs.Update
    rs.MoveNext
    If Not rs.EOF Then strApp = rs!Application
    If (Not rs.EOF And strApp Like "Outlook*") Or rs.EOF Then 
        CurrentDb.Execute "UPDATE [3rd-Party001_Main] SET EndAtt = '" & strEnd & "' WHERE StartAtt='" & strStart & "'"
    End If
Loop Until rs.EOF
End Sub

Advise not to use spaces nor punctuation/special characters in naming convention.



来源:https://stackoverflow.com/questions/56261769/how-to-update-begatt-endatt-values-by-mdb-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!