Solved

convert string to date in vb6

Posted on 2004-10-05
3
1,039 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

690 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