Using an ADODB record set to perform a joined update query

早过忘川 提交于 2019-12-13 12:28:06

问题


In the following code, I would like to join ADODB record set 'rs3' to table 'tblValueChain10' and update 3 different columns based on the values extracted in the ADODB record set 'rs3'. Currently, the update query is not returning anything.

Dim st_Sql3 As String
Dim rs3 As ADODB.Recordset
Set rs3 = New ADODB.Recordset
Dim Max3 As Integer

rs3.Open "SELECT tblRisk05Holding.IDMacroProcesso01, tblRisk05Holding.Level01Risk, Max(tblRisk05Holding.ManualityStatus) AS MaxDiManualityStatus, Max(tblRisk05Holding.RiskProbabilityStatus) AS MaxDiRiskProbabilityStatus, Max(tblRisk05Holding.RiskExposureStatus) AS MaxDiRiskExposureStatus FROM tblRisk05Holding GROUP BY tblRisk05Holding.IDMacroProcesso01, tblRisk05Holding.Level01Risk", CurrentProject.Connection


st_Sql3 = "UPDATE tblValueChain10 INNER JOIN rs3 ON (tblValueChain10.IDMacroProcesso01 = tblRisk05Holding.IDMacroProcesso01) SET L1RiskManuality = " & rs3.Fields(2) & ", L1RiskProbability = " & rs3.Fields(3) & ", L1RiskGravity = " & rs3.Fields(4) & ""
Application.DoCmd.RunSQL (st_Sql2)

rs3.Close
Set rs3 = Nothing

回答1:


Access never allows you to use a recordset object as a data source in another query. It doesn't matter whether you have an ADO or DAO recordset; you can't do it. And the query type (SELECT, UPDATE, INSERT, etc.) also doesn't matter; you can't use a recordset object as a data source in any query type.

You might get a workable UPDATE by first saving your SELECT statement as a named query, qryRS3. Then revise the UPDATE to INNER JOIN tblValueChain10 to qryRS3. But I'm uncertain whether Access would consider that query to be updatable; the GROUP BY might cause Access to treat it as not updatable. You'll have to test to see.




回答2:


Although the goal you try to achieve using ADO Recordset in the way presented can't be done (as user @HansUp wrote), you could try the approach with updating your table tblValueChain10 using subquery taken from the rs3 recordset open call.

I hope that the following query :

UPDATE 
  tblValueChain10 
  INNER JOIN 
  (
    SELECT 
       tblRisk05Holding.IDMacroProcesso01, 
       tblRisk05Holding.Level01Risk, 
       Max(tblRisk05Holding.ManualityStatus) AS MaxDiManualityStatus, 
       Max(tblRisk05Holding.RiskProbabilityStatus) AS MaxDiRiskProbabilityStatus, 
       Max(tblRisk05Holding.RiskExposureStatus) AS MaxDiRiskExposureStatus 
    FROM 
       tblRisk05Holding 
    GROUP BY 
       tblRisk05Holding.IDMacroProcesso01, 
       tblRisk05Holding.Level01Risk
  ) AS qry_Risk05Holding
  ON (tblValueChain10.IDMacroProcesso01 = qry_Risk05Holding.IDMacroProcesso01) 
SET 
  tblValueChain10.L1RiskManuality = qry_Risk05Holding.MaxDiManualityStatus, 
  tblValueChain10.L1RiskProbability = qry_Risk05Holding.MaxDiRiskProbabilityStatus, 
  tblValueChain10.L1RiskGravity = qry_Risk05Holding.MaxDiRiskExposureStatus

could help you solve your problem.

You should be able to run the above SQL with the following code:

Dim st_Sql3 As String

st_Sql3 = " UPDATE tblValueChain10 INNER JOIN ( "

st_Sql3 = st_Sql3 & " SELECT  " _
    & " tblRisk05Holding.IDMacroProcesso01,  " _
    & " tblRisk05Holding.Level01Risk,  " _
    & " Max(tblRisk05Holding.ManualityStatus) AS MaxDiManualityStatus,  " _
    & " Max(tblRisk05Holding.RiskProbabilityStatus) AS MaxDiRiskProbabilityStatus,  " _
    & " Max(tblRisk05Holding.RiskExposureStatus) AS MaxDiRiskExposureStatus  "

st_Sql3 = st_Sql3 & " FROM  " _
    & " tblRisk05Holding  " _
    & " GROUP BY  " _
    & " tblRisk05Holding.IDMacroProcesso01,  " _
    & " tblRisk05Holding.Level01Risk "

st_Sql3 = st_Sql3 & " ) AS qry_Risk05Holding " _
  & " ON (tblValueChain10.IDMacroProcesso01 = qry_Risk05Holding.IDMacroProcesso01) " _
  & " SET " _
  & " tblValueChain10.L1RiskManuality = qry_Risk05Holding.MaxDiManualityStatus,  " _
  & " tblValueChain10.L1RiskProbability = qry_Risk05Holding.MaxDiRiskProbabilityStatus, " _
  & " tblValueChain10.L1RiskGravity = qry_Risk05Holding.MaxDiRiskExposureStatus "

Application.DoCmd.RunSQL (st_Sql3)

The query is being built-up in four stages because as far as I remember there is a restriction in VBA that there can't be too many line breaks _.

Please notice also that the JOIN used in the update query assumes that connecting two tables by IDMacroProcesso01 would ensure that the records are updated in the right way.

I can't check the solution in Acces right now but maybe it could somehow help you, at least a concept. If there were any errors, please write.




回答3:


You need to loop through the recordset to iteratively update by each record:

Do While NOT rs3.EOF

       st_Sql3 = "UPDATE tblValueChain10"_
                 & " INNER JOIN rs3 ON (tblValueChain10.IDMacroProcesso01 = tblRisk05Holding.IDMacroProcesso01)" _
                 & " SET L1RiskManuality = " & rs3.Fields(2) & ", L1RiskProbability = " & rs3.Fields(3) & "," _
                 & " L1RiskGravity = " & rs3.Fields(4) & "" 
       DoCmd.RunSQL (st_Sql3)

rs3.MoveNext
Loop

Also please note, your RunSQL () command is calling the incorrect SQL string.




回答4:


If no err appears, then try to place this code:

Set rs3 = New ADODB.Recordset

in form_load, as in,

private sub Form_load()
Set rs3 = New ADODB.Recordset
End Sub

note: this sample is in vb6.0. that's what I can do. Besides, check the version in references



来源:https://stackoverflow.com/questions/27977627/using-an-adodb-record-set-to-perform-a-joined-update-query

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