Results 1 to 19 of 19

Thread: [2005] Querying A Table

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Question [2005] Querying A Table

    Hi, i'm using the date picker to try and allow the user to select two dates and the let them run a table query to find all the records between the two dates.

    I got the date picker outputing the strings date1 & date2 and these are getting displayed in text boxes.

    1.) How do i get rid of the time that is thrown in with the date picker? only need the date.

    2.) how can i create a query that allows me to have the String 'date1' included in it without defining any values.

    e.g.
    SELECT BookingID, [Date Of Booking], [Date of Arrival], [Date of Departure], CustomerID
    FROM [Booking Table]
    WHERE Date_Of_Booking >= date1 AND Date_Of_Booking <= date2


    Thanks for the any help,

    Ewan

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Querying A Table

    You can use:

    TextBox1.Text = Me.DateTimePicker1.Value.Date.ToShortDateString

    Not eactly sure what you mean on point 2. Where do you define Date1 & date2?

    When I'm taking values from textboxes I use parameters:


    vb Code:
    1. ' Set the SELECT statement for the Command object
    2.                 cmdUserSelect.CommandText = "SELECT myID, " _
    3.                     & "firstName, secondName " _
    4.                     & "FROM newTbales WHERE firstName = @newName"
    5.  
    6.                 cmdUserSelect.Parameters.AddWithValue("@newName", Me.searchField.Text)
    Last edited by stimbo; Apr 4th, 2007 at 04:38 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    thanks for the help got it displaying the short date now, probabily a few more questions to come.
    Last edited by ewankirky; Apr 4th, 2007 at 05:03 AM.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Querying A Table

    I think you can use the BETWEEN keyword in SQL statements.

    Which would roughly look like:

    ...WHERE Date_Of_Booking BETWEEN Date1 AND Date2
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    thanks i'll give it a try.


    Sorry forgot to answer your question about point 2.
    Just threw this "Dim date1 As String" at the start of the class and then read the value of the datetimepicker and made date1 = datetimepicker.value

    could you tell me how i'd do a very simple text box search?

    Say type a name into textbox1 and the click a search button and that would return all the records FirstName = search.

    pretty new to VB and trying to create a CRM system for uni.

    also is there any way to get back into a query after it has been created on the toolstrip or as a button?

  6. #6
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Querying A Table

    The SQL query would probably be something along the lines of:

    vb Code:
    1. mySQLstatement.CommandText = "SELECT first_Name FROM MyTable WHERE first_Name = '" & Me.name.Text & "'"

    Or:

    vb Code:
    1. cmdUserSelect.CommandText = "SELECT first_Name FROM MyTable WHERE first_Name = @firstName"

    vb Code:
    1. 'And then add the parameter to your command
    2. cmdUserSelect.Parameters.AddWithValue("@firstName", Me.searchField.Text)
    Last edited by stimbo; Apr 4th, 2007 at 10:28 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    Quote Originally Posted by stimbo
    The SQL query would probably be something along the lines of:

    vb Code:
    1. SELECT first_Name FROM MyTable WHERE first_Name = '" & Me.name.Text & "'"

    Or as shown above:

    vb Code:
    1. SELECT first_Name FROM MyTable WHERE first_Name = @firstName"
    2.  
    3. 'And then add the parameter to your command
    4. cmdUserSelect.Parameters.AddWithValue("@firstName", Me.searchField.Text)
    what do i put in place of cmdUserSelect?

    saying its not declared.

    thanks alot,

    really not great at this program at all!

  8. #8
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Querying A Table

    That's the name of MY SQL declaration command.

    You will have your own I would imagine... might look something like this?

    vb Code:
    1. Dim mySQLcommand As New SqlClient.SqlCommand()
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    right got that bit working, but now it doesnt show the name i type in when i click search, just displays a blank table....

    any chance you would have a look at it for me if your not too busy?

  10. #10
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Querying A Table

    Can't right now I'm afraid (actually working and leaving for meeting). Post it on here anyway and I'm sure someone will respond.

    I know if sounds stupid but the name definitely exists that you are searching for...
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    Quote Originally Posted by stimbo
    Can't right now I'm afraid (actually working and leaving for meeting). Post it on here anyway and I'm sure someone will respond.

    I know if sounds stupid but the name definitely exists that you are searching for...
    Yeh tried a few that existed. no luck.

    thanks for all your help today anyway.

  12. #12
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Querying A Table

    If it's any consolation I only started SQL a couple of weeks ago... it does get easier (a little)

    Here's a link to one of the VB members. Check out his signature. It has the best SQL/ADO tutorials I've read. Really simple to follow. Just take it slow:
    http://www.vbforums.com/member.php?u=30656
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    Hi, still cant get this working..... doesnt seem to be displaying anything in the tables when i type in the name of a person....

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    Anyone know why this wont work?

    SELECT BookingID, [Date Of Booking], [Date of Arrival], [Date of Departure], CustomerID
    FROM [Booking Table]
    WHERE ([Date of Arrival] >= Me.DateTimePicker3.Value)


    DateTimePicker
    Private Sub DateTimePicker3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker3.ValueChanged
    date1 = DateTimePicker3.Value
    TextBox3.Text = Me.DateTimePicker3.Value.Date.ToShortDateString
    End Sub

    EDIT: BUMP!
    Last edited by ewankirky; Apr 10th, 2007 at 07:53 AM.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    Anyone?

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Querying A Table

    I'm not going to read this entire thread but there should be NO strings involved at all. Do NOT convert dates to strings unless you want to display them. In all other circumstances keep then as binary date objects:
    vb Code:
    1. Dim cmd As New SqlCommand("SELECT * FROM MyTable WHERE DateColumn BETWEEN @StartDate AND @EndDate", con)
    2.  
    3. cmd.Parameters.AddWithValue("@StartDate", startDatePicker.Value.Date)
    4. cmd.Parameters.AddWithValue("@EndDate", endDatePicker.Value.Date)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: [2005] Querying A Table

    Quote Originally Posted by jmcilhinney
    I'm not going to read this entire thread but there should be NO strings involved at all. Do NOT convert dates to strings unless you want to display them. In all other circumstances keep then as binary date objects:
    vb Code:
    1. Dim cmd As New SqlCommand("SELECT * FROM MyTable WHERE DateColumn BETWEEN @StartDate AND @EndDate", con)
    2.  
    3. cmd.Parameters.AddWithValue("@StartDate", startDatePicker.Value.Date)
    4. cmd.Parameters.AddWithValue("@EndDate", endDatePicker.Value.Date)
    thanks this helps, what does the con bit mean. saying its not declared.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Querying A Table

    What is the second parameter to the SqlCommand constructor? It's an SqlConnection object. YOUR SqlConnection object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19
    New Member
    Join Date
    Jun 2007
    Posts
    2

    Re: [2005] Querying A Table

    Hi

    I have a data base, Ms Access, with forms and tables and macro... and all.

    But I want to create a search button, using VB, to bring specific data from one of my tables to be viewable in a form I already created.
    For example:
    Example.mdb
    FirstTable has:
    ID, EMployeeName, EmployeeNumber, EmployeeAddress
    The Form has to have a text box and the search button. It's purpose is to the User to enter the number of the employee and search it in the database. This will bring the Show-Employee-Information Form with the desire employee information.

    I create the command button and selected the OnClick option...but that's it.
    I'm kind of stuck there... I'm new at this and would really appreciate any advise.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width