Solved convert a date (1 Viewer)

Tom d

Member
Local time
Today, 02:00
Joined
Jul 12, 2022
Messages
45
I have a text field in the format mmddyyyy and I want to covert it to a serial number.
 

plog

Banishment Pending
Local time
Today, 01:00
Joined
May 11, 2011
Messages
11,020
You need Cdate and Cdbl to do it:

SerialNumber: CDbl(CDate([TextField]))

Make it a date first, then convert it to a number.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:00
Joined
Feb 28, 2001
Messages
23,210
Give us your idea about what this serial number should resemble - including whether your serial number can allow duplicates. And by the way, CDbl(CDate([TextField])) IS a valid code snippet if you put the correct value in place of [TextField]. If you have a control then put the name of the control there. If you have a field in a record, that is what goes in place of the [TextField] argument. This is what the VBA code might look like.

a = "#01/23/2022#"
b = CLng(Cdate(a))
c = CDbl(Cdate(a))

In these examples if I allow A, B, and C to be VARIANT data types, A will become a string. B will become a long integer (32-bit). C will become a double precision floating number. If your input string is not formatted as a true date (which mmddyyyy isn't), then Access CDate function will still try to convert it, but if you don't provide proper punctuation and if both mm and dd are less than 12, you can have a problem depending on how you set things up. See, for example,


Your question, because it leaves some parts ambiguous, is actually trickier than you might have thought.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:00
Joined
May 7, 2009
Messages
16,875
SerialNumber: CDbl(DateSerial(Right([TextField], 4), Mid([TextField], 3, 2), Left([TextField], 2)))
 

Users who are viewing this thread

Top Bottom