[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1104
  • Last Modified:

Runtime error 6 Overflow

Am I missing something very obvious here??

strvdatenear = "20040927"
MsgBox Val(Format$(strvdatenear, "YYYYMMDD"))

gives me runtime error 6.. overflow in windows xp..

Thanks
Kwaish
0
kwaish
Asked:
kwaish
  • 2
  • 2
  • 2
  • +5
3 Solutions
 
ClifCommented:
I don't know if it's all that obvious, but "20040927" isn't a valid date format.
0
 
Erick37Commented:
Right.

This works for me:

Dim strvDateNear As Date
strvDateNear = #9/27/2004#
MsgBox Val(Format(strvDateNear, "YYYYMMDD"))

this too:

Dim strvDateNear As String
strvDateNear = "2004/09/27"
MsgBox Val(Format(strvDateNear, "YYYYMMDD"))
0
 
bkthompson2112Commented:
If you don't declare strvdatenear, it will be created as a Long
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Erick37Commented:
From MSDN:
If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Deftype statement.

0
 
bkthompson2112Commented:
Oops! You're right.
Took one too many stupid pills today :)
0
 
Rick_TownsendCommented:
Two points here, really:
1) I assume you're trying to convert a string to a date, and then output that date (which is why you're using the Val function
2) Using a dollar sign ($) after a function or variable is equivalent to declaring it as a String.  (If anyone's interested, we can discuss the merits & drawbacks to BASIC's shorthand.)

So, you're telling VB to convert a Varian, which it will treat as a String because it is storing a String value, to a String (because you used format$), with a date format.  That doesn't make sense, but the BASIC compiler will make guesses as to what you really meant, and drop the dollar sign.

As for the Overflow, the User Defined Date, Time and Number formats require some sort of separator between the formats, UNLESS the interpreter knows what the type of the incoming variable is.  In that case, it compensates by guessing where your formats are separated.

Lost?  Basically, if a String is going into the Format() function, and the format is "YYYYMMDD", VB doesn't know that the individual formats are "YYYY""MM""DD".  That's why Erick37's examples work.  In the first one, the incoming variable is a DATE, so the interpreter already has the format spelled out, and is matching date formats to your custom format.
I'm guessing that the second example is handled similarly, that the interpreter sees a String that it can easily convert to a DATE (assuming that "/" is the date separator defined for Erick37's locale), does the conversion, and is able to guess at your format.
0
 
Rick_TownsendCommented:
Whoops, misread one of my own comments.  Why _are_ you using Val()?  It's designed to return numbers from a String.  If that's what you want, why use the Format() function?
0
 
remoterportsCommented:
Dates in VB are Doubles
?cdbl(now)
  38253.8587731481
  38,253 Days have passed since 1900 and today is about 85.877% over

 ?.8587731481 * 24
  20.6105555544
 About 20.611 Hours have passed so far today
? format(.8587731481 ,"HH:MM")
 20:36

 ? format(38253.8587731481, "YYYYMMDD HH:MM")
20040923 20:36

I don't know why you need the numeric but this might be a way to get you there.

BTW  ( 38253.8587731481 -1) = yesterday at this time

0
 
JR2003Commented:



I'm not sure what you trying to do.
The format function you have will convert a date to the format yyyymmdd. It will not take a string in the format yyyymmdd and convert it to a date!

The following will work:

strvdatenear = "2004-09-27"
MsgBox Val(Format$(strvdatenear, "YYYYMMDD"))

strvdatenear = "27 Sep 04"
MsgBox Val(Format$(strvdatenear, "YYYYMMDD"))

If you want to conver a string to a date then the following will work

MsgBox CDate("2004-09-27")



.
0
 
anvCommented:
hi  kwaish

may i know what exacly u want to achieve with the above code of urs??
0
 
kwaishAuthor Commented:
Guys.. thanks for all the comments... Basically I am trying to debug an age old application which is huge in size and they are doing this at many places.. Someone else's code.. I clearly do not know what they r trying to do.. Anyways

I have the value initially as "20040927".. And after all that it does what it returns is the same "20040927"..

So basically msgbox strvdatenear will give me what i want.. No need for val. No need for Format.. Other than that I can only think of checking with the person who wrote it..

Eric and  Rick.. Thanks for ur inputs..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now