billritz

New Member
Joined
Aug 20, 2012
Messages
23
Trying to connect to an Access database, I copied some production code that works with the same .accdb file- why type it again, right?
'open connection
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\TestData.accdb;"


'build recordset
sSQL = "SELECT * FROM tblTest;"
Set rs = CreateObject("ADODB.Recordset")
rs.Open sSQL, cn, adOpenDynamic, adLockOptimistic

It blows up, highlighting the word adOpenDynamic saying variable not defined. So I open the other project, hit the compile under debug, and it goes right through! I go back to the new one, and try to run it, and boom. If the old code runs, the correct libraries are open, so what could cause this?

Next, I changed the parameters to 2 and 3 (below) and it compiles, but the first time I do a .Find on the rs it gives me a 3001 (bad arguments) so it's really not opening correctly, though the same code is working on the same .accdb file in another workbook open in the same session. Weird. Any help would be appreciated.

rs.Open sSQL, cn, 2, 3 'adOpenDynamic, adLockOptimistic
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
In your Visual Basic window, go to Tools | References and ensure that Microsoft ActiveX Data Objects are selected - check the other project to see which one(s) it uses...

HTH,
~ Jim
 
Upvote 0
What happens if you set the same references in the new project as those in the original project?
 
Upvote 0

Forum statistics

Threads
1,216,052
Messages
6,128,509
Members
449,455
Latest member
jesski

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