大家好我有一些代码可以从Excel更新Access字段,当我只想更新一个值(MonitorCapacity
)时,它可以正常工作,但我现在希望它更新多个值,例如一个名为MonitorSiteref
的字段,它将具有相同的WHERE
- 所以当Monitor_Siteref = '" & strID & "'"
我不确定从哪里开始时这是因为我对SQL比较新,所以我已经有了:
strID = ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 66).Value
cnn.Execute "UPDATE Tbl_Primary SET MonitorCapacity = " & _
IIf(ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 74).Value2 = "", "Null", _
"'" & ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 74).Value2 & "'") & _
" WHERE Monitor_Siteref = '" & strID & "'"`
下次修改:
cnn.Execute "UPDATE Tbl_Primary " & _
"SET MonitorCapacity = " & IIf(ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 74).Value2 = "", "Null", _
"'" & ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 74).Value2 & "'"), _
"Monitor_Permnumber = " & IIf(ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 73).Value2 = "", "Null", _
"'" & ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 73).Value2 & "'") & _
" WHERE Monitor_Siteref = '" & strID & "'"
lngRow = lngRow + 1
答案 0 :(得分:1)
标准SQL Update语法如下:
update <TABLE> set <COLUMN> = <VALUE>,
<COLUMN> = <VALUE> where <COND>;
所以我想你想用逗号分隔你的列,值tupels。
您也可能希望将逻辑置于语句创建之外。这将使它更具可读性。
选中此处以获取进一步说明:UPDATE Syntax
答案 1 :(得分:1)
我正在构建一个包含多列的更新语句,以使用VBA更新MSQL。这是我的简单代码。我正在解析函数中的变量来执行SQL命令:
Function GetUpdateTextSQL(PIC As String, Customer As String, DOB As Date,
Rank As String, Organization As String,
Status As String, Gender As String,
Religion As String, Hobby As String,
CreatedBy1 As String, CreatedOn1 As Date,
ChangedBy1 As String, ChangedOn1 As Date,
PeopleID As Integer) As String
Dim SQLStr As String
SQLStr = "UPDATE People" & _
"SET PIC = " & _
"'" & PIC & "', Customer = '" & Customer & "'," & _
"DOB = '" & Format(DOB, "yyyy-mm-dd") & "',_
Rank = '" & Rank & "'," & _
"Organization = '" & Organization & "',Status = '" &_
Status & "', Gender = '" & Gender & "'," & _
"Religion = '" & Religion & "', Hobby = '" & Hobby &_
"'," & _
"CreatedBy = '" & CreatedBy1 & "', CreatedOn = '" &_
CreatedOn1 & "'," & _
"ChangedBy = '" & ChangedBy1 & "', ChangedOn = '" &_
ChangedOn1 & "'" & _
"WHERE PeopleID = & PeopleID &;"
GetUpdateTextSQL = SQLStr
我得到的错误是“PIC附近的语法不正确”。怎么了?我该怎么办呢?
答案 2 :(得分:0)
首先,您确实应该将值作为预处理语句参数传递。为什么?阅读Bobby Tables的故事。
例如:
Dim siteref As String = _
ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 66).Value
Dim capacity As String = _
capacity = ThisWorkbook.Worksheets("Tbl_Primary").Cells(lngRow, 74).Value2
If capacity = "" Then capacity = Nothing
Dim stmt As New ADODB.Command
With stmt
.ActiveConnection = cnn
.CommandType = adCmdText
.Prepared = True
.CommandText = _
"UPDATE Tbl_Primary " & _
"SET MonitorCapacity = ? " & _
"WHERE Monitor_Siteref = ?"
.Parameters.Append .CreateParameter( _
"MonitorCapacity", _
adVarChar, _
adParamInput, _
Len(capacity), _
capacity _
)
.Parameters.Append .CreateParameter( _
"Monitor_Siteref", _
adVarChar, _
adParamInput, _
Len(siteref), _
siteref _
)
.Execute
End With
然后,要回答您的实际问题,只需在SET
子句中添加多个术语(以逗号分隔):
.CommandText = _
"UPDATE Tbl_Primary " & _
"SET MonitorCapacity = ?, " & _
" OtherColumn = ? " & _
"WHERE Monitor_Siteref = ?"
显然,您还需要在命令中添加其他参数。