Bob Stone
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?
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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()<<<<<<<<<<<<<<<
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<<<<<<<<<<<<<<<<<<<<<<<
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
ASKER
That worked perfectly.
Thanks =o)
Thanks =o)