Results 1 to 7 of 7

Thread: [RESOLVED] Compile Error when converting to date

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    3

    Resolved [RESOLVED] Compile Error when converting to date

    Hi, to preface my question, I'm not very experienced in writing code so I wont understand complex answers.

    The problem I am having is I am trying to convert a string to a date.

    I am using the following code

    Dim Current_Date As String
    Dim D As Integer
    Dim M As Integer
    Dim Y As Integer


    D = Current_Date.Substring(0, 2)
    M = Current_Date.Substring(2, 2)
    Y = Current_Date.Substring(4, 4)
    MsgBox (CDate(D & M & Y))

    End Sub



    The problem is occuring when it gets to the D variable, the error says Compile error; Invalid qualifier.

    Can anyone help?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Compile Error when converting to date

    Welcome to the forums.

    This is the VB6 & earlier forum. It appears you are using .Net? If so, a moderator will move this for you to the correct forum. In the mean time, you may want to search the .Net forum for a quick solution.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Compile Error when converting to date

    what is it you are trying to do? Umm.... are you using VB6 or VB.NET? there's a big difference. Specifically, the code you posted is .NET code, not VB6.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    3

    Re: Compile Error when converting to date

    Hi,

    I am using VB6.5, I didn't realise the code is not interchangable. I have changed the code into what I think is VB6 code but its still not working, the new code is

    Sub test3()
    '
    ' test3 Macro


    Dim Current_Date As String

    Dim MyValue As Date



    Current_Date = Application.InputBox("Please enter end date of period (DDMMYYYY)", Type:=2)


    File_path = "K:\Investment\Appraisals\Rebalance\"
    File_name = Current_Date & ".xls"
    Application.Workbooks.Open Filename:=File_path & "Cashflows " & File_name
    MyValue = CDate(Current_Date)
    Workbooks("Asset Allocations FY2010 (27 November_ Current)1.xls").Activate
    Sheets("Cap Stable").Activate
    Rows("1:1").Select
    Selection.Find(What:=MyValue, After:=ActiveCell, LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False).Activate


    The code stops at the CDate function with the error message saying 'Run time error 13 Type mismatch'

    Any Thoughts??

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Compile Error when converting to date

    This can be handled with a one-liner:
    vb Code:
    1. MyValue = DateSerial(Right(Current_Date,4),Mid(Current_Date,3,2),Left(Current_Date,2))
    For clarity you can declare three additional variables if you like:
    vb Code:
    1. Dim lngYear As Long
    2. Dim lngMonth As Long
    3. Dim lngDay As Long
    4.  
    5. lngYear = Right(Current_Date, 4)
    6. lngMonth = Mid(Current_Date, 3, 2)
    7. lngDay = Left(Current_Date, 2)
    8.  
    9. MyValue = DateSerial(lngYear, lngMonth, lngDay)

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    3

    Re: Compile Error when converting to date

    Thanks all, problem solved and I even learnt a few things.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Compile Error when converting to date

    Just to be even more awkward, VB 6.5 is not the same as VB 6 either, even tho they can be very similar (such as the kind of thing Ellis Dee showed).

    VB 6.x is actually VBA rather than VB (I'm not sure why MS has never made that clear in Help/About etc!), so there are significant differences depending on which host program you are using (typically one of the programs in MS Office).


    I don't know which host program you are using, but I suspect that you should post future questions in our 'Office Development' forum rather than the 'VB6' forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width