Heder_Santos
New Member
- Joined
- Jun 5, 2015
- Messages
- 12
Hello All,
I'm using a connection to query an external database. Basically, I'm selecting a few columns and filtering them by a list of contracts. This list will always vary in length, so I'm iterating through a single column in a worksheet and then concatenating each row in a single string variable (examples below), which I will later use in the SQL WHERE Clause (WHERE contract_number IN "concatenated_list").
When I concatenate up to 4 contracts, everything works and the SQL query runs perfectly, but when I concatenate more than that, it throws an error: "Run Time Error '5': Invalid Procedure Call or Argument".
Does anyone know why it's happening and how can I solve this? I'd really appreciate your help =)
Here are the procedures I'm using to generate the string and then passing it to the query in the next procedure.
Example of a String contained in "strContractList" who works fine:
'27-98477/17',
'27-05165/17',
'27-00427987/18',
'27-00430901/18'
Example of a String contained in "strContractList" who throws an error (just one more contract added...):
'27-98477/17',
'27-05165/17',
'27-00427987/18',
'27-00430901/18',
'27-19088/17'
Thanks!
Heder D. Santos
I'm using a connection to query an external database. Basically, I'm selecting a few columns and filtering them by a list of contracts. This list will always vary in length, so I'm iterating through a single column in a worksheet and then concatenating each row in a single string variable (examples below), which I will later use in the SQL WHERE Clause (WHERE contract_number IN "concatenated_list").
When I concatenate up to 4 contracts, everything works and the SQL query runs perfectly, but when I concatenate more than that, it throws an error: "Run Time Error '5': Invalid Procedure Call or Argument".
Does anyone know why it's happening and how can I solve this? I'd really appreciate your help =)
Here are the procedures I'm using to generate the string and then passing it to the query in the next procedure.
Code:
[COLOR="#0000CD"]Sub[/COLOR] CreateTargetContractsList()
[COLOR="#0000CD"]Dim[/COLOR] strContractList [COLOR="#0000CD"]As String[/COLOR]
[COLOR="#0000CD"]Dim[/COLOR] strActualContract [COLOR="#0000CD"]As String[/COLOR]
[COLOR="#0000CD"]Dim[/COLOR] dblLastUsedRowIndex [COLOR="#0000CD"]As Double[/COLOR]
[COLOR="#0000CD"]Dim[/COLOR] dblActualIterationRow [COLOR="#0000CD"]As Double[/COLOR]
dblActualIterationRow = 4
dblLastUsedRowIndex = wksLendingSQL.Cells(Rows.Count, 1).End(xlUp).Row
[COLOR="#0000CD"]Do While[/COLOR] wksLendingSQL.Cells(dblActualIterationRow, 1) <> [COLOR="#0000CD"]Empty[/COLOR]
strActualContract = "'" & wksLendingSQL.Cells(dblActualIterationRow, 1).Value & "'"
dblActualIterationRow = dblActualIterationRow + 1
[COLOR="#0000CD"]If Not[/COLOR] dblActualIterationRow > dblLastUsedRowIndex [COLOR="#0000CD"]Then[/COLOR]
strActualContract = strActualContract & ", " & Chr(13) & Chr(10)
[COLOR="#0000CD"]End If[/COLOR]
strContractList = strContractList & strActualContract
[COLOR="#0000CD"]Loop[/COLOR]
[COLOR="#0000CD"]Call [/COLOR]ConsultaLending(strContractList)
[COLOR="#0000CD"]End Sub[/COLOR]
[COLOR="#0000CD"]Sub[/COLOR] ConsultaLending(strContractList [COLOR="#0000CD"]As String[/COLOR])
[COLOR="#0000CD"]With[/COLOR] ActiveWorkbook.Connections("Consulta de DW").ODBCConnection
.BackgroundQuery = [COLOR="#0000CD"]True[/COLOR]
.CommandText = Array( _
"SELECT " & _
"nr_contrato, " & _
"parcela, " & _
"taxa_ap_dia, " & _
"taxa_cliente_ao_mes, " & _
"data_originacao, " & _
"data_vencimento, " & _
"data_pagto_cont, " & _
"valor_cedido, " & _
"valor as valor_pago " & _
"FROM " & _
"lending " & _
"WHERE " & _
[COLOR="#00FF00"]'Problem arises here... (in fact, when running the entire "CommandText"property)[/COLOR]
"nr_contrato IN (" & strContractList & ")")
.CommandType = xlCmdSql
.Connection = "ODBC;DSN=GBCerebro32Bits;"
.RefreshOnFileOpen = [COLOR="#0000CD"]False[/COLOR]
.SavePassword = [COLOR="#0000CD"]False[/COLOR]
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = [COLOR="#0000CD"]False[/COLOR]
[COLOR="#0000CD"]End With[/COLOR]
ActiveWorkbook.Connections("Consulta de DW").Refresh
[COLOR="#0000CD"]End Sub[/COLOR]
Example of a String contained in "strContractList" who works fine:
'27-98477/17',
'27-05165/17',
'27-00427987/18',
'27-00430901/18'
Example of a String contained in "strContractList" who throws an error (just one more contract added...):
'27-98477/17',
'27-05165/17',
'27-00427987/18',
'27-00430901/18',
'27-19088/17'
Thanks!
Heder D. Santos