Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Replacing fixed data in a WHERE clause with dynamic data Expand / Collapse
Author
Message
Posted Saturday, January 5, 2013 6:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 1:20 AM
Points: 7, Visits: 22
Hi,

I am new to SQL 2005 and have a question about Stored Procedures and have not been able to find a suitable answer to my question after trawling Google and Bing for days. I hope you can help.

I have a Visual Basic form and present my data in a DataGridView. My data table consists of 4 columns, ID, FirstName, LastName and DateofBirth. I can show data for a specific date of birth range using the following stored procedure:
ALTER PROCEDURE dbo.DateRange


SELECT FirstName, LastName, DateofBirth

FROM Table1

WHERE DateofBirth BETWEEN '1-january-1952' AND '31-december-2012'
ORDER BY DateofBirth
/* SET NOCOUNT ON */
RETURN


How do I use two datetimepickers on the VB form to replace the fixed dates in the WHERE clause in the code above?

Your help please.

Mike
Post #1403227
Posted Saturday, January 5, 2013 7:44 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 29, 2014 5:21 AM
Points: 197, Visits: 730
hi Mike,

you try to do it the wrong way. You should not try to modify your SQL-Code from outside. You can define input parameters in SQL-Procedures too.

ALTER PROCEDURE dbo.DateRange
@inFromDate datetime,
@inToDate datetime
as
set nocount on;

SELECT FirstName, LastName, DateofBirth
FROM Table1
WHERE DateofBirth BETWEEN @inFromDate AND @inToDate
ORDER BY DateofBirth

RETURN

Know you just have to find out how to pass over the date values from Visual Basic to the procedure. As this is nothing unusual you should find your way trough.
Post #1403232
Posted Saturday, January 5, 2013 8:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 1:20 AM
Points: 7, Visits: 22
Thanks for the prompt response.

I assume that the two statements directly below the procedure name, "@inFromDate" and "@inToDate" are just two variables for the WHERE clause to use. Where do they get their values from?

I have seen that method on the web but do not know how to get the dates from the two datetimepickers into those variables. This what I have been search for without success.

What I want to do is select the start and end dates using the 2 datetimepickers and then use a "Button_Click sub" to start the search. I am missing the code that is executed upon that button_click.

Can you help here please?
Post #1403236
Posted Saturday, January 5, 2013 9:08 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 29, 2014 5:21 AM
Points: 197, Visits: 730
michael.newman560 (1/5/2013)

I assume that the two statements directly below the procedure name, "@inFromDate" and "@inToDate" are just two variables for the WHERE clause to use. Where do they get their values from?


Yes, these are variables for the where clause. I don't know how to call a procedure using VB but maybe you find the anser here: http://support.microsoft.com/kb/146651/en-us
Post #1403237
Posted Saturday, January 5, 2013 9:14 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 1:20 AM
Points: 7, Visits: 22
Thanks. I will have a look at that.


Mike
Post #1403238
Posted Saturday, January 5, 2013 9:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 1:20 AM
Points: 7, Visits: 22
Hi Wolfgang,

I took a look at the link you gave me but that applied to VB version 4 so I guess I will have to keep looking.


Mike
Post #1403239
Posted Saturday, January 5, 2013 10:19 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:25 PM
Points: 43,008, Visits: 36,164
michael.newman560 (1/5/2013)
I assume that the two statements directly below the procedure name, "@inFromDate" and "@inToDate" are just two variables for the WHERE clause to use. Where do they get their values from?


They're passed as parameters from the application. You're looking for the ADO.net parameters collection.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1403251
Posted Wednesday, January 9, 2013 5:12 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 31, 2013 3:44 AM
Points: 314, Visits: 4,128
In some ways, your question is outside the scope of the Sql Forums here and you will be better off posing
the question "How can I call a sql stored procedure with parameters from VB" in a VB Forum.
However, ADO, as has been suggested previously is the technology you seek.
A google search could also provide you with pointers to a few code snippets that will help.
Post #1404702
Posted Wednesday, January 9, 2013 1:17 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 21, 2013 1:20 AM
Points: 7, Visits: 22
Thanks but that is what I did before coming to this forum. I will go back there and keep trying.
Post #1404971
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse