我在使用参数进行查询时遇到了一个小问题,无法在我正在构建的MS Access DB中工作。当参数为数字形式时,我无法正确获取格式/ synthax。
以下是代码:
Private Sub Command15_Click()
Dim conn As ADODB.Connection
Dim strConnection As String
strConnection = "Driver={Microsoft ODBC for Oracle};Server=SERVER;Uid=USER_ID;Pwd=PASSWORD;"
Set conn = New ADODB.Connection
conn.Open strConnection
conn.Execute " UPDATE L2000.DEPARTURE_STATUS DS " & _
" SET DS.DEPARTURE_STATUS = '" & Forms!frmDepartureStatus.txtdeparturestatus & "' " & _
" WHERE DS.DEPARTURE_NO = " & Forms!frmDepartureStatus.txtdepno
conn.Close
Set conn = Nothing
End Sub
通过参数发送的txtdepno的值是一个数字,我尝试了几种不同的方法,没有运气。
这是另一个类似的代码,但这次参数是一个字符串:
Private Sub Command16_Click()
Dim conn As ADODB.Connection
Dim strConnection As String
strConnection = "Driver={Microsoft ODBC for Oracle};Server=SERVER;Uid=USER_ID;Pwd=PASSWORD;"
Set conn = New ADODB.Connection
conn.Open strConnection
conn.Execute " UPDATE L2000.USERS U " & _
" SET U.STORER_ID = '" & Forms!EXAMPLE.txtInput2 & "'" & _
" WHERE U.USER_ID = '" & Forms!EXAMPLE.txtInput & "'"
conn.Close
Set conn = Nothing
End Sub
答案 0 :(得分:0)
字符串连接不是参数化。但是,如果您正在使用字符串连接,则可以非常轻松地解决此问题。只需省略单引号,这样Oracle就会将它们视为数字。使用单引号,它们被视为字符串:
Private Sub Command16_Click()
Dim conn As ADODB.Connection
Dim strConnection As String
strConnection = "Driver={Microsoft ODBC for Oracle};Server=SERVER;Uid=USER_ID;Pwd=PASSWORD;"
Set conn = New ADODB.Connection
conn.Open strConnection
conn.Execute " UPDATE L2000.USERS U " & _
" SET U.STORER_ID = " & Forms!EXAMPLE.txtInput2 & _
" WHERE U.USER_ID = " & Forms!EXAMPLE.txtInput
conn.Close
Set conn = Nothing
End Sub
如果你真的想构建一个参数化查询,它看起来像这样:
Private Sub Command16_Click()
Dim conn As ADODB.Connection
Dim strConnection As String
strConnection = "Driver={Microsoft ODBC for Oracle};Server=SERVER;Uid=USER_ID;Pwd=PASSWORD;"
Set conn = New ADODB.Connection
conn.Open strConnection
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandText = "UPDATE L2000.USERS U " & _
" SET U.STORER_ID = ?"
" WHERE U.USER_ID = ?"
cmd.Parameters.Append cmd.CreateParameter(, adInteger,adParamInput, 5, Forms!EXAMPLE.txtInput2 )
cmd.Parameters.Append cmd.CreateParameter(, adInteger,adParamInput, 5, Forms!EXAMPLE.txtInput )
cmd.Execute
conn.Close
Set conn = Nothing
End Sub