ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Querying a SQL Server Database (https://www.excelbanter.com/excel-programming/345542-querying-sql-server-database.html)

Chaplain Doug

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

Tom Ogilvy

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




Bob Phillips[_6_]

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




Chaplain Doug

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





DM Unseen

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


Chaplain Doug

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



Bob Phillips[_6_]

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








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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com