|
Off-Topic Lounge [WARNING: NO POLITICS] For all off-topic discussion topics. |
|
Thread Tools | Search this Thread |
06-04-2013, 12:10 PM | #1 |
Senior Member
Join Date: Oct 2012
Drives: 2013 Ultramarine FRS
Location: Grants Pass, OR, USA
Posts: 654
Thanks: 140
Thanked 180 Times in 110 Posts
Mentioned: 7 Post(s)
Tagged: 0 Thread(s)
|
Visual Basic and SQL
Does anybody know how to pass a variable from a VB form (datetimepicker) into an SQL query?
Heres what i've got and it doesn't quite work: SELECT FirstName, LastName, Phone, DateJoined FROM Members WHERE (DateJoined < date1) Bascially i just want to get all the records before the date that user selects from the date time picker that is stored in date1.
__________________
|
06-04-2013, 12:41 PM | #2 | |
Senior Member
Join Date: Jun 2012
Drives: 2013 Subaru BRZ Limited
Location: MA
Posts: 2,974
Thanks: 972
Thanked 1,552 Times in 843 Posts
Mentioned: 164 Post(s)
Tagged: 0 Thread(s)
|
Quote:
Dim strSQL as string = "SELECT blah FROM blah WHERE blah = " & variable Alternatively, you could use a stored procedure with a parameter, but will still need an object to interface with it. In most of my applications, I use some form of abstraction to handle all my database functions/needs. Of course choosing your solution should be based on the requirements of the project, scalability, etc. So it's solely your discretion. Also SQL is a pretty arbitrary description of what database you're trying to run against. Some of the less popular databases (I'm looking at you, Sybase), need the date formatted in a certain way before it will be able to analyze it in a WHERE clause. The VB version also makes a difference. |
|
06-04-2013, 01:35 PM | #4 | |
Senior Member
Join Date: Oct 2012
Drives: 2013 Ultramarine FRS
Location: Grants Pass, OR, USA
Posts: 654
Thanks: 140
Thanked 180 Times in 110 Posts
Mentioned: 7 Post(s)
Tagged: 0 Thread(s)
|
Quote:
Google hasn't given me anything that that really makes sense to me as far as setting up the ADODB object. I'm using VS 2012. Code from the form that i'm working on. Public Class MembersDates Dim retVal As New Date Private Sub gtnGo_Click(sender As Object, e As EventArgs) Handles gtnGo.Click ' Me.MembersTableAdapter.Fill(Me.FindMemberDataSet.M embers, txtLastName.Text) dtpDate.Value = Now() 'Takes the value of whatever is in the datetimepicker Dim date1 As Date = dtpDate.Value '2forme's suggestion, not sure how to implement further Dim strSQL As String = "SELECT FirstName, LastName, Phone, DateJoined FROM Members WHERE " & date1 & " > DateJoined" 'just to see what the output looks like as a string, in a textbox on the form. txtDate.Text = dtpDate.Value.ToString() Me.MembersTableAdapter.Fill(Me.DateJoinedDataSet.M embers) End Sub Public Function getDate() 'Earlier attempt, obviously didn't work, no longer calling txtDate.Text = dtpDate.Value.ToString() retVal = txtDate.Text Return retVal End Function End Class ---------------------------------------------------------------
__________________
|
|
06-04-2013, 01:37 PM | #5 |
Senior Member
Join Date: Oct 2012
Drives: 2013 Ultramarine FRS
Location: Grants Pass, OR, USA
Posts: 654
Thanks: 140
Thanked 180 Times in 110 Posts
Mentioned: 7 Post(s)
Tagged: 0 Thread(s)
|
Yup, it doesn't seem to understand the date1 variable. If i plug in a static number in the sql, it works fine, but not with a variable.
__________________
|
06-04-2013, 06:17 PM | #7 | |
Senior Member
Join Date: May 2012
Drives: 2013 Scion FR-S, 1993 Lexus SC300
Location: Houston, TX
Posts: 411
Thanks: 284
Thanked 175 Times in 102 Posts
Mentioned: 5 Post(s)
Tagged: 0 Thread(s)
|
Quote:
As someone in the security industry. Please, please, please do not do this. While it will likely not matter for a local application, it is a bad habit to get into. Please use parameters and prepared statements. Sorry...after investigating about the 100th SQL injection caused by this exact method, you tend to get a bit touchy. |
|
06-04-2013, 07:03 PM | #8 | |
Senior Member
Join Date: Jun 2012
Drives: 2013 Subaru BRZ Limited
Location: MA
Posts: 2,974
Thanks: 972
Thanked 1,552 Times in 843 Posts
Mentioned: 164 Post(s)
Tagged: 0 Thread(s)
|
Quote:
|
|
06-04-2013, 07:59 PM | #9 | |
Senior Member
Join Date: Oct 2012
Drives: 2013 Ultramarine FRS
Location: Grants Pass, OR, USA
Posts: 654
Thanks: 140
Thanked 180 Times in 110 Posts
Mentioned: 7 Post(s)
Tagged: 0 Thread(s)
|
Quote:
__________________
|
|
06-10-2013, 11:11 AM | #10 | |||
Senior Member
Join Date: Aug 2010
Drives: 4 Wheels Auto
Location: Canada
Posts: 1,191
Thanks: 251
Thanked 274 Times in 187 Posts
Mentioned: 4 Post(s)
Tagged: 0 Thread(s)
|
@FrX
Quote:
Quote:
Hope this helps.
__________________
Quote:
|
|||
06-10-2013, 12:18 PM | #11 | |
Senior Member
Join Date: Oct 2012
Drives: 2013 Ultramarine FRS
Location: Grants Pass, OR, USA
Posts: 654
Thanks: 140
Thanked 180 Times in 110 Posts
Mentioned: 7 Post(s)
Tagged: 0 Thread(s)
|
Quote:
__________________
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Visual Garage Shark fin antenna | EvoXDD | Cosmetic Modification (Interior/Exterior/Lighting) | 86 | 08-02-2015 02:20 PM |
GC basic coilovers / Cusco Strut bar | Evilbrz151 | Brakes, Suspension, Chassis | 3 | 03-29-2013 09:35 PM |
Question about basic performance mods | bdanisi | Engine, Exhaust, Transmission | 2 | 01-25-2013 05:40 PM |
visual garage shark fin group buy. | Mr.Jay | Exterior Parts - The Rest | 39 | 07-02-2012 10:09 PM |