Search code examples
functionwindows-7vb6dateadd

Error calling dateadd function


Please help me on this. I run the same code on other computer and they return no error. But on my pc this still pops up.

EDIT:

Code:

Private Sub Form_Load()
    Msgbox (DateAdd("d",-1,Date))
End Sub

Output:

Run-time error '5';
Invalid procedure call or argument

BTW. The computer I used is a windows 7 VM.

EDIT 2:

I manage to run the code by editing a parameter but with a wrong output.

Code:

Private Sub Form_Load()
    Msgbox (DateAdd("dddd",-1,Date))
End Sub

Output:

5/5/2014

Solution

  • The following code works without any problems with VB6 on my Windows7 64bit machine:

    Option Explicit
    
    Private Sub Command1_Click()
      Dim datNow As Date
      Dim datYesterday As Date
      datNow = Now
      datYesterday = DateAdd("d", -1, datNow)
      Print "Yesterday = " & CStr(datYesterday)
    End Sub
    
    Private Sub Form_Load()
      MsgBox (DateAdd("d", -1, Now))
    End Sub
    

    Btw: you can remove the () in the MsgBox call:

      MsgBox DateAdd("d", -1, Now)
    

    I let those stay in my test to see if that might have caused the problem, but it works with them as well

    Are you sure you are using VB6?

    You could also try to create an install package on the machine where it works, and then install it on the other machine. If it then works, then there are probably some libs missing