Convert a textbox string value into date

S

s_mohtaseb

I need to convert a textbox string value in the form of 31-12-09 into a date
in the same format, can any one help please, thank you.
 
K

KARL DEWEY

I need to convert a textbox string value in the form of 31-12-09 into a
date in the same format,
Converting I understand but 'into a date in the same format' is confusing.
DateSerial("20" &
Right("31-12-09",2),Mid("31-12-09",InStr("31-12-09","-")+1,2),Left("31-12-09",2))

If you format it for display the results is a string. If you are going to
use it in calculation it does not matter what it looks like as you will not
see it inside of the math formula.
 
L

Linq Adams via AccessMonster.com

In point of fact, if you're using it with one of the Date functions, such as
DateAdd() or DateDiff(), it will do just fine leaving it as Text, assuming it
is in a recognized date format, as your

31-12-09

Alternatively, you can use the CDate() function to force the Date/Time
datatype.

Where you're more likely to have problems, though, is from using the British
date format, especially if you do anything in queries. Allen Browne has a
short tutorial on the subject of International Dates here:

http://www.allenbrowne.com/ser-36.html
 
S

s_mohtaseb via AccessMonster.com

I tried the expression but still I am having a problem. It is best describe
the situation I am having. I have a form based on a transaction table with a
transaction date field, TransDate. On the form I would like to have an
unbound textbox, TextDate through which I can change the default date for the
TransDate field every time I open the form to add new records using the
following line of code for the TextDate update event:
TransDate.DefaultValue = DateSerial("20" & Right(TextDate, 2), Mid(TextDate,
InStr(TextDate, "-") + 1, 2), Left(TextDate, 2))
now using the above expression I am getting 09-01-1900 in response to the 31-
12-09 that I enter into the TextDate.
any help would be appreciated.
 
D

Douglas J. Steele

Hmm. I would have expected a different value...

The DefaultValue property is text. I'm wondering whether Access is seeing
the value you're trying to assign as a number and doing arithmetic on it
before it sets the property.

See whether this works any better:

TransDate.DefaultValue = Chr(34) & DateSerial("20" & Right(TextDate, 2),
Mid(TextDate,
InStr(TextDate, "-") + 1, 2), Left(TextDate, 2)) & Chr(34)

However, you might be able to get away with:

TransDate.DefaultValue = Chr(34) & TextDate & Chr(34)

or

TransDate.DefaultValue = Chr(34) & CDate(TextDate) & Chr(34)
 
S

s_mohtaseb via AccessMonster.com

Thank you so much for your help, the first expression did work great. I am
much obliged.
Hmm. I would have expected a different value...

The DefaultValue property is text. I'm wondering whether Access is seeing
the value you're trying to assign as a number and doing arithmetic on it
before it sets the property.

See whether this works any better:

TransDate.DefaultValue = Chr(34) & DateSerial("20" & Right(TextDate, 2),
Mid(TextDate,
InStr(TextDate, "-") + 1, 2), Left(TextDate, 2)) & Chr(34)

However, you might be able to get away with:

TransDate.DefaultValue = Chr(34) & TextDate & Chr(34)

or

TransDate.DefaultValue = Chr(34) & CDate(TextDate) & Chr(34)
I tried the expression but still I am having a problem. It is best
describe
[quoted text clipped - 29 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top