ADO in Excel VBA

dugdugdug

Active Member
Joined
May 11, 2012
Messages
342
I am using ADO to retrieve data from sheet 1 and 2 into sheet 3, within the same workbook.

The following code does the job correctly.

However, when I have two such workbooks open, say workbook 1 and 2 and run the code separately to retrieve data into sheet 3 of workbook 1 (from sheets 1 and 2 of workbook 1) and retrieve data into sheet 3 of workbook 2 (from sheets 1 and 2 of workbook 2), I get a runtime error message.

I suspect it might have something to do with locking: rs.LockType = adLockOptimistic

Can anyone help?


Sub Get_Data

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

rs.LockType = adLockOptimistic

Dim strFile As String
Dim strcon As String

strFile = ThisWorkbook.FullName

strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strFile & ";Extended Properties=""Excel 12.0 Macro;HDR=Yes;IMEX=1;MaxScanRows=0"";"

cn.Open strcon

Dim strSQL As String

strSQL = "SELECT [Sheet1$].[Field1], [Sheet1$].[Field2]" & _
"FROM [Sheet1$]" & _
"LEFT JOIN [Sheet2$]" & _
"ON [Sheet1$].[Field1] = [Sheet2$].[Field1]" & _
"WHERE [Sheet2$].[Field1] Is Null"

rs.Open strSQL, cn

Dim wsm as Worksheets

Set wsm = Thisworkbook.Worksheets("Sheet3")

wsm.Select

wsm.Range("A2").CopyFromRecordset rs

rs.Close
cn.Close

Set rs = Nothing
Set cn = Nothing

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi,

Where are you getting the error and what is the error?

Thnks,
Vish
http://www.learnexcelmacro.com

With both workbooks open, I run the code in the first workbook and all is fine.

Without closing the first workbook, I run the code in the second workbook and get the following message:

Run-time error '-2147217904 (80040e10)':

No value given for one or more required parameters.

The line in VBA that fails is the following: rs.Open strSQL, cn

This message (the one in bold) is normally seen if the SQL statement is wrong but clearly it's not because if I ran the second workbook with the first workbook CLOSED, all is fine.
 
Last edited:
Upvote 0
If you have the workbooks open why are you using ADO?
 
Upvote 0
You could do that without ADO.
 
Upvote 0

Forum statistics

Threads
1,203,742
Messages
6,057,112
Members
444,905
Latest member
Iamtryingman

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