Solved

convert string to date in vb6

Posted on 2004-10-05
3
854 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
Comment Utility
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
Comment Utility
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 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now