I have this code below. I asked a non-related question about it yesterday and it was pointed out to me that it's wide open for SQL Injection. I did some research but I am a bit lost about what exactly to do here.
How should I rewrite this procedure to prevent SQL Injection possibility? Thanks. I would appreciate if you could point me in the right direction, not write the code entirely.
It's a code in MS Access VBA, using ADODB connection to SQL Server 2019.
This is my SQL Server module in VBA to make the code more readable:
Option Compare Database
Option Explicit
Private Const CONNECTION_STRING = "some random connection string"
Public Conn As ADODB.Connection
' ÚČEL FUNKCE: Spojení s SQL Server databází (bez DSN)
Public Function ConnectToServer() As String
On Error GoTo Catch
ConnectToServer = CONNECTION_STRING
Exit Function
Catch:
ConnectToServer = ""
MsgBox "GetDSNLessCnnString function" & vbCrLf & vbCrLf & "Error#: " _
& Err.Number & vbCrLf & vbCrLf & Err.Description
End Function
Public Sub Connect()
Set Conn = New ADODB.Connection
Conn.ConnectionString = ConnectToServer
Conn.Open
End Sub
Public Sub Exec(Command As String)
Conn.Execute Command
End Sub
Public Sub Disconnect()
Conn.Close
Set Conn = Nothing
End Sub
This is the main procedure:
Private Sub cmdShipOrder_Click()
Dim intShipmentID As Integer
On Error GoTo ErrHandler
If Me.ReservationStatus <> "ZBOŽÍ KOMPLETNĚ REZERVOVÁNO" Then
MsgBox "Nejprve je nutné do objednávky rezervovat hmotné zboží.", vbCritical + vbOKOnly, "Chyba"
Exit Sub
End If
If MsgBox("Bude vytvořena expedice a celá objednávka bude označena jako expedovaná. Pokračovat?", vbExclamation + vbYesNoCancel, "Upozornění") <> vbYes Then Exit Sub
Connect
Exec "INSERT INTO tbl1Shipments (CustomerID, ShippingMethodID, ShipToID, CarrierID, ShipmentCode, DateShipped) " & _
"VALUES (" & Me.CustomerID & ", " & _
Me.ShippingMethodID & ", " & _
Me.ShipToID & ", " & _
Me.CarrierID & ", " & _
"'" & Year(Date) & "EXP" & Format(DCount("*", "dbo_tbl1Shipments", "ShipmentCode LIKE '%" & Year(Date) & "EXP%'") + 1, "000") & "', " & _
"GETDATE())"
intShipmentID = DMax("ShipmentID", "dbo_tbl1Shipments", "CustomerID=" & Me.CustomerID)
Conn.BeginTrans
Exec "INSERT INTO tbl1ShipmentDetails (ShipmentID, SalesOrderDetailID, Quantity) " & _
"SELECT " & intShipmentID & ", SalesOrderDetailID, Quantity " & _
"FROM v_SalesOrderSub " & _
"WHERE SalesOrderID=" & Me.SalesOrderID
Exec "UPDATE A " & _
"SET ShipmentDetailID = B.ShipmentDetailID " & _
"FROM tbl1Units A JOIN v_ShipmentSub B ON A.SalesOrderDetailID = B.SalesOrderDetailID " & _
"WHERE B.ShipmentID = " & intShipmentID & " AND B.ProductTypeID <> 3"
Conn.CommitTrans
Disconnect
RequeryForm ("frmSalesOrderDetails")
RequeryForm ("frmSalesOrderList")
DoCmd.OpenForm "frmShipmentDetails", , , "ShipmentID=" & intShipmentID
Exit Sub
ErrHandler:
MsgBox "CHYBA: " & Err.Description
Conn.RollbackTrans
Exec "DELETE FROM tbl1Shipments WHERE ShipmentID=" & intShipmentID
Disconnect
End Sub