VBA usign predefined SQL over SQL server database



Hi

Appologies up front if the terminology in this question is not correct - I
am very much a VB beginner - with grandious ideas.

I am floundering around trying to figure out how to run a parameter query
using VBA.

I have already defined the query in Excel - Data > Import External Data >
New Database Query) from Excel VBA

The query accesses a SQL Server database

It is working up to that point and uses a parameter from the workbook, and 2
parameters that it needs to ask the user for

I have put the request for the 2 user parameters into the VBA macro

I need to run the query from the macro attached to a button in Excel as I
have some other work to do before it runs.

Is there anyway that I can run the query I have already defined from VBA or
do I have to define the SQL etc from scratch in VBA?

BTW - I am using Excel 2003. I tried to define the SQL using the SQLOpen,
SQLExecQuery etc, then found out I needed the ODBC add-in that is not
available for Excel 2003. Below is how I had done this, I tried to understand
the ADO info and got completely lost - it would seem logical that I can use
the predefined query - (logical to me anyway :)

Thanks in advance for any assistance you are able to offer.

Frankie


'Find last detail row in Date Work***
LastRow = Worksheets(replicondataSheet).UsedRange.Rows.count

'Add 1 to last row

StartPopulationRow = LastRow + 1
StartData = "A" + StartPopulationRow

'Retrieve Replicon Data

Dim projectcode As String
Dim DatabaseName As String

'DatabaseName = "replicon_test"
projectcode = Worksheets(summarySheet).Name("projectcode")

QueryString = _
"SELECT Rp_V_EDProjStatusUserDt_ts.taskname1,
Rp_V_EDProjStatusUserDt_ts.username, Rp_V_EDProjStatusUserDt_ts.entrydate,
Rp_V_EDProjStatusUserDt_ts.billable_hours,
Rp_V_EDProjStatusUserDt_ts.comments " _
& "FROM Replicon.dbo.Rp_V_EDProjStatusUserDt_ts
Rp_V_EDProjStatusUserDt_ts " _
& "WHERE (Rp_V_EDProjStatusUserDt_ts.projectcode=pProjectCode) AND
(Rp_V_EDProjStatusUserDt_ts.billable_hours>0) AND
(Rp_V_EDProjStatusUserDt_ts.entrydate>=StartDateResponse) AND
(Rp_V_EDProjStatusUserDt_ts.entrydate<=EndDateResponse)"
Chan = SQLOpen("DSN=" & DatabaseName)
SQLExecQuery Chan, QueryString
Set Output = Worksheets(replicondataSheet).Range(StartData)
SQLRetrieve Chan, Output, , , True
SQLClose Chan


.