Results 1 to 9 of 9

Thread: Sorting Dates

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    8

    Sorting Dates

    Hi,

    I am wondering if anyone could help me.

    I am using a DataGrid to display employee expenses. Each Expense has an "Incurred_Date". A the moment my grid shows all the expenses for a particular employee.

    How do I go about showing an employee's expenses just for one month??

    I have a dropdown list with the 12 months of the year and was thinking that if i click on a month, it would compare it to the database and then display the necessary data.

    If someone could show me some sample code I would really appreciate it.

    Thank You!

  2. #2
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: Sorting Dates

    How are you populating the datagrid now?
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    8

    Re: Sorting Dates

    Not sure if this is what you mean but this is my code!


    strSQL = "SELECT Expenses.EmpID,Expenses.ExpID, Expenses.Date_Incurred, Expenses.RateID, Rates.Description, Expenses.Mileage, Expenses.Total_Cost FROM Expenses INNER JOIN Rates ON Expenses.RateID = Rates.RateID WHERE EmpID = " & empExp & " "
    cmdComm.ActiveConnection = conConnect
    cmdComm.CommandText = strSQL

    Dim rs As New ADODB.Recordset
    rs.CursorLocation = adUseClient
    rs.Open strSQL, conConnect

    ident = "" & rs!EmpID
    If ident Is Nothing Then
    MsgBox "There is no such Record! Enter another I.D.", vbInformation, "No Record"
    Else
    Set dgdExpAd.DataSource = rs 'was rstRec
    dgdExpAd.ReBind
    'Display the results on the grid
    dgdExpAd.Visible = True
    End If

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sorting Dates

    Ok. Then unless I'm missing something it would seem that you could get what you wanted but adding an AND to your SQL. Something like
    VB Code:
    1. strSQL = "SELECT Expenses.EmpID,Expenses.ExpID, Expenses.Date_Incurred, "
    2. strSQL = strSQL & "Expenses.RateID, Rates.Description, Expenses.Mileage, "
    3. strSQL = strSQL & "Expenses.Total_Cost "
    4. strSQL = strSQL & "FROM Expenses INNER JOIN Rates ON Expenses.RateID = Rates.RateID "
    5. strSQL = strSQL & "WHERE EmpID = " & empExp & "
    6. strSQL = strSQL & "AND datefield BETWEEN #startdate# AND #enddate# "
    7. cmdComm.ActiveConnection = conConnect
    8. cmdComm.CommandText = strSQL'etc
    How you would encapsulate your two dates would depend on the DB field types (i.e., depending on whether your date field is text or date/time)

    PS: I broke your sql statement into different lines simply to make it easier to read in this forum thread. That isn't something you necessarily have to do in your code.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    8

    Re: Sorting Dates

    Thanks for your help..

    I am nit getting an error but my grid is just not showing any detail! This is my SQL statement.

    strSQL = "SELECT Expenses.EmpID,Expenses.ExpID, Expenses.Date_Incurred, Expenses.RateID, Rates.Description, Expenses.Mileage, Expenses.Total_Cost FROM Expenses INNER JOIN Rates ON Expenses.RateID = Rates.RateID WHERE EmpID = " & empExp & ""
    strSQL = strSQL & " AND Expenses.Date_Incurred BETWEEN " & DTPicker2.Value & " AND " & DTPicker1.Value & " "

    As I am usind date pickers, do i need to change the way the date puts its values into the data base..I am using date/time types

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sorting Dates

    I went back through everything that had been posted and couldn't see where what kind of database you are using was mentioned. How you deal with dates if different between Microsoft product (SQL Server and Access) and Oracle.

    I'm going to make the assumption you are using a Microsoft product when I make the suggestion to encapsulate your datepicker values in # signs. See if that works.

  7. #7
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602

    Re: Sorting Dates

    Try this:

    strSQL = strSQL & " AND Expenses.Date_Incurred BETWEEN " & "#" & DTPicker2.Value & "# AND #" & DTPicker1.Value & "#"
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    8

    Re: Sorting Dates

    Thank you so much..that worked a charm. Legends!

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sorting Dates

    Cool!

    Please edit your first post, and add {RESOLVED} to the original text of the subject line, and then add the green checkmark icon.

    By the way: Welcome to the forums.

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