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
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