How to find duplicate entries in table (SQL Server) in VBA [duplicate] - sql-server

This question already has answers here:
INSERT VALUES WHERE NOT EXISTS
(6 answers)
Closed 9 months ago.
I am inserting records into a SQL Server table from an Excel sheet through VBA code with the same column headers as in the SQL Server table.
There are 4 columns in the SQL Server table which should not have values to be inserted that already exist. Those columns on the table are SF Payroll ID, Unity Payroll Element Name, ICP/LMC Element Code & ICP/LMC Element Name.
Every time each row is being inserted from the Excel file the code needs to check, that inserting values for columns SF Payroll ID + Unity Payroll Element Name + ICP/LMC Element Code + ICP/LMC Element Name does not already exist.
If the row contains the same value that already exists on the column then that row should be rejected from being uploaded to the SQL Server table and the rest of the rows should be uploaded.
Below is my code which uploads the records into the SQL Server table. Kindly suggest how to code in VBA to validate this.
Private Sub PushToDB_Click()
Dim conn As ADODB.connection
Dim connString As String
connString = "Provider=SQLOLEDB;Server=DEEPAKSQL;Database=Workload;User Id=DHARMA;Password= ********"
Set conn = New ADODB.connection
conn.Open connString
Dim rowCountsheet As Integer
rowCountsheet = ActiveSheet.UsedRange.Rows.Count
Dim tempisheet As Integer
tempisheet = 2
Dim shsheet As Worksheet
Set shsheet = ThisWorkbook.Worksheets("Data copied")
On Error GoTo CleanFail
conn.BeginTrans
Dim rowCount As Integer
rowCount = ActiveSheet.UsedRange.Rows.Count
Dim tempi As Integer
tempi = 2
Dim sql As String
sql = " INSERT INTO [dbo].[ELEMENT_INFO] ([DB_Upload_Action_Key],[Account_Client_Customer_Name],[SF_Account_ID],[Payroll],[SF_Payroll_ID],[Record_Creation_Date],[Record_Creation_Time_At],[Record_Creation_Guardian_Name],[Record_Last_Modified_Date],[Record_Last_Modified_Time_At],[Record_Last_Modified_Guardian_Name]," _
& "[Unity_Payroll_Element_Name],[Element_Description],[Element_Status],[Element_Type]," _
& "[Element_Input_Classification],[Element_Input_Type],[Element_Frequency]," _
& "[ICP_Or_LMC_Element_Code],[ICP_Or_LMC_Element_Name],[Source_Of_Data_Input_HCM_Integration_EUT_T_A_Other],[GL_Code_Debit],[GL_Code_Credit],[GL_Account_Name],[Comments])" & _
"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Data copied")
Dim pctDone As Single
Dim iLabelWidth As Integer
iLabelWidth = 240
Do Until tempi = rowCount + 1
mapping_upload.Hide
frmProgressForm.Show
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = sql
cmd.Parameters.Append cmd.CreateParameter("DB_Upload_Action_Key", adVarChar, adParamInput, 100, sh.Cells(tempi, 1).Value)
cmd.Parameters.Append cmd.CreateParameter("Account_Client_Customer_Name", adVarChar, adParamInput, 250, sh.Cells(tempi, 2).Value)
cmd.Parameters.Append cmd.CreateParameter("SF_Account_ID", adVarChar, adParamInput, 250, sh.Cells(tempi, 3).Value)
cmd.Parameters.Append cmd.CreateParameter("SF_Payroll_ID", adVarChar, adParamInput, 250, sh.Cells(tempi, 6).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Creation_Date", adDBDate, adParamInput, 100, dateLabel.Caption) 'sh.Cells(tempi, 10).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Creation_Time_At", adDBTime, adParamInput, 100, timeLabel.Caption) 'sh.Cells(tempi, 11).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Creation_Guardian_Name", adVarChar, adParamInput, 100, TextBox1.Text) 'sh.Cells(tempi, 12).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Last_Modified_Date", adDBDate, adParamInput, 100, dateLabel.Caption) 'sh.Cells(tempi, 13).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Last_Modified_Time_At", adDBTime, adParamInput, 100, timeLabel.Caption) 'sh.Cells(tempi, 14).Value)
cmd.Parameters.Append cmd.CreateParameter("Record_Last_Modified_Guardian_Name", adVarChar, adParamInput, 100, TextBox1.Text) 'sh.Cells(tempi, 15).Value)
cmd.Parameters.Append cmd.CreateParameter("Unity_Payroll_Element_Name", adVarChar, adParamInput, 3500, sh.Cells(tempi, 16).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Description", adVarChar, adParamInput, 5000, sh.Cells(tempi, 17).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Status", adVarChar, adParamInput, 500, sh.Cells(tempi, 18).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Type", adVarChar, adParamInput, 5000, sh.Cells(tempi, 19).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Input_Classification", adVarChar, adParamInput, 5000, sh.Cells(tempi, 32).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Input_Type", adVarChar, adParamInput, 5000, sh.Cells(tempi, 33).Value)
cmd.Parameters.Append cmd.CreateParameter("Element_Frequency", adVarChar, adParamInput, 5000, sh.Cells(tempi, 39).Value)
cmd.Parameters.Append cmd.CreateParameter("ICP_Or_LMC_Element_Code", adVarChar, adParamInput, 5000, sh.Cells(tempi, 40).Value)
cmd.Parameters.Append cmd.CreateParameter("ICP_Or_LMC_Element_Name", adVarChar, adParamInput, 5000, sh.Cells(tempi, 41).Value)
cmd.Parameters.Append cmd.CreateParameter("Source_Of_Data_Input_HCM_Integration_EUT_T_A_Other", adVarChar, adParamInput, 5000, sh.Cells(tempi, 42).Value)
cmd.Parameters.Append cmd.CreateParameter("GL_Code_Debit", adVarChar, adParamInput, 5000, sh.Cells(tempi, 43).Value)
cmd.Parameters.Append cmd.CreateParameter("GL_Code_Credit", adVarChar, adParamInput, 5000, sh.Cells(tempi, 44).Value)
cmd.Parameters.Append cmd.CreateParameter("GL_Account_Name", adVarChar, adParamInput, 5000, sh.Cells(tempi, 45).Value)
cmd.Parameters.Append cmd.CreateParameter("Comments", adVarChar, adParamInput, 8000, sh.Cells(tempi, 50).Value)
cmd.Execute
tempi = tempi + 1
pctDone = (tempi - 1) / rowCount
frmProgressForm.lblProgress.Width = iLabelWidth * pctDone
frmProgressForm.FrameProgress.Caption = Format(pctDone, "0%")
DoEvents
Loop
Unload frmProgressForm
MsgBox "Data Loaded Successfully to T-1 DataBase", vbInformation, "SDD04 - Commit Transaction, Okay!"
mapping_upload.Show
conn.CommitTrans
CleanExit:
conn.Close
Exit Sub
CleanFail:
conn.RollbackTrans
MsgBox "Input file error either value exceeds or having wrong type. Transaction was rolled back. " & Err.Description, vbCritical, "SDD04 - Input error"
Unload frmProgressForm
Debug.Print Err.Number, Err.Description
Resume CleanExit
End Sub

You are using a loop to reach every row, and building the INSERT INTO statement. That's right, but I don't see any code trying to check what you need: If that row already exists before inserting it.
So, what you need is to check, for every row, if it exists in your SQL table before inserting it. As you have to do it for every row, think that the first thing you should do in the loop before build the INSERT statement, is to build the SELECT statement to do the check, and then, depending on the result, you will do the INSERT or not.
I suppose you can build a SELECT statement from VBA if you are able to build an INSERT statement. Otherwise you can ask for help about it.
ADDED: 2022-05-20
Here is a code snippet that may help you with the SELECT statement.
Private Sub Test()
Dim cnn As New ADODB.Connection
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim ConnectionString As String
Dim strSql As String
ConnectionString = "Provider=SQLOLEDB;Server=DEEPAKSQL;Database=Workload;User Id=DHARMA;Password= ********"
strSql = "SELECT * FROM [dbo].[ELEMENT_INFO] WHERE [SF Payroll ID]=" & Value1 & " AND [Unity Payroll Element Name]='" & Value2 & "' AND ..."
rst.CursorLocation = adUseClient ' Client-side cursor
rst.Open strSql, cnn
If Not rst.RecordCount > 0 Then
'Not Exists -> Do the Insert
Else
'Exists -> Don't do the Insert
End If
rst.Close
Set rst = Nothing
End Sub

Related

VB6 - Call Stored Procedure with recordset output on SQLServer

I'm supposed to run a procedure that results in a recordset. I found some code online.
I used this code:
Dim cnAdoDB As ADODB.Connection
Dim StrConnection As String
Dim ACmd As ADODB.Command
Dim TempRS As ADODB.Recordset
Dim RecordNumber As Long
Set cnAdoDB = New ADODB.Connection
StrConnection = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=xxx;Password=xxx;Initial Catalog=DB-Name;Data Source=xxx.xxx.xxx.xxx"
cnAdoDB.ConnectionString = StrConnection
cnAdoDB.Open
cnAdoDB.CursorLocation = adUseClient
Set ACmd = New ADODB.Command
ACmd.CommandType = adCmdStoredProc
ACmd.CommandText = "[dbo].[st_log_GetDatiTracciabilitaLottoArticolo]"
ACmd.Parameters.Append ACmd.CreateParameter("#ReturnValue", adInteger, adParamReturnValue)
ACmd.Parameters.Append ACmd.CreateParameter("#vLotto", adVarChar, adParamInput, 20, "2022085001")
ACmd.Parameters.Append ACmd.CreateParameter("#vCodArticolo", adVarChar, adParamInput, 32, "A014L628")
ACmd.Parameters.Append ACmd.CreateParameter("#vDataOraPompa", adDate, adParamInput, , Null)
Set ACmd.ActiveConnection = cnAdoDB
Set TempRS = ACmd.Execute(RecordNumber)`
ADODB.Recordset RecordCount property always returns -1:
The TempRS recordset is empty but the "RecordNumber" variable has a value of 2 as the result records really are.
Make it a habit to never rely on the .RecordCount property. It's too situational. From the help:
Use the RecordCount property to find out how many records are in a
Recordset object. The property returns -1 when ADO cannot determine
the number of records or if the provider or cursor type does not
support RecordCount. Reading the RecordCount property on a closed
Recordset causes an error.
Your code also has errors:
ACmd.Parameters.Append ACmd.CreateParameter("#vLotto", adVarChar, adParamInput, 20, "2022085001")
ACmd.Parameters.Append ACmd.CreateParameter("#vCodArticolo", adVarChar, adParamInput, 32, "A014L628")
Instead of "20" and "32" (presumably the defined size), the Parameter object expects that actual length of the passed data, i.e.
ACmd.Parameters.Append ACmd.CreateParameter("#vLotto", adVarChar, adParamInput, Len("2022085001"), "2022085001")
ACmd.Parameters.Append ACmd.CreateParameter("#vCodArticolo", adVarChar, adParamInput, Len("A014L628"), "A014L628")

Passing empty cells to sql-server shows "0" instead of NULL

I'm passing values from excel to sql-server via stored procedure.
There are two columns in excel in wich the cells contain integers or are empty.
But in sql-server, the empty cells from one column are shown as NULL, and the empty cells from the other are shown as "0". Why is that? Both variables are declared as integers in vba, and in sql.
What do I have to do to get everywhere NULL if the cell is empty?
This is part of the vba-code:
Dim rst As New ADODB.Recordset
Dim cmd As ADODB.Command
Dim query As String
Global ktghnev As String
Global ktghID As Integer
Dim elsosor As Integer
Dim pt, bank As Integer
Dim fizmod As String
Dim honap As String
With ActiveSheet
elsosor = 3
Do Until .Cells(elsosor, 1) = ""
nevID = .Cells(elsosor, 1)
pt = .Cells(elsosor, 4)
bank = .Cells(elsosor, 5)
If IsEmpty(Range("D" & elsosor)) = False Or IsEmpty(Range("E" & elsosor)) = False Then
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "EURfeltoltes"
cmd.Parameters.Append cmd.CreateParameter("#nevID", adInteger, adParamInput, , nevID)
cmd.Parameters.Append cmd.CreateParameter("#ktghID", adInteger, adParamInput, , ktghID)
cmd.Parameters.Append cmd.CreateParameter("#honap", adVarChar, adParamInput, 10, honap)
cmd.Parameters.Append cmd.CreateParameter("#pt", adInteger, adParamInput, , pt)
cmd.Parameters.Append cmd.CreateParameter("#bank", adInteger, adParamInput, , bank)
cmd.Execute
End If
elsosor = elsosor + 1
Loop
End With
This is the stored procedure:
ALTER PROCEDURE [dbo].[EURfeltoltes]
#nevID int = null,
#ktghID int = null,
#honap nvarchar(10) = null,
#pt int = null,
#bank int = null
AS
BEGIN
SET NOCOUNT ON;
insert into bertabla (nevID, ktghelyID, honap, eurpt, eurbank) values (#nevID, #ktghID, #honap, #pt, #bank)
END

ASP Classic vbScript - Execute SQL stored procedure with recordset and returned values

I am trying to execute a stored procedure with input parameters, a recordset returned and a return parameter. When I go to the asp page, I get a 500 error. The stored procedure runs as expected in SQL Server by itself. Can you tell where the errors are in the below code?
' The following line must be changed to reflect your data source info
Conn.Open "Data Source=path; Initial Catalog=database;","username","password"
set cmd = Server.CreateObject("ADODB.Command")
set cmd.ActiveConnection = Conn
' Specify the name of the stored procedure you wish to call
cmd.CommandText = "ap_RF_085"
cmd.CommandType = adCmdStoredProc
' Use the values from the table in the following lines to define parameters
cmd.Parameters.Append cmd.CreateParameter("#RETURN_VALUE", adInteger, adParamReturnValue)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Loc", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Zone", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Width_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Height_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Depth_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, 1)
cmd.Parameters.Append cmd.CreateParameter("#Site", adVarChar, adParamInput, 8)
cmd.Parameters("#Whse_Loc") = "4H"
cmd.Parameters("#Whse_Zone") = ""
cmd.Parameters("#Width_Char") = ""
cmd.Parameters("#Height_Char") = ""
cmd.Parameters("#Depth_Char") = ""
cmd.Parameters("#Type") = ""
cmd.Parameters("#Site") = ""
cmd.Parameters("#Whse_Loc") = ""
cmd.Parameters("#Whse_Loc") = "MZ"
SET rs = cmd.Execute
returned = cmd.parameters("#RETURN_VALUE").value
RESPONSE.WRITE(returned)
Thanks for your help!
I updated my code to the suggestion below. I am still getting a 500 error. Also the tab in IE11 show "Waiting on [serverName]". Can you tell why from the code what is not being passed correctly?
cmd.Parameters.Append cmd.CreateParameter("#RETURN_VALUE", adInteger, adParamReturnValue)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Loc", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Whse_Zone", adVarChar, adParamInput, 6)
cmd.Parameters.Append cmd.CreateParameter("#Width_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Height_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Depth_Char", adVarChar, adParamInput, 5)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, 1)
cmd.Parameters.Append cmd.CreateParameter("#Site", adVarChar, adParamInput, 8)
cmd.Parameters("#Whse_Loc") = "4H"
cmd.Parameters("#Whse_Zone") = ""
cmd.Parameters("#Width_Char") = ""
cmd.Parameters("#Height_Char") = ""
cmd.Parameters("#Depth_Char") = ""
cmd.Parameters("#Type") = ""
cmd.Parameters("#Site") = ""
cmd.Parameters("#Whse_Loc") = ""
cmd.Parameters("#Whse_Loc") = "MZ"
SET rs = cmd.Execute
IF NOT rs.EOF THEN data = rs.GETROWS()
CALL rs.CLOSE()
returned = cmd.parameters("#RETURN_VALUE").value
Please, try this: Procedure
DECLARE #RETURN_VALUE XXXXXX = NULL
OUTPUT ASP file SET cmd = server.CreateObject("ADODB.command")
cmd.ActiveConnection = conn cmd.CommandText = "" cmd.CommandType = 4 cmd.CommandTimeout = 3000 cmd.Parameters.refresh
cmd.Execute returned = cmd.Parameters.item("#RETURN_VALUE")
& "" RESPONSE.WRITE(returned)
cmd.close
Set cmd = Nothing

Write Multiple Rows in EXCEL (VB) to SQL Table

Would like to be able to write all rows that have data in them to SQL table, with the below I can only export the 1st row. If I add more rows of data and click the Export Button I will only send the first row. Can some one help me script my request?
Sub Button1_Click()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim strSQL As String
strSQL = "INSERT INTO dbo.TimeLog" & _
"(EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) " & _
"VALUES (?,?,?,?,?,?,?);"
Set conn = New ADODB.Connection
conn.Open "Provider=SQLOLEDB;Data Source=db\db1;Initial Catalog=Table1;Integrated Security=SSPI;"
'Skip the header row
iRowNo = 2
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = strSQL
iRowNo = 2
With Sheets("Sheet1")
'Loop until empty cell in EventDate
Do Until .Cells(iRowNo, 1) = ""
cmd.Parameters.Append _
cmd.CreateParameter("pEventDate", adVarChar, adParamInput, 8, .Cells(iRowNo, 1))
cmd.Parameters.Append _
cmd.CreateParameter("pID", adInteger, adParamInput, , .Cells(iRowNo, 2))
cmd.Parameters.Append _
cmd.CreateParameter("pDeptCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 3))
cmd.Parameters.Append _
cmd.CreateParameter("pOpCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 4))
cmd.Parameters.Append _
cmd.CreateParameter("pStartTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 5))
cmd.Parameters.Append _
cmd.CreateParameter("pFinishTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 6))
cmd.Parameters.Append _
cmd.CreateParameter("pUnits", adInteger, adParamInput, , .Cells(iRowNo, 7))
cmd.Execute
iRowNo = iRowNo + 20
Loop
MsgBox "Success!"
End With
conn.Close
Set conn = Nothing
End Sub

Error when using Parameters in ADODB

Dim strSQL As String
strSQL = "INSERT INTO [" & AccountCode & "].[Orders] ( OrderID,OrderDate,BarUTC,OrderUTC,Exchange,Symbol,OrderSignedAmount,OrderPrice,Type,TargetPosition,AccountIdent,Status)" _
& " VALUES ( #OrderID,#OrderDate,#BarUTC,#OrderUTC,#Exchange,#Symbol,#OrderSignedAmount,#OrderPrice,#Type,#TargetPosition,#AccountIdent,#Status)"
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = objMyConn
cmd.CommandText = strSQL
cmd.CommandType = adCmdText
cmd.NamedParameters = True
SeqNum = SeqNum + 1
AccountIdent = AccountCode + ":" + ModelCode
cmd.Parameters.Append cmd.CreateParameter("#OrderID", adInteger, adParamInput, -1, SeqNum)
cmd.Parameters.Append cmd.CreateParameter("#OrderDate", adDate, adParamInput, 0, Date)
cmd.Parameters.Append cmd.CreateParameter("#BarUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#OrderUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#Exchange", adVarChar, adParamInput, -1, "Future")
cmd.Parameters.Append cmd.CreateParameter("#Symbol", adVarChar, adParamInput, -1, Symbol)
cmd.Parameters.Append cmd.CreateParameter("#OrderSignedAmount", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#OrderPrice", adDouble, adParamInput, , -1)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, -1, "MARKETIOC")
cmd.Parameters.Append cmd.CreateParameter("#TargetPosition", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#AccountIdent", adVarChar, adParamInput, -1, AccountIdent)
cmd.Parameters.Append cmd.CreateParameter("#Status", adVarChar, adParamInput, -1, "NEW")
cmd.Execute
This gives me a "Must declare the scalar variable error "#OrderID"
Why?
You can't name your parameters in the SQL command text as you've tried to do.
If you change all of them simply to the "?" placeholder then your code should work, e.g.
Dim strSQL As String
strSQL = "INSERT INTO [" & AccountCode & "].[Orders] ( OrderID,OrderDate,BarUTC,OrderUTC,Exchange,Symbol,OrderSignedAmount,OrderPrice,Type,TargetPosition,AccountIdent,Status)" _
& " VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?)"
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = objMyConn
cmd.CommandText = strSQL
cmd.CommandType = adCmdText
cmd.NamedParameters = True
SeqNum = SeqNum + 1
AccountIdent = AccountCode + ":" + ModelCode
cmd.Parameters.Append cmd.CreateParameter("#OrderID", adInteger, adParamInput, -1, SeqNum)
cmd.Parameters.Append cmd.CreateParameter("#OrderDate", adDate, adParamInput, 0, Date)
cmd.Parameters.Append cmd.CreateParameter("#BarUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#OrderUTC", adDBTimeStamp, adParamInput, 0, Time)
cmd.Parameters.Append cmd.CreateParameter("#Exchange", adVarChar, adParamInput, -1, "Future")
cmd.Parameters.Append cmd.CreateParameter("#Symbol", adVarChar, adParamInput, -1, Symbol)
cmd.Parameters.Append cmd.CreateParameter("#OrderSignedAmount", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#OrderPrice", adDouble, adParamInput, , -1)
cmd.Parameters.Append cmd.CreateParameter("#Type", adVarChar, adParamInput, -1, "MARKETIOC")
cmd.Parameters.Append cmd.CreateParameter("#TargetPosition", adInteger, adParamInput, , TargetPosition)
cmd.Parameters.Append cmd.CreateParameter("#AccountIdent", adVarChar, adParamInput, -1, AccountIdent)
cmd.Parameters.Append cmd.CreateParameter("#Status", adVarChar, adParamInput, -1, "NEW")
cmd.Execute
At that point it doesn't matter what you call them when you create the command parameters. Take note that the parameters are passed by ordinal position not by name!
If you want to name the parameters in your SQL code nicely then you should look into creating a stored procedure, although your dynamic schema name would make that tricky.
You could possibly also use sp_executesql. https://msdn.microsoft.com/en-us/library/ms188001.aspx

Resources