Results 1 to 6 of 6

Thread: [2005] Datetimerpicker+Access 2000 sql statement

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Resolved [2005] Datetimerpicker+Access 2000 sql statement

    Hey,

    I've run into a problem with extracting information from a database and inserting it into a listview. I am unsure if I am using the easiest way.

    Problem: I have 2 DateTimePickers and a button. One datetimepicker is for the start date, the other for the finish date. When the button is clicked by the user, I want to check the database for all transactions that have been made between the start date and the finish date. Hope that makes sense, here is the code so far.

    Controls:
    BtnDatesearch = Button user will click
    Startdate = Datetimepicker
    Finishdate = datetimepicker

    Code:
          Private Sub BtnDateSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDateSearch.Click
            Connection.CreateCommand()
            Command.CommandText = "SELECT * FROM Transactions WHERE Transaction_Date > " & StartDate.Text & " AND Transaction_Date < " & FinishDate.Text & ""
            Reader = Command.ExecuteReader
    
            While (Reader.Read)
                Dim value3 As Double = Reader.GetValue(3)
                Transactions.Items.Add(New ListViewItem(New String() {Reader.GetValue(0), Reader.GetValue(1), Reader.GetValue(2), value3.ToString("n2"), Reader.GetValue(4), Reader.GetValue(5)}))
            End While
            Reader.Close()
        End Sub
    I am not sure if you are able to search in-between 2 date values the way I attempted to, is there another way to do this?

    Another quick question if you don't mind.
    In Access 2000, how can I change the date format from American to Australian?

    Thanks
    Last edited by Wulfgur; Apr 12th, 2008 at 07:49 PM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Datetimerpicker+Access 2000 sql statement

    With Access database you have to enclose your date literal with the # signs. Thus you select statement should look something like this
    Code:
    "SELECT * FROM Transactions WHERE Transaction_Date > #" & StartDate.Text & "# AND Transaction_Date < #" & FinishDate.Text & "#"
    However, I strongly recommend you to use parameters in your query instead of string concatenation as it is now.

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

    Re: [2005] Datetimerpicker+Access 2000 sql statement

    I don't know much about access or DB's but I presume Transaction_Date is a column with a Date/Time data type?

    Anyway, should you be using the .Value property of the dateTimePicker rather than the Text property of the DTP:
    e.g. StartDate.Value
    Stim

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

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Re: [2005] Datetimerpicker+Access 2000 sql statement

    Thanks for the replies,

    Not sure if I am getting the operators correct, since the code is still not working. I coded it to find a transaction on a certain day and that worked.

    start = startdate.value (DateTimePicker)
    finish = finishdate.value (DateTimePicker)

    This code (Below) worked fine.
    Code:
    Command.CommandText = "SELECT * FROM Transactions WHERE Transaction_Date = #" & start & "#"
    However this code didn't:
    Code:
    Command.CommandText = "SELECT * FROM Transactions WHERE Transaction_Date > #" & start & "# AND Transaction_Date < #" & finish & "#"
    Transaction_Date is in the table 'Transactions' and is a field of data type Date/Time.

    Thanks.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Datetimerpicker+Access 2000 sql statement

    because you are looking for dates greater than.... rather than greater or equal to...you just need to add a couple of equal signs in there.

    Transaction_Date >= #" & start & "# AND Transaction_Date <= #" & finish

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Re: [2005] Datetimerpicker+Access 2000 sql statement

    Thanks for the help, seems to be working fine at the moment.

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