VBA Type Mismatch Error on passing long String to SQL

neb255

Board Regular
Joined
Sep 14, 2011
Messages
64
Hi,

i have the below macro which takes a range of cells in a single column and concatenates the values into a single string. The string is then passed via an ODBC connection to SQL to be used as the variables the "Where" statement.

My problem seems to be related to how long the string i am trying to pass is. The set of values which i am trying to pass is: (These are the values referenced in the macro for Column A on Sheets("Accounts")) [TABLE="width: 186"]
<tbody>[TR]
[TD]Z00007528-USD[/TD]
[/TR]
[TR]
[TD]Z00007882-EUR[/TD]
[/TR]
[TR]
[TD]BOARDINTUSD[/TD]
[/TR]
[TR]
[TD]C&SWHOLESALEUSD[/TD]
[/TR]
[TR]
[TD]CGIINCUSD[/TD]
[/TR]
[TR]
[TD]CHOUETUSD[/TD]
[/TR]
[TR]
[TD]DOUBLEROCKUSD[/TD]
[/TR]
[TR]
[TD]DTELLCUSD[/TD]
[/TR]
[TR]
[TD]INCEPTYLTDZAR[/TD]
[/TR]
[TR]
[TD]MEADOWBRKUSD[/TD]
[/TR]
[TR]
[TD]NIGERIALNGUSD[/TD]
[/TR]
[TR]
[TD]ONEROCKCAPUSD[/TD]
[/TR]
[TR]
[TD]OPXBUSD[/TD]
[/TR]
[TR]
[TD]SITELUSD[/TD]
[/TR]
[TR]
[TD]STCLOUDCAPUSD[/TD]
[/TR]
[TR]
[TD]Z00001411-USD[/TD]
[/TR]
[TR]
[TD]Z00002486-USD[/TD]
[/TR]
[TR]
[TD]Z00002494-USD[/TD]
[/TR]
[TR]
[TD]Z00002661-USD[/TD]
[/TR]
[TR]
[TD]Z00003279-USD[/TD]
[/TR]
</tbody>[/TABLE]

however when i try to run the macro i get "Run Error 13':, Type Mismatch"

Without changing anything in the macro, if i cut the number of values down so that the total string length is 140 or below then it runs fine, ie this set:
[TABLE="width: 186"]
<tbody>[TR]
[TD]Z00007528-USD[/TD]
[/TR]
[TR]
[TD]Z00007882-EUR[/TD]
[/TR]
[TR]
[TD]BOARDINTUSD[/TD]
[/TR]
[TR]
[TD]C&SWHOLESALEUSD[/TD]
[/TR]
[TR]
[TD]CGIINCUSD[/TD]
[/TR]
[TR]
[TD]CHOUETUSD[/TD]
[/TR]
[TR]
[TD]DOUBLEROCKUSD[/TD]
[/TR]
[TR]
[TD]DTELLCUSD[/TD]
[/TR]
[TR]
[TD]INCEPTYLTDZAR[/TD]
[/TR]
</tbody>[/TABLE]

I dont know if this is something in how i wrote my macro or if its a SQL limitation.

any help is greatly appreciated
Cheers
-Ben


Code:
Dim lasta As Long
Dim all As String
Dim cell As Range
    
lasta = Sheets("Accounts").Range("a" & Rows.Count).End(xlUp).Row
    

  For Each cell In Sheets("Accounts").Range("A1:A" & lasta)
        all = all & cell.Value & "','"
    Next cell

all = Left(all, Len(all) - 3)

Sheets("accounts").Range("c5").Value = all

    With Sheets("ARData").ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DRIVER=SQL Server;SERVER=NYCVMBUS007;UID=;Trusted_Connection=Yes;APP=Microsoft Office 2013;DATABASE=Staging;Net" _
        ), Array("work=DBMSSOCN")), Destination:=Range("$A$10")).QueryTable
          .CommandText = Array( _
        "SELECT VW_AR_COLLECTIONS.""AR Rep"",VW_AR_COLLECTIONS.Category, VW_AR_COLLECTIONS.""GP ID"",", _
        "VW_AR_COLLECTIONS.""CUSTOMER NAME"",VW_AR_COLLECTIONS.""BILLING CONTACT"", VW_AR_COLLECTIONS.CCode,VW_AR_COLLECTIONS.""TOTAL DUE $"",", _
        " VW_AR_COLLECTIONS.INET1,VW_AR_COLLECTIONS.INET2, VW_AR_COLLECTIONS.CURRENCY,VW_AR_COLLECTIONS.""OPEN BALANCE"",", _
        "VW_AR_COLLECTIONS.INV" & Chr(13) & "" & Chr(10) & _
        "FROM Staging.dbo.VW_AR_COLLECTIONS VW_AR_COLLECTIONS" & Chr(13) & "" & Chr(10) & _
        "WHERE (VW_AR_COLLECTIONS.""GP ID"" in ('" & all & "'))")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Accounts"
        .Refresh BackgroundQuery:=False
    End With
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top