Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Create Excel Document from a Dataset using VB.Net

Hi;
I am using Visual Studio 2003 and Office 2003. What I
would like is on a button click, run query against a
database and take the returned dataset to create an excel
document.

I am pretty sure it is possible, but I just don't know how
to.

Any help will be greatly apprecicated.

thanks
Jacob
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Create Excel Document from a Dataset using VB.Net

In the IDE set a reference to Microsoft Active Data
Objects

In your code, dim a variable as a ADODB connection and
alother variable as an ADOB recordset. Here's a very
simple, but working example

Sub GetData()

Dim oConn As ADODB.Connection
Dim oRST As ADODB.Recordset
Dim sConnectionString As String
Dim sSQL As String
Dim ws As Worksheet
Dim i As Long

' Initialise variables
sConnectionString = _
"PROVIDER=MSDASQL;driver={SQL Server};" & _
"server=MyServer;uid=;pwd=;database=MyDatabaseName ;"

' set the SQL query command text:
sSQL = "SELECT * from Funds"

' create objects
Set oConn = New ADODB.Connection
Set oRST = New ADODB.Recordset

' connect to the database

With oConn
.ConnectionString = sConnectionString
.Open
End With

' fetch the data
oRST.Open sSQL, oConn, adOpenForwardOnly,
adLockOptimistic

'drop data into a new worksheet
With oRST
If Not .EOF Then
Set ws = Worksheets.Add
' get the field names as headers
For i = 1 To .Fields.Count
ws.Cells(1, i).Value = .Fields(i - 1).Name
Next
ws.Range("A2").CopyFromRecordset oRST
End If

.Close

End With

oConn.Close

Set oRST = Nothing
Set oConn = Nothing

End Sub

Make sure that you (a) set the correct text for MyServer
and MyDatabaseName in the connection string, and that you
can use NT security with the database.

Patrick Molloy
Microsoft Excel MVP


-----Original Message-----
Hi;
I am using Visual Studio 2003 and Office 2003. What

I
would like is on a button click, run query against a
database and take the returned dataset to create an

excel
document.

I am pretty sure it is possible, but I just don't know

how
to.

Any help will be greatly apprecicated.

thanks
Jacob
.

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
Can I create a sequential numbering system on an Excel document? Jamie Excel Discussion (Misc queries) 6 November 11th 08 06:34 PM
How do you create address labels from an excel document? Address labels created in Excel Excel Discussion (Misc queries) 1 January 16th 07 02:14 AM
How to make Excel create and fill another document when filling a HumbleStudent Excel Discussion (Misc queries) 3 August 28th 06 10:41 PM
How to create a formatted 'readable' document based on Excel document? [email protected] Excel Discussion (Misc queries) 0 June 23rd 06 06:09 PM
How do I create a mail merge document in Excel? Is it possible? Plbowles Excel Discussion (Misc queries) 6 December 19th 05 07:28 PM


All times are GMT +1. The time now is 01: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"