Runtime error 6 Overflow

Posted on 2004-09-23
Last Modified: 2008-02-01
Am I missing something very obvious here??

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

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

Question by:kwaish
  • 2
  • 2
  • 2
  • +5
LVL 10

Expert Comment

ID: 12136390
I don't know if it's all that obvious, but "20040927" isn't a valid date format.
LVL 32

Assisted Solution

Erick37 earned 50 total points
ID: 12136419

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"))

Expert Comment

ID: 12136432
If you don't declare strvdatenear, it will be created as a Long
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 32

Expert Comment

ID: 12136502
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.


Expert Comment

ID: 12136550
Oops! You're right.
Took one too many stupid pills today :)

Assisted Solution

Rick_Townsend earned 75 total points
ID: 12138000
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.

Accepted Solution

Rick_Townsend earned 75 total points
ID: 12138051
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?

Expert Comment

ID: 12139034
Dates in VB are Doubles
  38,253 Days have passed since 1900 and today is about 85.877% over

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

 ? 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

LVL 18

Expert Comment

ID: 12139129

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")

LVL 10

Expert Comment

ID: 12140171
hi  kwaish

may i know what exacly u want to achieve with the above code of urs??

Author Comment

ID: 12144459
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..

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
to transfer string from C lanaguage to VBA 4 64
Adding to a VBA? 6 69
vb6 connector to mongodb 2 79
Advice in Xamarin 21 78
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now