Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default How to select other sheets using ADO

Hi,



I use the folowing code to get information from a closed workbook with
ADO. It automatically selects the first sheet.

How do I get it to select other sheets? e.g. sheet2


sub test()

Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim i As Integer

dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" &
"ReadOnly=1;DBQ=E:\AdoVBA\99budget.xls"
Set dbConnection = New ADODB.Connection

dbConnection.Open dbConnectionString ' open the database connection

Set rs = dbConnection.Execute("[" & "a:a" & "]")

Do While Not rs.EOF
UserForm1.ListBox1.AddItem rs.Fields.Item(i)
rs.MoveNext
Loop

rs.Close
dbConnection.Close
Set rs = Nothing
Set dbConnection = Nothing

End sub


Help appreciated,

Farmer
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to select other sheets using ADO

Farmer,

This is the code that I use to read closed workbooks

Public Sub GetData()
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim sFilename As String
Dim sConnect As String
Dim sSQL As String

sFilename = "c:\Mytest\Volker1.xls"
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sFilename & ";" & _
"Extended Properties=Excel 8.0;"

sSQL = "SELECT * FROM [Sheet1$]"

Set oRS = New ADODB.Recordset
oRS.Open sSQL, sConnect, adOpenForwardOnly, _
adLockReadOnly, adCmdText

If Not oRS.EOF Then
ActiveSheet.Range("A1").CopyFromRecordset oRS
Else
MsgBox "No records returned.", vbCritical
End If

oRS.Close
Set oRS = Nothing

End Sub


Just change the Sheet1$ to your named sheet.

--

HTH

RP

"farmer" wrote in message
...
Hi,



I use the folowing code to get information from a closed workbook with
ADO. It automatically selects the first sheet.

How do I get it to select other sheets? e.g. sheet2


sub test()

Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim i As Integer

dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" &
"ReadOnly=1;DBQ=E:\AdoVBA\99budget.xls"
Set dbConnection = New ADODB.Connection

dbConnection.Open dbConnectionString ' open the database connection

Set rs = dbConnection.Execute("[" & "a:a" & "]")

Do While Not rs.EOF
UserForm1.ListBox1.AddItem rs.Fields.Item(i)
rs.MoveNext
Loop

rs.Close
dbConnection.Close
Set rs = Nothing
Set dbConnection = Nothing

End sub


Help appreciated,

Farmer



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default How to select other sheets using ADO














Hi,

Thanks It's just what I wanted. Works great.

Greetings,

Farmer





On Sun, 24 Oct 2004 15:19:58 +0100, "Bob Phillips"
wrote:

Farmer,

This is the code that I use to read closed workbooks

Public Sub GetData()
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim sFilename As String
Dim sConnect As String
Dim sSQL As String

sFilename = "c:\Mytest\Volker1.xls"
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sFilename & ";" & _
"Extended Properties=Excel 8.0;"

sSQL = "SELECT * FROM [Sheet1$]"

Set oRS = New ADODB.Recordset
oRS.Open sSQL, sConnect, adOpenForwardOnly, _
adLockReadOnly, adCmdText

If Not oRS.EOF Then
ActiveSheet.Range("A1").CopyFromRecordset oRS
Else
MsgBox "No records returned.", vbCritical
End If

oRS.Close
Set oRS = Nothing

End Sub


Just change the Sheet1$ to your named sheet.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to select all sheets belvy123 Excel Discussion (Misc queries) 7 March 27th 08 09:43 PM
Select Multiple sheets Steve Excel Discussion (Misc queries) 1 October 7th 05 06:14 PM
select a1 on all sheets Piers Clinton-Tarestad Excel Programming 1 October 1st 03 08:34 PM
All Sheets Unhide and Select Hande & Tolga Excel Programming 2 September 11th 03 02:16 PM
select sheets by name - how? Walt[_2_] Excel Programming 5 July 30th 03 10:42 PM


All times are GMT +1. The time now is 07:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"