Solved

convert string to date in vb6

Posted on 2004-10-05
3
871 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 69

Accepted Solution

by:
Éric Moreau earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

910 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

23 Experts available now in Live!

Get 1:1 Help Now