使用 VBA 访问更新查询
Access Update Query using VBA
我有一个Table这样的
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
谷歌搜索后,我为此写了一段代码,代码成功运行,没有任何错误,但只有 SAP 列被 NULL,SAG 列没有被 NULL(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
有什么建议吗?
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
我有一个Table这样的
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
谷歌搜索后,我为此写了一段代码,代码成功运行,没有任何错误,但只有 SAP 列被 NULL,SAG 列没有被 NULL(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
有什么建议吗?
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