Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDate function 3

Status
Not open for further replies.

Slippenos

MIS
Apr 22, 2005
333
US
The value of a form textbox looks like this:
Code:
Jul-01-2005
I want to insert this into an Access table in 'Short Date' format. The code looks like this:
Code:
Rs( "SampleDate2" ) = CDate(Request.Form( "SampleDate2" ))
I also set the field properties to 'Short Date'. I get the following error:
Code:
Microsoft VBScript runtime error on '800a00d'
Type mismatch: 'CDate'

Any thoughts?




[blue]There's no place like 127.0.0.1 ...[/blue]
 
You might try the FormatDateTime() function.

Code:
Rs( "SampleDate2" ) = FormatDateTime(CDate(Request.Form( "SampleDate2" )), vbShortDate)   'vbShortDate = 2

You might also try testing the input value with the IsDate() function ... just to make sure in case an empty value comes over from your form.
 
Make sure that Request.Form( "SampleDate2" ) is not null or else CDate() function will throw an error...

-DNG
 
Even if those values are not null, I get the same error. Why is this so?

[blue]There's no place like 127.0.0.1 ...[/blue]
 
Try this:

Rs( "SampleDate2" ) = DateValue(Request.Form( "SampleDate2" ))

-DNG
 
Type mismatch: 'DateValue'

[blue]There's no place like 127.0.0.1 ...[/blue]
 
What happens if you just do these as a test:
Code:
Rs( "SampleDate2" ) = Now

or

Code:
Rs( "SampleDate2" ) = FormatDateTime(Now, 2)


or

Code:
Rs( "SampleDate2" ) = Date

 
What happens if you run:
Code:
Response.write getLocale()
?

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
If DateValue gives a type mismatch then I'd imagine that the value coming across in the form is not recognized as a date.

You might need some crazy validation like this:
Code:
MyVar = Request.Form("MyDateField")
if (Len(MyVar) > 0) then
  If IsDate(MyVar) Then
    IF DateValue(MyVar) > 0 THEN
      'Finally I have a decent date!
    ELSE
      'Date is undefined state!
    END IF
  Else
    'String is not a date!
  End If
else
  'Nothing passed from the form!
end if

Bleh!
 
When DateValue() failed...I was wondering if he needed to use DatePart() function to get different portions of the date and then join them back...

-DNG

 
DateValue() simply extracts date from date+time. It won't help if source date string is not in recognizable format.

My initial thought was about regional settings. For example:
Code:
Dim sDate: sDate = "Jul-01-2005"
Setlocale( "en-us")
Response.write "us: " & IsDate( sDate ) & "<br>"
Setlocale( "fr")
Response.write "fr: " & IsDate( sDate ) & "<br>"
Setlocale( "en-gb")
Response.write "uk: " & IsDate( sDate ) & "<br>"
Setlocale( "ru")
Response.write "ru: " & IsDate( sDate ) & "<br>"

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Why can't I use the CDate function with a Null value?

Furthermore,

The DateValue function inserts the date even when it is not instructed to do so.

I wrote some script like this:

Code:
<script language = javascript>
<!--

if( drf.SampleDate.value.length > 0 )

	{

	document.forms.drf.elements['SampleDate2'].value = document.forms.drf.elements['SampleDate'].value;

	}


//-->
</script>

When I use
Code:
Rs( "SampleDate2" ) = CDate(Request.Form( "SampleDate2" ))
And it is null, I get an error. If it is not null- it works fine.

If I use:
Code:
Rs( "SampleDate2" ) = DateValue(Request.Form( "SampleDate2" ))
It will insert the date even when I don't want it to.

Any thoughts?

(Thanks for the good posts so far, all!!)


[blue]There's no place like 127.0.0.1 ...[/blue]
 
Cdate(expression) casts expression to a Date value...expression should be numeric or something that can be cast to a number, or a string of a commonly used date format. Thats the reason why it fails for NULLS...

i am confused..earlier you said you were getting a type mismatch when you used DateValue...how come it is working now...

-DNG
 
A date is really a number that represents the amount of time from some offset date. I think the default offset date for a VB date is December 1889 or 1899 or somesuch.
 
It wasn't working before due to a programming error on my behalf. When I fixed it, the above happened. I hope to resolve this issue on Monday and I'll let you all know what happens. Thanks for the great advice!

[blue]There's no place like 127.0.0.1 ...[/blue]
 
Sheco,

When I tried your validation- I got this error:
Code:
Provider error '80020005' 

Type mismatch.

My code looked like this:
Code:
Rs( "SampleDate2" ) = Request.Form("SampleDate2")

	If (Len(SampleDate2) > 0) then
  		If IsDate(SampleDate2) then
    			If DateValue(SampleDate2) > 0 then

	'Finally I have a decent date!

    			else

	'Date is undefined state!

    			End If

  			else

	'String is not a date!

  			End If

			else

  'Nothing passed from the form!

			End if

[blue]There's no place like 127.0.0.1 ...[/blue]
 
Ok, this is what I came up with and it worked:

Code:
If (Len (Rs( "SampleDate" )) > 1) Then

		Rs( "SampleDate2" ) = CDate(Request.Form( "SampleDate2" ))

	End If

It will only insert a Short Date value for the SampleDate is it is not null. If it is null, no value will be inserted.

[blue]There's no place like 127.0.0.1 ...[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top