Link to home
Start Free TrialLog in
Avatar of Bob Stone
Bob StoneFlag for United States of America

asked on

Fix syntax error

I am trying to run a sub to check the date to make a mdb/mde time limited. I can't quite get the sytax right.

This is what I got so far. I get syntax error on first line.  "Compile Error, expected end sub"

How do I fix it?
Private Sub Form_Load()
 
Function CheckDate()
    Dim DataMax As Date
    DataMax = "12/31/2007"
    If Now() > DataMax Then
        MsgBox "Your time has expired! Please Register"
        DoCmd.Quit
    End If
End Function
 
End Sub

Open in new window

Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

DataMax = #12/31/2007#

Private Sub Form_Load()
 
Function CheckDate()        'get this out of the sub procedure
    Dim DataMax As Date
    DataMax = #12/31/2007#
    If Now() > DataMax Then
        MsgBox "Your time has expired! Please Register"
        DoCmd.Quit
    End If
End Function
 
End Sub

like this

Private Sub Form_Load()
 
CheckDate
 
End Sub

Function CheckDate()        'get this out of the sub procedure
    Dim DataMax As Date
    DataMax = #12/31/2007#
    If Now() > DataMax Then
        MsgBox "Your time has expired! Please Register"
        DoCmd.Quit
    End If
End Function
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to clarify what the error was telling you (as resolved by capricorn1's most recent post).  Your original code was attempting to define the Function CheckDate within the code of the Form_Load Sub definiton:

Private Sub Form_Load()<<<<<<<<<<<<<<<   this goes with
 
Function CheckDate()
    Dim DataMax As Date
    DataMax = "12/31/2007"
    If Now() > DataMax Then
        MsgBox "Your time has expired! Please Register"
        DoCmd.Quit
    End If
End Function
 
End Sub<<<<<<<<<<<<<<<<<<<<<<<<<  This

you cannot have another Code Block to define a new function, entirely within the defintion of the Sub.

As Capricorn1 shows you, move the Function defintion outside to the Sub definition, and call the funxstion from the Sub:

Private Sub Form_Load()
 CheckDate
End Sub

Function CheckDate()
    Dim DataMax As Date
    DataMax = "12/31/2007"
    If Now() > DataMax Then
        MsgBox "Your time has expired! Please Register"
        DoCmd.Quit
    End If
End Function


In addition, a Function should Return a Value, which your function does not do, so you really should make it a Sub procedure, like this:


Private Sub Form_Load()
 CheckDate
End Sub
 
Private Sub CheckDate()
    Dim DataMax As Date
    DataMax = "12/31/2007"
    If Now() > DataMax Then
        MsgBox "Your time has expired! Please Register"
        DoCmd.Quit
    End If
End Sub

Open in new window

Avatar of Bob Stone

ASKER

That worked perfectly.

Thanks =o)