ADODB connection opening read only workbooks

niall91

New Member
Joined
Jul 21, 2020
Messages
45
Office Version
  1. 2019
Platform
  1. Windows
Hi Im having an issue when getting data from an excel workbook that is read only.
When i open the connection it opens the actual workbook which i obviously want to keep closed.
Any ideas?
Thanks

VBA Code:
Dim cnt As ADODB.Connection
Dim rs As ADODB.Recordset

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

'connect using database string
    cnt.ConnectionString = _
    "Provider=Microsoft.ACE.OLEDB.12.0;Data " _
    & "Source=S:\Invoicing & Delivery Docket\Customer Database\Customer database.xlsm;Extended " _
    & "Properties=""Excel 12.0 Macro;HDR=YES;"""

'open the connection
    cnt.Open
'set record set with connection
    rs.ActiveConnection = cnt
    rs.LockType = adLockReadOnly
'construct the sourse for the record set. in this case column with surf codes
    rs.Source = "SELECT [UI],[Company] FROM [Customers$]" '[Sheet4$]
'open the record set
    rs.Open , , adModeRead
'set error handler
    On Error GoTo Finish
'loop through record set
    Do Until rs.EOF
        CustomerUI.AddItem rs.Fields("UI") & " " & rs.Fields("Company")
        rs.MoveNext
    Loop

Finish:
'close the connection
    rs.Close
'close the connection
    cnt.Close
 
    Set cnt = Nothing
    Set rs = Nothing
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Now that you have posted your code, why don't you post in words EXACTLY what you are trying to accomplish, step by step. From that we can probably tell where your code goes into the weeds.
 
Upvote 0
Your code, as written doesn’t open the workbook so I suspect the issue lies elsewhere
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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