access to Access

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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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