Avatar of GPSPOW
GPSPOWFlag for United States of America

asked on 

Using Excel cell reference as a parameter in a SQL statement

I have created a SQL query procedure called from Excel by an External SQL connection:

There is a Where clause to limit the data by a date field between "StartDate" and "EndDate".

I want to be able to reference a cell value in Excel for the Start and End date.

What is the syntax for the SQL code?

Thanks

Glen
Microsoft ExcelMicrosoft SQL Server

Avatar of undefined
Last Comment
GPSPOW
Avatar of lludden
lludden
Flag of United States of America image

You will have to add a parameter to the query and pass it when the SP is called.
Avatar of GPSPOW
GPSPOW
Flag of United States of America image

ASKER

Can you store a date value in Excel VBa and pass that to SQL statement?
Avatar of lludden
lludden
Flag of United States of America image

Yes.  If you are calling the SQL from vba, you can pass it arguments.
Avatar of SixSigmaGuy
SixSigmaGuy
Flag of United States of America image

I think a better way to do it (it's easier to debug) is to write some VBA code that pulls the values from the worksheet cells and then you concatenate them to your SQL string.  I'm not a SQL expert, so excuse me if my SQL syntax is wrong, but it should give you the idea.

Say you have the start date stored in cell A1 and the end date stored in cell A2.

Sub SendSQL()
     Dim stStartDate as String  ' or "as Date" if you need to convert the date formats
     Dim stEndDate as String
     Dim stSQL as String

     stStartDate = Me.Cells(1, 1)
     stEndDate = Me.Cells(1, 2)

     stSQL = "Select * Where StartDate > " & stStartDate & " AND EndDate < " & stEndDate

     '
     ' Send SQL string
     '
End Sub
Avatar of GPSPOW
GPSPOW
Flag of United States of America image

ASKER

I read a thread from DataPig explaining how you can use MS Query in conjunction with MS Access:

Here is my MS Query Code:

SELECT VW_NHSN_PATIENTS.VisitID, VW_NHSN_PATIENTS.AccountNumber, VW_NHSN_PATIENTS.BirthDateTime, VW_NHSN_PATIENTS.UnitNumber, VW_NHSN_PATIENTS.SurgeonID, VW_NHSN_PATIENTS.SurgeonName, VW_NHSN_PATIENTS.Description, VW_NHSN_PATIENTS.DateTime
FROM `C:\Documents and Settings\gpowers\My Documents\NHSN.accdb`.VW_NHSN_PATIENTS VW_NHSN_PATIENTS
WHERE (VW_NHSN_PATIENTS.SurgeonID='CHANI') AND (VW_NHSN_PATIENTS.DateTime>={ts '2013-07-01 07:00:00'} And VW_NHSN_PATIENTS.DateTime<={ts '2013-08-16 12:30:00'})


How can pass the SurgeonID and DateTime parameters from a cell reference with the spreadsheet?

Thanks

Glen
ASKER CERTIFIED SOLUTION
Avatar of winheim
winheim

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of GPSPOW
GPSPOW
Flag of United States of America image

ASKER

It took a while but I got it to work.

Thanks

Glen
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo