Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 127
Default Querying a SQL Server Database

SQL Server 2000. Excel 2003. I know how to set an Excel sheet to query my
SQL Server database table(s) and populate the sheet cells. This works for
some of my needs. But in general I would like to be able to do a query and
have the results go to a record set, that I can then programmatically
manipulate. Can anyone help me with some sample VBA code that populates a
record set from a SQL Server database? Thanks and God bless for any help
provided.

What I am using now to populate the Excel sheet is:

With ThisWorkbook.Sheets("Project
Definitions").QueryTables.Add(Connection:= _

"ODBC;DSN=AFNINI_1;Description=Good_News_FE;UID=FE open7;;APP=Microsoft Office
2003;WSID=DOUG;DATABASE=Good_News_FE" _
, Destination:=Range("J1"))
.CommandText = "SELECT GL7PROJECTS.PROJECTID,
GL7PROJECTS.DESCRIPTION, GL7PROJECTS.STARTDATE, TABLEENTRIES.DESCRIPTION FROM
(GL7PROJECTS LEFT JOIN GL7PROJECTATTRIBUTES ON GL7PROJECTS.GL7PROJECTSID =
GL7PROJECTATTRIBUTES.PARENTID) LEFT JOIN TABLEENTRIES ON
GL7PROJECTATTRIBUTES.TABLEENTRIESID = TABLEENTRIES.TABLEENTRIESID ORDER BY
GL7PROJECTS.PROJECTID;"
.Name = "Project Attributes Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Querying a SQL Server Database

http://tinyurl.com/d2yva

--
Regards,
Tom Ogilvy


"Chaplain Doug" wrote in message
...
SQL Server 2000. Excel 2003. I know how to set an Excel sheet to query

my
SQL Server database table(s) and populate the sheet cells. This works for
some of my needs. But in general I would like to be able to do a query

and
have the results go to a record set, that I can then programmatically
manipulate. Can anyone help me with some sample VBA code that populates a
record set from a SQL Server database? Thanks and God bless for any help
provided.

What I am using now to populate the Excel sheet is:

With ThisWorkbook.Sheets("Project
Definitions").QueryTables.Add(Connection:= _

"ODBC;DSN=AFNINI_1;Description=Good_News_FE;UID=FE open7;;APP=Microsoft

Office
2003;WSID=DOUG;DATABASE=Good_News_FE" _
, Destination:=Range("J1"))
.CommandText = "SELECT GL7PROJECTS.PROJECTID,
GL7PROJECTS.DESCRIPTION, GL7PROJECTS.STARTDATE, TABLEENTRIES.DESCRIPTION

FROM
(GL7PROJECTS LEFT JOIN GL7PROJECTATTRIBUTES ON GL7PROJECTS.GL7PROJECTSID =
GL7PROJECTATTRIBUTES.PARENTID) LEFT JOIN TABLEENTRIES ON
GL7PROJECTATTRIBUTES.TABLEENTRIESID = TABLEENTRIES.TABLEENTRIESID ORDER BY
GL7PROJECTS.PROJECTID;"
.Name = "Project Attributes Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Querying a SQL Server Database

Look at the CopyFromRecordset method in VBA help.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Chaplain Doug" wrote in message
...
SQL Server 2000. Excel 2003. I know how to set an Excel sheet to query

my
SQL Server database table(s) and populate the sheet cells. This works for
some of my needs. But in general I would like to be able to do a query

and
have the results go to a record set, that I can then programmatically
manipulate. Can anyone help me with some sample VBA code that populates a
record set from a SQL Server database? Thanks and God bless for any help
provided.

What I am using now to populate the Excel sheet is:

With ThisWorkbook.Sheets("Project
Definitions").QueryTables.Add(Connection:= _

"ODBC;DSN=AFNINI_1;Description=Good_News_FE;UID=FE open7;;APP=Microsoft

Office
2003;WSID=DOUG;DATABASE=Good_News_FE" _
, Destination:=Range("J1"))
.CommandText = "SELECT GL7PROJECTS.PROJECTID,
GL7PROJECTS.DESCRIPTION, GL7PROJECTS.STARTDATE, TABLEENTRIES.DESCRIPTION

FROM
(GL7PROJECTS LEFT JOIN GL7PROJECTATTRIBUTES ON GL7PROJECTS.GL7PROJECTSID =
GL7PROJECTATTRIBUTES.PARENTID) LEFT JOIN TABLEENTRIES ON
GL7PROJECTATTRIBUTES.TABLEENTRIESID = TABLEENTRIES.TABLEENTRIESID ORDER BY
GL7PROJECTS.PROJECTID;"
.Name = "Project Attributes Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 127
Default Querying a SQL Server Database

Dear Bob:

It appears that the CopyFromRecordset works on an ADO recordset already
created. What I need is some sample VBA code to create the ADO recordset
from an SQL Server database to begin with. I do not know how to do this.
Can you help?
--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org


"Bob Phillips" wrote:

Look at the CopyFromRecordset method in VBA help.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Chaplain Doug" wrote in message
...
SQL Server 2000. Excel 2003. I know how to set an Excel sheet to query

my
SQL Server database table(s) and populate the sheet cells. This works for
some of my needs. But in general I would like to be able to do a query

and
have the results go to a record set, that I can then programmatically
manipulate. Can anyone help me with some sample VBA code that populates a
record set from a SQL Server database? Thanks and God bless for any help
provided.

What I am using now to populate the Excel sheet is:

With ThisWorkbook.Sheets("Project
Definitions").QueryTables.Add(Connection:= _

"ODBC;DSN=AFNINI_1;Description=Good_News_FE;UID=FE open7;;APP=Microsoft

Office
2003;WSID=DOUG;DATABASE=Good_News_FE" _
, Destination:=Range("J1"))
.CommandText = "SELECT GL7PROJECTS.PROJECTID,
GL7PROJECTS.DESCRIPTION, GL7PROJECTS.STARTDATE, TABLEENTRIES.DESCRIPTION

FROM
(GL7PROJECTS LEFT JOIN GL7PROJECTATTRIBUTES ON GL7PROJECTS.GL7PROJECTSID =
GL7PROJECTATTRIBUTES.PARENTID) LEFT JOIN TABLEENTRIES ON
GL7PROJECTATTRIBUTES.TABLEENTRIESID = TABLEENTRIES.TABLEENTRIESID ORDER BY
GL7PROJECTS.PROJECTID;"
.Name = "Project Attributes Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 233
Default Querying a SQL Server Database

I'm currently developing code that helps with that, but its not
finished yet. At least I know now there is some demand for it.

Just to be curious, why do you need the ADO recordset at all when you
have a good querytable?

Dm Unseen



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 127
Default Querying a SQL Server Database

There are some instaces where I just want to get some data from the SQL
database and not populate an Excel sheet.
--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org


"DM Unseen" wrote:

I'm currently developing code that helps with that, but its not
finished yet. At least I know now there is some demand for it.

Just to be curious, why do you need the ADO recordset at all when you
have a good querytable?

Dm Unseen


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Querying a SQL Server Database

Here is an example of getting data from an Access table

Const adOpenForwardOnly As Long = 0
Const adLockReadOnly As Long = 1
Const adCmdText As Long = 1
Dim oRS As Object
Dim sConnect As String
Dim sSQL As String
Dim ary

sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "c:\bob.mdb"

sSQL = "SELECT * From Contacts"
Set oRS = CreateObject("ADODB.Recordset")
oRS.Open sSQL, sConnect, adOpenForwardOnly, _
adLockReadOnly, adCmdText

' Check to make sure we received data.
If Not oRS.EOF Then
ary = oRS.getrows
MsgBox ary(0, 0) & " " & ary(1, 0) & ", " & ary(2, 0)
Else
MsgBox "No records returned.", vbCritical
End If

oRS.Close
Set oRS = Nothing
End Sub

you will need to connect to SQL server, so the conn ection string will
change (don't have an example to hand I am afraid), of which your
connecttion string of

ODBC;DSN=AFNINI_1;Description=Good_News_FE;UID=FEo pen7;;APP=Microsoft Office
2003;WSID=DOUG;DATABASE=Good_News_FE"

might well work, but I would check
http://www.carlprothman.net/Default....erForSQLServer
for an OLE DB version.

Use your SQL code where I have sSQL

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Chaplain Doug" wrote in message
...
Dear Bob:

It appears that the CopyFromRecordset works on an ADO recordset already
created. What I need is some sample VBA code to create the ADO recordset
from an SQL Server database to begin with. I do not know how to do this.
Can you help?
--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org


"Bob Phillips" wrote:

Look at the CopyFromRecordset method in VBA help.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Chaplain Doug" wrote in

message
...
SQL Server 2000. Excel 2003. I know how to set an Excel sheet to

query
my
SQL Server database table(s) and populate the sheet cells. This works

for
some of my needs. But in general I would like to be able to do a

query
and
have the results go to a record set, that I can then programmatically
manipulate. Can anyone help me with some sample VBA code that

populates a
record set from a SQL Server database? Thanks and God bless for any

help
provided.

What I am using now to populate the Excel sheet is:

With ThisWorkbook.Sheets("Project
Definitions").QueryTables.Add(Connection:= _

"ODBC;DSN=AFNINI_1;Description=Good_News_FE;UID=FE open7;;APP=Microsoft

Office
2003;WSID=DOUG;DATABASE=Good_News_FE" _
, Destination:=Range("J1"))
.CommandText = "SELECT GL7PROJECTS.PROJECTID,
GL7PROJECTS.DESCRIPTION, GL7PROJECTS.STARTDATE,

TABLEENTRIES.DESCRIPTION
FROM
(GL7PROJECTS LEFT JOIN GL7PROJECTATTRIBUTES ON

GL7PROJECTS.GL7PROJECTSID =
GL7PROJECTATTRIBUTES.PARENTID) LEFT JOIN TABLEENTRIES ON
GL7PROJECTATTRIBUTES.TABLEENTRIESID = TABLEENTRIES.TABLEENTRIESID

ORDER BY
GL7PROJECTS.PROJECTID;"
.Name = "Project Attributes Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org






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
Querying Large Database RussellT Excel Discussion (Misc queries) 0 November 2nd 09 07:46 PM
Querying SQL Server from Excel James C. Excel Discussion (Misc queries) 1 January 16th 07 05:51 PM
Database access & querying Steve[_78_] Excel Programming 7 September 12th 05 02:21 PM
Querying Access Database Edgar Thoemmes Excel Worksheet Functions 1 December 15th 04 01:58 PM
Querying a database for values in each row Stephen Goldfinger Excel Programming 13 December 19th 03 01:25 PM


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