LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Worksheets and SQL

You can use ADO to retrieve from a DB (the Provider would differ depending
on the typew of DB. For a list of providers for ADO , go to:
www.connectionstrings.com:

Sub GetProjects()
Dim src As String
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim col As Integer
Dim mydate As Date

'On Error Resume Next
Application.ScreenUpdating = False
Workbooks("WORKBOOK NAME HERE").Worksheets("WORKSHEET NAME").Select

'Open a connection to the Orb DB view in POSTGRESQL using ODBC DSN
Set cnn = New ADODB.Connection
cnn.ConnectionString = "DSN=Postgresql30;UID=PUT USERID HERE;PWD=PUT
PASSWORD HERE;"
cnn.Open

'Set recordset as ADODB recordset
Set rs = New ADODB.Recordset

'Filter
src = "SELECT LIST FIELD NAMES HERE"
src = src & "FROM TABLENAME"
src = src & "WHERE project_name IN('" & TheseProjects & "') "
src = src & " ORDER BY- LIST FIELD NAMES HERE"
rs.Open Source:=src, ActiveConnection:=cnn,
CursorType:=adOpenForwardOnly, Options:=adCmdText

'Write the field names
For col = 0 To rs.Fields.Count - 1
Workbooks("WORKBOOK NAME").Worksheets("WORKSHEET
NAME").Range("N1").Offset(0, col).Value = _
rs.Fields(col).Name
Next
'Write the recordset
Workbooks("WORKBOOK NAME").Worksheets("WORKSHEET
NAME").Range("N1").Offset(1, 0).CopyFromRecordset rs

'Clear the connection
Set rs = Nothing
'Close the Recordset
cnn.Close
Set cnn = Nothing

End Sub

NOTE: TheseProjects is a variable created in another procedure
"John" wrote in message
om...
How does one use SQL with worksheets? Please give an example of how
you can use a select statement on a database in a worksheet and then
display the result in a another worksheet.

Than you



 
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
How use info in Excel shared worksheets to create new worksheets dkc Excel Worksheet Functions 0 June 28th 07 08:36 PM
how do i copy a cell in worksheets 10 to the other 9 worksheets bete New Users to Excel 3 March 15th 07 10:41 AM
How do i assign the ActiveWorkbook.Worksheets to a worksheets object? TS Excel Worksheet Functions 2 December 27th 06 02:49 PM
Assigning Cells in worksheets to other data in other worksheets. David McRitchie Excel Discussion (Misc queries) 0 November 27th 04 06:15 PM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM


All times are GMT +1. The time now is 05:02 PM.

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"