简体   繁体   中英

MS Access parameterized passthrough query

I've got a small problem getting a query with a parameter to work in a MS Access DB I'm building. I can't get the formatting/synthax right when the parameter is in number form.

Here's the code:

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

The value from txtdepno to be send through the parameter is a number, I've tried a few different approaches with no luck.

Here's another similar code that works but this time the parameter is a string:

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

String concatenation is not parametrization. However, if you're using string concatenation, you can solve this pretty easily. Just omit the single quotes, and that way Oracle will treat them as numbers. With the single quotes, they're treated as strings:

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

If you actually wanted to build a parameterized query, it would look something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM