使用VBA访问更新查询

时间:2017-06-22 19:38:14

标签: ms-access access-vba ms-access-2013

我有一个像这样的表

SNo Block   SAP SAG BAP BAG DEP DEG
1   600403  1   3   5   4       
2   600405  1   3   1   3   1   1
3   600407  3   1           2   4
4   600409                  3   1
5   600410  1   3   2   5   1   3
6   600413  1   4           1   3

我希望SAP和SAG的单元格为空,其中SAP = 1且SAG = 3,并且BAP和BAG的单元格为空,其中BAP = 1且BAG = 3,对DEP和DEG来说是明智的,我期待结果如下所示

SNo Block   SAP SAG BAP BAG DEP DEG
1   600403          5   4       
2   600405                  1   1
3   600407  3   1           2   4
4   600409                  3   1
5   600410          2   5       
6   600413  1   4               

在googling之后,我为此编写了一个代码,代码运行成功,没有任何错误,但只有SAP列变为NULL并且SAG列未归零(SAG docmd的第二个查询不起作用) )!

以下是我的VBA,抱歉,我是Access VBA的新手!

Private Sub VbaModule()

Dim db As DAO.Database
Dim rs As Recordset
Dim sSQL As String
Dim sSQL1 As String

Set db = CurrentDb()
Set rs = db.OpenRecordset("T05_Pr2_Null_Not_In_Rem")

sSQL = "UPDATE T05_Pr2_Null_Not_In_Rem SET SAP = NULL " & _
     " WHERE (SAP = 1 AND SAG = 3)"
     DoCmd.RunSQL sSQL

sSQL = "UPDATE T05_Pr2_Null_Not_In_Rem SET SAG = NULL " & _
     " WHERE (SAP = 1 AND SAG = 3)"
     DoCmd.RunSQL sSQL

rs.Close
Set rs = Nothing
db.Close

End Sub

有任何建议吗?

1 个答案:

答案 0 :(得分:2)

  Dim strSQL As String

  strSQL = "UPDATE T05_Pr2_Null_Not_In_Rem " & _
           "SET " & _
           "  SAP = NULL, " & 
           "  SAG = NULL " & _
           "WHERE SAP = 1 AND SAG = 3;"
  CurrentDb.Execute strSQL, dbFailOnError

  strSQL = "UPDATE T05_Pr2_Null_Not_In_Rem " & _
           "SET " & _
           "  BAP = NULL, " & 
           "  BAG = NULL " & _
           "WHERE BAP = 1 AND BAG = 3;"
  CurrentDb.Execute strSQL, dbFailOnError

  strSQL = "UPDATE T05_Pr2_Null_Not_In_Rem " & _
           "SET " & _
           "  DEP = NULL, " & 
           "  DEG = NULL " & _
           "WHERE DEP = 1 AND DEG = 3;"
  CurrentDb.Execute strSQL, dbFailOnError