我正在使用Access 2016,但遇到错误,其中备忘录字段已更新,人们正在使用撇号,这会干扰代码更新。该代码非常有效,并且除非存在撇号,否则它将为特定值更新表。没有任何括号或括号可以解决此问题,以使文本中的撇号与代码隔离。我希望有一些建议,如果可能的话,我会允许我自己说。
Private Sub btnlledit_Click()
Dim SQL As String
SQL = "UPDATE tblll " & _
"SET [LLN] = '" & Forms!frmaddll!txtlln & "',[REF] = '" & Forms!frmaddll!txtllref & "',[TRN] = '" & Forms!frmaddll!txtlltr & "',[HN] = '" & Forms!frmaddll!txtllhull & "', [LL] = '" & Forms!frmaddll!txtll & "',[CA] = '" & Forms!frmaddll!txtllca & "',[CP] = '" & Forms!frmaddll!txtllcomponent & "',[LOC] = '" & Forms!frmaddll!txtllloc & "',[LFSE] = '" & Forms!frmaddll!txtlllfse & "',[FC] = '" & Forms!frmaddll!txtllfc & "' " & _
"WHERE [LLN] = '" & Forms!frmaddll!txtlln.Value & "';"
DoCmd.RunSQL SQL
DoCmd.Requery
Me.Refresh
End Sub
答案 0 :(得分:1)
我已经削减了您的SQL以便给您一个想法。
SQL的第一行是参数-注意第一行末尾的==
。
===
一种更好的方法是将查询的执行转移到另一个过程,并将所需的值传递给该过程:
var item
for (item in this.items) {
var a = this
var s = this.items[item].Stock
var sup = this.newStockSupplier
var m = this.items[item].Model
var exists = false
stockRef.orderByChild("Model").equalTo(m).once("value", snapshot => {
// Declare your code inside the callback function
if (snapshot.exists()) {
stockRef.orderByChild("Model").equalTo(this.items[item].Model).on("child_added", function (snapshot) {
console.log('Item exists in DB')
var stockItemRef = db.ref('stock/' + snapshot.key + '/Stock')
stockItemRef.transaction(function (currentStock) {
return currentStock + s
})
console.log('Updated Stock.')
})
} else {
console.log("Item doesn't exist in DB")
var newItem = new Object()
newItem.Model = a.items[item].Model
newItem.Stock = a.items[item].Stock
newItem.Supplier = a.newStockSupplier
stockRef.push(newItem)
console.log('Added new product')
}
});
}
然后您可以通过单击按钮来调用此过程:
;
或者从其他地方获取,并将您的价值从其他地方获取:
Private Sub btnlledit_Click()
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.CreateQueryDef("", _
"PARAMETERS LLN_Value TEXT(255), REF_Value TEXT(255), TRN_Value TEXT(255); " & _
"UPDATE tblll " & _
"SET LLN = LLN_Value, REF = REF_Value, TRN = TRN_Value " & _
"WHERE LLN = LLN_Value")
With qdf
.Parameters("LLN_Value") = Forms!frmaddll!txtlln
.Parameters("REF_Value") = Forms!frmaddll!txtllref
.Parameters("TRN_Value") = Forms!frmaddll!txtlltr
.Execute
End With
End Sub
答案 1 :(得分:1)
我说了一会儿,但还有其他内容(+在VBA中写这是一种折磨):
Dim SQL As String
SQL = "UPDATE tblll " & _
"SET [REF] = @ref,[TRN] = @trn,[HN] = @hn" & _
", [LL] = @ll,[CA] = @ca,[CP] = @cp,[LOC] = @loc " & _
", [LFSE] = @lfse, [FC] = @fc" & _
"WHERE [LLN] = @lln";"
Dim oConnection As ADODB.Connection
Dim oCommand As ADODB.Command
Set oConnection = New ADODB.Connection
Set oCommand = New ADODB.Command
oConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\MyFolder\MyData.accdb;"
oConnection.Open
oCommand.ActiveConnection = oConnection
oCommand.CommandText = SQL
oCommand.Parameters.Append oCommand.CreateParameter("@ref", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@trn", adVarChar, adParamInput, 100)
oCommand.Parameters.Append oCommand.CreateParameter("@hn", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@ll", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@ca", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@cp", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@loc", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@lfse", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@fc", adInteger)
oCommand.Parameters.Append oCommand.CreateParameter("@lln", adInteger)
oCommand.Parameters("@ref" ).Value = Forms!frmaddll!txtref.Value
oCommand.Parameters("@trn" ).Value = Forms!frmaddll!txttrn.Value
oCommand.Parameters("@hn" ).Value = Forms!frmaddll!txthn.Value
oCommand.Parameters("@ll" ).Value = Forms!frmaddll!txtll.Value
oCommand.Parameters("@ca" ).Value = Forms!frmaddll!txtca.Value
oCommand.Parameters("@cp" ).Value = Forms!frmaddll!txtcp.Value
oCommand.Parameters("@loc" ).Value = Forms!frmaddll!txtloc.Value
oCommand.Parameters("@lfse").Value = Forms!frmaddll!txtlfse.Value
oCommand.Parameters("@fc" ).Value = Forms!frmaddll!txtfc.Value
oCommand.Parameters("@lln" ).Value = Forms!frmaddll!txtlln.Value
oCmd.Execute
此代码不是访问代码,而是VBA(可以在任何VBA环境中执行,例如Excel,Word ...)。 由于我不知道您的字段,因此参数类型仅用于采样。这里重要的是,您必须按照与查询字符串中出现的顺序相同的顺序定义参数。附加参数后,您可以按自己喜欢的顺序随意设置它们的值(这是驱动程序的限制,我认为参数不是命名的而是位置的)。
注意:我删除了初始LLN =,因为您正在搜索它并将其设置为相同的值(IOW不变)。
答案 2 :(得分:0)
将单引号加倍即可转义,就像您在示例中向我们展示的一样。以下SQL说明了此功能。
顺便说一句,请使用SQL参数代替这些内联SQL注入。
尝试如下:
Private Sub btnlledit_Click()
Dim SQL As String
SQL = "UPDATE tblll " & _
"SET [LLN] = '" & Forms!frmaddll!txtlln & "',[REF] = '" & Forms!frmaddll!txtllref & "',[TRN] = '" & Forms!frmaddll!txtlltr & "',[HN] = '" & Forms!frmaddll!txtllhull & "', [LL] = '" & Forms!frmaddll!txtll & "',[CA] = '" & Forms!frmaddll!txtllca & "',[CP] = '" & Forms!frmaddll!txtllcomponent & "',[LOC] = '" & Forms!frmaddll!txtllloc & "',[LFSE] = '" & Forms!frmaddll!txtlllfse & "',[FC] = '" & Forms!frmaddll!txtllfc & "' " & _
"WHERE [LLN] = '" & Forms!frmaddll!txtlln.Value & "';"
Replace (SQL, "'", "''")
DoCmd.RunSQL SQL
DoCmd.Requery
Me.Refresh
End Sub