|
-
Feb 8th, 2016, 12:41 PM
#1
Thread Starter
PowerPoster
[RESOLVED] trying to determine the days between dates between now and the dates listed using vb6
this gives an overflow error
dates are in this format: 07-26-2016
Code:
Dim i As Integer
GRD_TIME_DATE = 3 'hard coded
Dim Days As Integer
With gridMaster
For i = 1 To .Rows - 1
Days = DateDiff("d", .TextMatrix(i, GRD_TIME_DATE), Now)
Next
End With
how can this be written ?
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 8th, 2016, 12:56 PM
#2
Re: trying to determine the days between dates between now and the dates listed using
When you format data for display you are "killing" it. It is now dead and you can't use it for general operations (calculations, etc.).
Grids and other UI controls need to have a data structure of some sort behind them to hold "living" data. In the absence of such a thing you have to write logic to "resurrect" dead data. This is called parsing.
Newbies often play fast and loose with text, hoping that the compiler will insert coercion logic for them, to take care of data resurrection as if by magic. This works after a fashion, right up until it begins to corrupt data or fail entirely.
This is what you have here.
What you'll have to do if you insist on trying to treat display elements as data structures is write parsing/resurrecting functions that can handle unscrambling your text into data.
In the simplest case you'd need to break your text into numeric substrings, apply a conversion function that can parse text to binary such as CInt() in this case, and then recombine the three resulting Integer values using the DateSerial() function.
Ideally you'd start with real (binary) data from some source like a database and only use your grid for display and retain the binary data for computation. However at some point data has to be supplied by a person and then this either requries a proper date-input control like DateTimePicker or else you are back to text parsing again. But it is far better to parse the data from text to binary forms during input and then retain the binary for all future use.
-
Feb 8th, 2016, 12:57 PM
#3
Re: trying to determine the days between dates between now and the dates listed using
You should convert the text to a date before using DateDiff
That said are all the values in column3 holding date values as your sample?
Have you steeped through the code or checked after the error to see which row the error is on and/or what the value of the variables are?
If not then that should be your first step.
-
Feb 8th, 2016, 02:00 PM
#4
Thread Starter
PowerPoster
Re: trying to determine the days between dates between now and the dates listed using
I am using datepicker to enter values
the grid only has 1 row for now
maybe i should change the format to 07/26/2016
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 8th, 2016, 02:41 PM
#5
Re: trying to determine the days between dates between now and the dates listed using
What DM says....
make sure all variables are dimensioned.
If you do something like this, it might give you the clues:
Code:
Dim myDate as Date
With gridMaster
For i = 1 To .Rows - 1
myDate = .TextMatrix(i, GRD_TIME_DATE)
Days = DateDiff("d", myDate, Now)
Next
End With
-
Feb 8th, 2016, 06:02 PM
#6
Re: trying to determine the days between dates between now and the dates listed using
The "overflow" error is the key I suppose.
A string like "07-26-2016" will probably coerce as expected, though values such as "07-12-2016" can yield unpredictable results which is why you can't rely on coercion. Depending on the locale this will be taken as either a date in July 2016 or a date in December 2016.
Most likely the overflow is because there are more days between two of your dates than will fit in an Integer, which can only contain values up to 32767.
-
Feb 8th, 2016, 06:31 PM
#7
Thread Starter
PowerPoster
Re: trying to determine the days between dates between now and the dates listed using
Most likely the overflow is because there are more days between two of your dates than will fit in an Integer, which can only contain values up to 32767.
You are right about the overflow
changed Days from integer to long and now Days = 657290
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 8th, 2016, 06:33 PM
#8
Thread Starter
PowerPoster
Re: trying to determine the days between dates between now and the dates listed using
Dim myDate as Date
With gridMaster
For i = 1 To .Rows - 1
myDate = .TextMatrix(i, GRD_TIME_DATE)
Days = DateDiff("d", myDate, Now)
Next
End With
tried that and still get the overflow problem
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 8th, 2016, 07:08 PM
#9
Re: trying to determine the days between dates between now and the dates listed using
Which line do you get the overflow on? What is days defined as there?
You should also use CDate() for date conversion rather than just assigning a string to a date.
Have you tried stepping through the code and checking variable content?
-
Feb 8th, 2016, 07:19 PM
#10
Thread Starter
PowerPoster
Re: trying to determine the days between dates between now and the dates listed using
Dim myDate as Date
With gridMaster
For i = 1 To .Rows - 1
myDate = .TextMatrix(i, GRD_TIME_DATE)
Days = DateDiff("d", myDate, Now) '<-overflow here
Next
End With
also tried this
MyDate = CDate(.TextMatrix(i, GRD_TIME_DATE))<-Type Mismatch error
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 8th, 2016, 08:00 PM
#11
Re: trying to determine the days between dates between now and the dates listed using
 Originally Posted by isnoend07
also tried this
MyDate = CDate(.TextMatrix(i, GRD_TIME_DATE))<-Type Mismatch error
That would indicate that your string is not in a valid format to be converted to a date.
This could also happen if you have a blank row in your grid
-
Feb 8th, 2016, 08:17 PM
#12
Thread Starter
PowerPoster
Re: trying to determine the days between dates between now and the dates listed using
That would indicate that your string is not in a valid format to be converted to a date.
This could also happen if you have a blank row in your grid
Had already checked that
Debug.Print .TextMatrix(i, GRD_TIME_DATE) produces 07-04-216
next line the error
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 8th, 2016, 09:13 PM
#13
Re: trying to determine the days between dates between now and the dates listed using
look at your 'date'....the year is 216....that is a LONG time ago.....
-
Feb 8th, 2016, 10:10 PM
#14
Re: trying to determine the days between dates between now and the dates listed using
Code:
Debug.Print CDate("7-20-2016")
Prints 7/20/2016
Code:
Debug.Print CDate("07-04-216")
Prints 7/4/216
So when you got a type mismatch there was something wrong with your data that made it invalid.
the year 216 could very well cause an overflow as liek Sam says that is a really long time ago. Need a bigger var to hold that value
Last edited by DataMiser; Feb 8th, 2016 at 10:13 PM.
-
Feb 8th, 2016, 10:31 PM
#15
Thread Starter
PowerPoster
Re: trying to determine the days between dates between now and the dates listed using
good eye, finally got it to work
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
-
Feb 8th, 2016, 10:34 PM
#16
Thread Starter
PowerPoster
Re: trying to determine the days between dates between now and the dates listed using
All you people have helped me. thank you
each got a message "to spread reputation"
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|