Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Treating WorkSheet As MS Access Table?

Within a VBA module in the same .XLS, is there any way to open up one
of the worksheets as an MS Access table?

My thinking is that then I could use of SQL against the worksheet
directly - without writing all the code to create a temporary MS
Access DB and write the worksheet's contents to a temp table within
said DB.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Treating WorkSheet As MS Access Table?


sure - you can treat a sheet/named range as if were a database...
here's and example where every column is read from a table range named
Instruments (its on Sheet1)

in the development environment you need to set a Reference to Microsoft
Active Data Objects 2.6 Library dll (or 2.7) (menu: Tools/References)




Sub LoadFromExcelDatabase()

Dim Conn As ADODB.Connection
Dim RST As ADODB.Recordset
Dim strConn As String
Dim SQL As String
Dim ws As Worksheet, wb As Workbook, cl As Long

Dim sExcelSourceFile As String

sExcelSourceFile = "C:\Temp\XL_Database.xls"

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel
8.0;"
strConn = strConn & "Data Source="
strConn = strConn & sExcelSourceFile

Set Conn = New ADODB.Connection
Conn.Open strConn

Set RST = New ADODB.Recordset
SQL = "SELECT * FROM [Instruments]"

RST.Open SQL, Conn, adOpenStatic

If Not RST.EOF Then

Set wb = Workbooks.Add(xlWBATWorksheet)
Set ws = wb.ActiveSheet

For cl = 1 To RST.Fields.Count
ws.Cells(1, cl).Value = RST.Fields(cl - 1).Name
Next
ws.Range("A2").CopyFromRecordset RST

Set ws = Nothing
Set wb = Nothing


End If



RST.Close

Conn.Close

Set RST = Nothing
Set Conn = Nothing

End Sub

"PeteCresswell" wrote in message
...
Within a VBA module in the same .XLS, is there any way to open up one
of the worksheets as an MS Access table?

My thinking is that then I could use of SQL against the worksheet
directly - without writing all the code to create a temporary MS
Access DB and write the worksheet's contents to a temp table within
said DB.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Treating WorkSheet As MS Access Table?

On Jul 2, 9:24*am, "Patrick Molloy"
wrote:
sure - you can treat a sheet/named range as if were a database...
here's and example where every column is read from a table range named
Instruments (its on Sheet1)

in the development environment you need to set a Reference to Microsoft
Active Data Objects 2.6 Library * dll (or 2.7) * (menu: Tools/References)


That's exactly, *precisely* what I was hoping for.

Thanks.
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
Pivot table treating numbers as text in value field Rune Wentzel Helms Excel Worksheet Functions 2 July 3rd 09 09:15 AM
excel worksheet export into access table H. Chudhary Excel Worksheet Functions 0 January 23rd 08 02:26 PM
Exporting excel worksheet to access table Jamie Collins Excel Programming 0 September 22nd 04 03:12 PM
Exporting excel worksheet to access table Tom Ogilvy Excel Programming 0 September 21st 04 05:03 PM
Exporting excel worksheet to access table Narwe Excel Programming 0 September 21st 04 04:31 PM


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

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

About Us

"It's about Microsoft Excel"