![]() |
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. |
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. |
have you tried
WHERE (getdate() > date1) |
2 Attachment(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 --------------------------------------------------------------- |
Quote:
|
|
Quote:
:confused0068: 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. |
Quote:
|
Quote:
|
@FrX
Quote:
Quote:
Hope this helps. |
Quote:
|
| All times are GMT -4. The time now is 11:05 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
User Alert System provided by
Advanced User Tagging v3.3.0 (Lite) -
vBulletin Mods & Addons Copyright © 2026 DragonByte Technologies Ltd.