Results 1 to 16 of 16

Thread: Listing Data between two Dates

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Listing Data between two Dates

    I need to print out data from a access database that is between two dates.

    For example, If i have entries in my database at
    2/5/06 Swimming
    3/5/06 Dancing
    7/6/06 Fighting
    2/7/08 Skipping

    and I have a DTPpicker which defines my start date at "3/5/06" and another DTPpicker that defines my end date at "1/7/06" I want a picture or list box to print out

    3/5/06 Dancing
    7/6/06 Fighting

    Thank you!
    x
    Last edited by Andy1723; May 7th, 2006 at 10:20 AM.

  2. #2

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Printing Data between two Dates

    Hmm maybe if i used DatePart("d", dtpStartDate)

    startDate = DatePart("d", dtpStartDate)
    endDate = DatePart("d", dtpEndDate)

    Do until startDate = endDate

    then add 1 onto the startdate until it equals the number of days of the end date?

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Printing Data between two Dates

    Some thing like this

    "Select fieldname,fieldname from tablename where fieldname Between #" & datepicker start date & "# And #" & datepicker end date & "#"

    I did assume you are using Access as a backend that is why the date values are inside pound signs
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Printing Data between two Dates

    Quote Originally Posted by GaryMazzone
    Some thing like this

    "Select fieldname,fieldname from tablename where fieldname Between #" & datepicker start date & "# And #" & datepicker end date & "#"

    I did assume you are using Access as a backend that is why the date values are inside pound signs
    Access is not doing any of the work, just retrieving data from my visual basic programming. When I try using that I get an error saying "Expected Case".

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Printing Data between two Dates

    VB Code:
    1. startDate = DatePart("d", dtpStartDate)
    2. endDate = DatePart("d", dtpEndDate)
    3.  
    4.     Do Until startDate = endDate
    5.         While Not found And Not tblBooking.EOF
    6.             If tblBooking!Date = dtpStartDate Then
    7.                 found = True
    8.             Else
    9.                 tblBooking.MoveNext
    10.             End If
    11.         Wend
    12.     Loop

    I got this so far, but my program keeps crashing, any ideas why??

    Thanks you for everyone who's helping, i'm really stuck

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Listing Data between two Dates

    Is access storing the data? How are you retrieving data from the database? The code I showed is the way you would request a recordset from the database. Are you using ADO or DAO data access methods? How did you open the results to VB?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Listing Data between two Dates

    Quote Originally Posted by GaryMazzone
    Is access storing the data? How are you retrieving data from the database? The code I showed is the way you would request a recordset from the database. Are you using ADO or DAO data access methods? How did you open the results to VB?
    Access stores the data!

    VB Code:
    1. Option Explicit
    2. Public dbfVillageHall As Database
    3. Public tblCustDetails, tblPrices, tblBooking As Recordset
    4.  
    5. Sub Main()
    6.     Set dbfVillageHall = OpenDatabase(App.Path & "\villageHall.mdb")
    7.     Set tblCustDetails = dbfVillageHall.OpenRecordset("customerDetails")
    8.     Set tblBooking = dbfVillageHall.OpenRecordset("booking")
    9.     Set tblPrices = dbfVillageHall.OpenRecordset("prices")
    Thats how i'm retrieving it.
    ADO DAO?? Sorry I don't understand what they are.

    I want to print the results in a Picture box or any other tool suitable for listing the data!
    thank you!

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Listing Data between two Dates

    ADO and DAO are methods of accessing data stored in a database. DAO is/was used pretty much exclusivly for MSAccess and has been de-supported by MS. ADO is the preferred way of accessing data using VB6. To see which you have set you check the references in VB. I think you are using DAO and are just opening up complete tables.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Listing Data between two Dates

    Quote Originally Posted by GaryMazzone
    ADO and DAO are methods of accessing data stored in a database. DAO is/was used pretty much exclusivly for MSAccess and has been de-supported by MS. ADO is the preferred way of accessing data using VB6. To see which you have set you check the references in VB. I think you are using DAO and are just opening up complete tables.
    How do i find out??

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Listing Data between two Dates

    Check the reference me menu item. Even if using DAO you can use SQL statements to limit the data coming back from the database to just what you want and not all the extra stuff
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Listing Data between two Dates

    Quote Originally Posted by GaryMazzone
    Check the reference me menu item. Even if using DAO you can use SQL statements to limit the data coming back from the database to just what you want and not all the extra stuff
    I'd prefer to do it the way I posted above if it is do-able like that??

    I know its probably not the easiest way but my knowledge is really limited so it would be really hard for me to alter the results it obtains!

    Thank you so much though!

  12. #12

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Listing Data between two Dates

    Hmmm, I decided to try using a counter, still doesn't work, but i think its a better idea.


    heres my code:
    For counter = startDate To endDate
    While Not found And Not tblBooking.EOF
    If tblBooking!Date = dtpStartDate Then
    found = True
    Print "wow" 'debug
    Else
    tblBooking.MoveNext
    End If
    Wend
    Next counter

    hmm

  13. #13
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Listing Data between two Dates

    This is probably the harder way to do this.
    VB Code:
    1. While not Found and not tblBooking.EOF
    2.    If tblBooking!Date >= startate and If tblBooking!Date <=EndDate then
    3.       Do what you want here
    4.    end if
    5.    tblBooking.MoveNext
    6. Wend
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  14. #14

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Listing Data between two Dates

    Quote Originally Posted by GaryMazzone
    This is probably the harder way to do this.
    VB Code:
    1. While not Found and not tblBooking.EOF
    2.    If tblBooking!Date >= startate and If tblBooking!Date <=EndDate then
    3.       Do what you want here
    4.    end if
    5.    tblBooking.MoveNext
    6. Wend
    That can't work because startDate and endDate is just the number for the day, not the full date!

  15. #15

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    33

    Re: Listing Data between two Dates

    Because the interval should be a week between the dates i want to retrieve, I added this to your code

    VB Code:
    1. startDate = DatePart("d", dtpStartDate)
    2. endDate = startDate + 7
    3. monthNumber = DatePart("mm", dtpStartDate)
    4. yearNumber = DatePart("yyyy", dtpStartDate)
    5.  
    6. fullSDate = startDate + "/" + monthNumber + "/" + yearNumber
    7. fullEDate = endDate + "/" + monthNumber + "/" + yearNumber
    8.  
    9. full
    10.  
    11.     While Not found And Not tblBooking.EOF
    12.        If tblBooking!Date >= fullSDate And tblBooking!Date <= fullEDate Then
    13.             Print "wow"
    14.        End If
    15.             tblBooking.MoveNext
    16.     Wend

    Hopefully that could work?

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Listing Data between two Dates

    Use "&", not "+", to concatenate strings, but fullSDate = dtpStartDate, so you don't have to calculate it, and fullEDate is just DateAdd("d", 7, dtpStartDate).

    However, tblBooking!Date would have to be the full date, not just "the number for the day", for that code (either way, yours or mine) to work.

    What Gary said, though, saves you a lot of work, because you'd only have records that fell between those dates, so you wouldn't have to test anything.

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