VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trouble with simple function

1 REPLY 1
Reply
Message 1 of 2
zale-86
247 Views, 1 Reply

Trouble with simple function

I have a function to reformat dates.

 

Public Function FixDate(tRevisionDate)

Dim Y
Dim M
Dim D
Dim arrRevisionDate
Debug.Print "RevisionDate In Is: " & tRevisionDate
If Len(tRevisionDate) < 10 Then
tRevisionDate = Replace(tRevisionDate, "/", "-")
tRevisionDate = Replace(tRevisionDate, ".", "-")
arrRevisionDate = Split(tRevisionDate, "-")
M = arrRevisionDate(0)
D = arrRevisionDate(1)
Y = arrRevisionDate(2)
    If Len(M) < 2 Then
    M = "0" & M
    End If
    If Len(D) < 2 Then
    D = "0" & D
    End If
    If Len(Y) < 4 Then
    Y = "20" & Y
    End If
tRevisionDate = Y & "-" & M & "-" & D
End If

Debug.Print "RevisionDate Out Is: " & tRevisionDate

End Function

 

I'm calling it like this:

RevisionDate = FixDate(tRevisionDate)

If I do a debug.print

Debug.Print "RevisionDate=" & RevisionDate

I get NOTHING. The debug.print commands from within the funtion return the correct values.

What AM I Doing WRONG?


Thanks In Advance!

 

1 REPLY 1
Message 2 of 2
norman.yuan
in reply to: zale-86

Since the FixDate() is declared as Function, you need return some value in the last line of executing code:

 

Public Fucntion FixDate(....)

    ...

 

    Debug.Print "RevisionDate Out Is: " & tRevisionDate

 

    ''Add this line of code

    FixDate=tRevisionDate

End Function

 

But your code seems overly complicated. If the input (tRevisionDate) is a value Date/Time string, you can

 

Public Function FixDate(tRevisionDate As String)

 

    ''Convert the string into a Date type

    ''You may want to trap possible error (On Error ,,,) here

    ''in case the input is invalid. Tha is

    ''if the input cannot be converted into a Date type,

    ''you may want to return Nothing or blank string

    Dim d As Date

    d=CDate(tRevisionDate

 

    ''reformat it in a desired date string

    FixDate=Format(d,"yyyy-MM-dd")

 

End Function

 

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost