Results 1 to 10 of 10

Thread: SUGGEST to get min max date from txt string

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    SUGGEST to get min max date from txt string

    I use the tipical free file eof .... to read line by line a txt file.

    During the loop a get in a mid postion of string a date form each lines and store the date value it in mYDate var.

    How to get the min and max date when the loop is finished?

    Peraphs i can store the date value in a collection, in a dictionary...

    note:
    In txt are approx 350.xxx lines

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: SUGGEST to get min max date from txt string

    Code:
    Private Sub Routine()
        Dim MaxDate As Date, MyDate As Date
        Dim MinDate As Date, sLine  As String
    
        Open "File.txt" For Input As #1
            If Not EOF(1) Then
                Line Input #1, sLine
                MyDate = CDate(Mid$(sLine, 1&))
               'Initialize both MaxDate & MinDate to MyDate
                MaxDate = MyDate
                MinDate = MyDate
               'Do whatever needs to be done with MyDate here
            End If
    
            Do Until EOF(1)
                Line Input #1, sLine
                MyDate = CDate(Mid$(sLine, 1&))
    
                If MyDate > MaxDate Then
                    MaxDate = MyDate
                ElseIf MyDate < MinDate Then
                    MinDate = MyDate
                End If
    
               'Do whatever needs to be done with MyDate here
            Loop
        Close #1
    End Sub
    Last edited by Bonnie West; Mar 25th, 2014 at 04:56 AM. Reason: Initialized MaxDate & MinDate
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: SUGGEST to get min max date from txt string

    Luca

    One possible way would be to create 2 more vars .. myMaxDate, and myMinDate. They probably should be Date type.

    Then, each time you update your myDate var, test it against the two
    new vars.
    1. If myDate < myMinDate .. replace myMinDate with myDate
    2. If myDate > myMaxDate .. replace myMaxDate with myDate.

    BTW, the first time you "get" myDate, you'll want to initiate both MyMinDate and MyMaxDate with that first date.

    EDIT:

    Ha! Bonnie definitely types faster than I do.

    Spoo

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

    Re: SUGGEST to get min max date from txt string

    You might need to set MinDate to some high value before the loop.

    This part confused me though:

    Code:
    MyDate = CDate(Mid$(sLine, 1&))
    Why not use:

    Code:
    MyDate = CDate(sLine)
    What am I missing?

    Also, writing simple constants with & meaning Long (as in 1& above) is overkill. It is a rare situation when the compiler doesn't know what type to use, typically only when using the ByVal override in API calls. It doesn't hurt but it adds a lot of clutter.
    Last edited by dilettante; Mar 24th, 2014 at 03:42 PM. Reason: oops, too slow!

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: SUGGEST to get min max date from txt string

    Quote Originally Posted by Spoo View Post
    BTW, the first time you "get" myDate, you'll want to initiate both MyMinDate and MyMaxDate with that first date.
    Quote Originally Posted by dilettante View Post
    You might need to set MinDate to some high value before the loop.
    I think initialization of both Max and Min dates aren't necessary. I can't see how leaving them uninitialized can affect the final outcome.

    Quote Originally Posted by dilettante View Post
    What am I missing?
    Quote Originally Posted by luca90 View Post
    During the loop a get in a mid postion of string a date form each lines and store the date value it in mYDate var.
    Quote Originally Posted by dilettante View Post
    Also, writing simple constants with & meaning Long (as in 1& above) is overkill. It is a rare situation when the compiler doesn't know what type to use, typically only when using the ByVal override in API calls. It doesn't hurt but it adds a lot of clutter.
    Well, I've gotten used to doing it that way and I'm not sure I'd want to break this habit. Anyway, it's just an example and it isn't the most important point here.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: SUGGEST to get min max date from txt string

    Bonnie

    If you don't initialize MinDate, then wouldn't there be a problem (using your code snippet) comparing myDate to MinDate in the line ElseIf myDate <= MinDate Then ,, ?

    MinDate would be empty, and thus would never be assigned a value, right?

    Spoo

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: SUGGEST to get min max date from txt string

    Quote Originally Posted by Spoo View Post
    MinDate would be empty, and thus would never be assigned a value, right?
    Um, no. Date variables actually have a default value (December 30, 1899 12:00:00 AM) so there shouldn't be any problem comparing minimum and maximum dates. If MyDate happened to be less than or exactly 12/30/1899 12:00:00 AM, then it becomes the MinDate. If it happened to be later than that date, then it becomes the MaxDate. MinDate/MaxDate would only be Empty if they were declared as a Variant.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: SUGGEST to get min max date from txt string

    Quote Originally Posted by Bonnie West View Post
    Um, no. Date variables actually have a default value (December 30, 1899 12:00:00 AM) so there shouldn't be any problem comparing minimum and maximum dates. If MyDate happened to be less than or exactly 12/30/1899 12:00:00 AM, then it becomes the MinDate. If it happened to be later than that date, then it becomes the MaxDate. MinDate/MaxDate would only be Empty if they were declared as a Variant.
    Right... so think about that.... if the min date in the data is Feb 2, 2000 ... it would never get found now would it? Because MyDate would NEVER be less than the default date... ergo... when you get the first date from the data stream... you set both Min and Max to that date, then do your comparisons from there... eventually the min date would work its way down to Feb 2, 2000 .... Otherwise the date would have to be nearly two centuries old in order for it to become the min date.


    -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??? *

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: SUGGEST to get min max date from txt string

    Quote Originally Posted by techgnome View Post
    Right... so think about that.... if the min date in the data is Feb 2, 2000 ... it would never get found now would it? Because MyDate would NEVER be less than the default date... ergo... when you get the first date from the data stream... you set both Min and Max to that date, then do your comparisons from there... eventually the min date would work its way down to Feb 2, 2000 .... Otherwise the date would have to be nearly two centuries old in order for it to become the min date.


    -tg
    Ah, now I get it. So Spoo's earlier advice was actually right. Thanks for the correction! The code in post #2 has now been amended.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: SUGGEST to get min max date from txt string

    Bonnie

    I knew you'd get it
    My earlier post was a little too terse ,, TG explained it much better.

    Spoo

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