[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert string to date in vb6

Posted on 2004-10-05
3
Medium Priority
?
1,774 Views
Last Modified: 2008-02-01
in vb6 I have a string 11012004 that I want to convert to be read as a date. I tried to use the cdate but got type mismatch errors. Anyone know how this could be done?
Thanks
0
Comment
Question by:cdb424ttm
3 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 12225944
Private Sub Command1_Click()
    Dim sDate As String
    Dim dDate As Date
    sDate = "11012004"
    dDate = CDate(Right$(sDate, 4) & "-" & Mid$(sDate, 3, 2) & "-" & Left$(sDate, 2))
    MsgBox dDate
End Sub
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12225965
as long as the string is of the same length:

Dim sDate As String
Dim TheDate As Date

sDate = "11012004"
sDate = Format(sDate, "@@/@@/@@@@")

If IsDate(sDate) Then
    TheDate = CDate(sDate)
End If

Debug.Print Format(TheDate, "mmm dd, yyyy")
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 12227493
You can also use DateSerial:

Private Sub Command1_Click()
Dim dtmDate As Date
Dim strDate As String

    strDate = "11012004"
   
    dtmDate = DateSerial(Mid$(strDate, 5, 4), Mid$(strDate, 1, 2), Mid$(strDate, 3, 2))
    MsgBox dtmDate
End Sub
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

873 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