Results 1 to 12 of 12

Thread: Compare filename date to todays date

  1. #1

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Compare filename date to todays date

    I am trying to make a personal reminder program.
    Compare file names to todays date

    I have files that are saved with the files date as the name
    the file names are loaded into a listbox
    02-20-2015.rtf
    02-21-2015.rtf
    02-22-2015.rtf
    02-20-2016.rtf


    Private Sub lstTasks_Click()
    Dim myDate As Date
    myDate = Len(lstTasks.Text) - 4 'minus extension
    If myDate = Date Then ' a new task to handle
    Call MsgBox("You have a scheduled task to consider", vbInformation, "Needs attention")
    'call load the rtf file here to richtextbox
    End If
    End Sub

    this code produces
    Debug.Print myDate = 1/9/1900
    how can this be fixed ?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Compare filename date to todays date

    Your code is looking at the text property of something and assigning myDate to the len of that text -4

    There is no way that would produce a date

    Assuming that the text was one of those file names then myDate would =10 in all cases but you are using a date var so you may get an unexpected result

    What you want is
    Code:
     MyDate=CDate(Left$(lstTasks.Text,Len(lstTasks.Text) - 4))
    That would extract the date portion from the filename and then you can compare it.
    If you want to check all the entries then you would need to loop through the list

    Would probably be better to first grab todays date and then test to see if it occurs in any of the list entries which you could do by using Instr() or by checking the Left$() 10 characters of the filename to the formatted date

  3. #3

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Compare filename date to todays date

    Thanks that works
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Compare filename date to todays date

    Of course using CDate() on a string value is rather unsafe.

    A "date" string like "01-06-2002" may just as easily convert to June 1st as Jan 6th depending on your current UI settings for date formats.

    Don't do this. You are building bugs into your code and bad habits into your brain.

  5. #5

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Compare filename date to todays date

    Quote Originally Posted by dilettante View Post
    Of course using CDate() on a string value is rather unsafe.

    A "date" string like "01-06-2002" may just as easily convert to June 1st as Jan 6th depending on your current UI settings for date formats.

    Don't do this. You are building bugs into your code and bad habits into your brain.
    Good Point
    How would you write it ?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Compare filename date to todays date

    I'd probably use a database and forget creating a raft of tiny RTF files. Then things can be "dated" using actual Date values.

    But barring that, I'd want to be very sure the program always creates those file names in exactly the same format no matter what locale is used. Format$() should be good enough for creating text date formats with all numeric subfields. Then to examine them as "dates" I'd parse the pieces and use DateSerial() to combine them back into a Date value. With the layout you have chosen a Split() on "-" may be all the parsing you need.

    That or use something like VConvert, A Class for Locale-Specific Data Conversions which allows your program to use a fixed locale such as LOCALE_INVARIANT which is normally suggested for portable data formats.

  7. #7
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    759

    Re: Compare filename date to todays date

    I'd name the files using a definitive format: yyyy-mm-dd.

    Regards, Phill W.

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Compare filename date to todays date

    Agree with Phill, additional advantage that the files can be sorted on name to get them chronologically

  9. #9

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Compare filename date to todays date

    This is a personal app and the dates are entered using a calendar control so the dates are always in the same format. if it was distributed then it would take more planning.
    The dates are entered in this format "mm/dd/yyyy", but cannot be saved with the slashes in the date filename
    this took care of that:
    myDate = (Format(txtTaskDate, "mm-dd-yyyy"))
    thanks everyone for the feedback
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Compare filename date to todays date

    While it might seem a clever cop out, "for personal use" is no excuse for bad programming. It just means you'll be back later when such flaws end up biting your metaphorical behind.

    Code:
    myDate = (Format(txtTaskDate, "mm-dd-yyyy"))
    Terrible, just terrible.

    You must either parse the input text and create actual Date values from it or punt and use a DateTimePicker instead of a TextBox.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Compare filename date to todays date

    I am a bit confused by why the code is in the list click event and why it is using the text property. If the goal is to see if there are any tasks that match todays date then it would seem making the user click on that task to check it is very poor design. I mean after all the user can see the date before they click on it so what would be the point?

    I would also agree that if you are going to use files with the date as the name that you should use the year month day format for proper sorting.
    If you want to check and see if there are any entries that match todays date then you should not have the code in the list box click event and it should loop through the list until it either finds a match or reaches the end.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Compare filename date to todays date

    Yeah, the entire thing is confusing. It seems like some sort of an attempt to avoid using a database, dumping data into files and then using a ListBox as some sort of an index. But why users would be clicking on it escapes me too.

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