Connection.add method in vba: resolving 'type mismatch' error

fboehlandt

Active Member
Joined
Sep 9, 2008
Messages
334
Hello everyone,
I have the following problem. Ii would like to import data from an access database into an excel pivottable using the connection.add method in vba. As a command string I want to implement a union query that combines the data from two tables in the original database.


Code:
Sub Record_dataimport()
 
Dim wb As Workbook
Dim commandarray
Dim connectionstring
 
Set wb = ThisWorkbook
 
'***********************************************
'This is a select / union query as command array
'***********************************************
 
commandarray = Array( _
"SELECT RETURNS.Code, RETURNS.Fund, ADMINISTRATIVE.Main_Strategy, IIf([ADMINISTRATIVE.Leverage]='No','No','Yes') AS Leverage, RETURNS.MM_DD_YYYY, RETURNS.Performance, 'ROI' AS [Type]" _
& "FROM `C:\path\database.accdb`.`ADMINISTRATIVE`" _
& "INNER JOIN `C:\path\database.accdb`.`RETURNS`" _
& "ON ADMINISTRATIVE.Code = RETURNS.Code" _
& "UNION" _
& "SELECT ASSETS.Code, ASSETS.Fund, ADMINISTRATIVE.Main_Strategy, IIf([ADMINISTRATIVE.Leverage]='No','No','Yes') AS Leverage, ASSETS.MM_DD_YYYY, ASSETS.Assets_Managed, 'AUM' AS [Type]" _
& "FROM `C:\path\database.accdb`.`ADMINISTRATIVE`" _
& "INNER JOIN `C:\path\database.accdb`.`ASSETS`" _
& "ON ADMINISTRATIVE.Code = ASSETS.Code")
 
'***********************************************
'This is the connection string to the Access database
'***********************************************
 
connectionstring = Array( _
"OLEDB;" _
& "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Password="""";" _
& "User ID=Admin;" _
& "Data Source=C:\path\database.accdb;" _
& "Mode=ReadWrite;" _
& "Extended Properties="""";" _
& "Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";" _
& "Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=6;" _
& "Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;" _
& "Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";" _
& "Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;" _
& "Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;" _
& "Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False")
 
'***********************************************
'Connections.Add Method -->
'Name As String, Description As String, ConnectionString As Variant,
'CommandText As Variant, lCmdtype As Variant
'***********************************************
 
wb.Connections.Add "HFR_ACCESS_20070115", "", connectionstring, commandarray, 3


When I run the code it returns the error message 'Type mismatch' on the last line of coding. Obviously, VBA doesn't recognize connectionstring and/or commandarray as variant datatype. I cannot set them to Variant either (commandarray As Variant), the error then just appears when I define the array. I'm sure this is a fairly simple problem that has just to do with my limited knowledge of VBA. Can somebody help please?
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi

Try amending that last line to:


Code:
wb.Connections.Add "HFR_ACCESS_20070115", "", Join(connectionstring,""), Join(commandarray,""), 3
 
Upvote 0
Excellent!
That works nicely! Thanks ver much. One question though: every time I run the macro a new data connection is created. When I delete all the old data connections and let the macro run it returns the following error message:

Run-time error '1004':
Syntac error (missing operator) in query expression
'ADMINISTRATIVE.Code = RETURNS.CodeUNIONSELECT
ASSETS.Cod.'

in the line of coding where I set the pivottable. The continuation of the code looks like this:

Code:
wb.Connections.Add "HFR_ACCESS_20070115", "", Join(connectionstring, ""), Join(commandarray, ""), 3
 
Dim rawroi As Worksheet
Dim ptCache As PivotCache
Dim ptroi As PivotTable
Dim dates, strategies, leverage, code, roi As PivotField
 
Set rawroi = wb.Worksheets("raw_data")
 
'********************************************
'Clear any previous pivottables in workbook
'Set up PivotCache and define in put area
'********************************************
 
For Each pt In rawroi.PivotTables
    pt.TableRange2.Clear
Next pt
 
'********************************************
'Create Pivot table report
'********************************************
 
Set ptCache = wb.PivotCaches.Create(SourceType:=xlExternal, SourceData:=wb.Connections("HFR_ACCESS_20070115"), Version:=xlPivotTableVersion12)
Set ptroi = ptCache.CreatePivotTable(TableDestination:=rawroi.Cells(1, 1), TableName:="PivotTable_ROI")
 
Set dates = ptroi.PivotFields("MM_DD_YYYY")
Set strategies = ptroi.PivotFields("Main_Strategy")
Set leverage = ptroi.PivotFields("Leverage")
Set code = ptroi.PivotFields("Code")
Set roi = ptroi.PivotFields("Performance")


The error occurs at 'Set ptroi = ptCache...', but ONLY if I delete the data connection(s) manually in Excel. Any ideas? Thanks very much for your help so far!!!
 
Upvote 0
I think that's just cos you need spaces in your SQL string so the words aren't joined up together. Because of the neat compartmentalized way you have written the SQL, you can probably just amend the Connections line to:

Code:
wb.Connections.Add "HFR_ACCESS_20070115", "", Join(connectionstring,""), Join(commandarray," "), 3
 
Upvote 0
Unfortunately not. I also tried to manually implement a character function into the search query --> & Chr(32) &, but to no avail. It establishes the datalink, but creates no pivottable. Any further ideas?
 
Upvote 0
Sorted! The flaw was in the commandarray. Members of the string must be separated by commas rather than &. Many thanks for your help,
cheers
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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