Link to home
Start Free TrialLog in
Avatar of heng03
heng03

asked on

CDate conversion error

Hi

I have a textbox ticketDate.text which have value "21-10-2003".

I used the CDate function to conversion it :-  CDate(ticketDate.Text) but have
run time error.
   Cast from string "21-10-2003" to type "Date" is not valid.

Pls help

Thanks & Rgds
Heng03
Avatar of mmarinov
mmarinov

the error rise because the format is 2003-10-21
CDate uses standarts for datatimeformats
if you want to use your custom format you can use ticketDate.Text.ToString("dd-mm-yyyy");

B..G
Avatar of heng03

ASKER

Hi mmarinov

Your suggestion have syntax error.


Thanks & Rgds
Heng03
Avatar of heng03

ASKER

Hi mmarinov

Your suggestion have syntax error & compilation error.
It do not work.


Thanks & Rgds
Heng03
SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use a datetimepicker control, it's easier and user friendly.
Avatar of heng03

ASKER

Hi
Sweet! Thanks!

   - Marc
heng03:
Thanks for the points!
Just out of curiosity, how does your corrected code look like?

Dabas
Avatar of heng03

ASKER

Hi everyone

I used the sample code from MarcGraff, it is the best for my situation.

My code is below.

dim TmpStr() As String
            TmpStr = Split(txtTicketDate.Text, "-")
            Try
                      TicketDate = CDate(TmpStr(1) & "-" & TmpStr(0) & "-" & TmpStr(2))
            Catch
                      validYes = False
                                errMsg &= "Error Ticket Date : Shall be dd-mm-yyyy. Please  try
                                                 again." + vbNewLine
            End Try

The reason is that, it re-substring my date from dd/mm/yyyy to mm/dd/yyyy which is
suitable for the us-EN culture date-time format ("d").
As such, the CDate(expression) will work fine. else error if format is dd/mm/yyyy.

Thanks & Rgds
Heng03

Avatar of heng03

ASKER

Hi everyone,

I increased the points from 50 to 75.
And split among 3 of you. Each 25 points.

Sorry for that.

Thanks & Rgds
Heng03