Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Fix syntax error

Posted on 2007-12-04
5
Medium Priority
?
950 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:Bob Stone
5 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 20402978
DataMax = #12/31/2007#
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20403120

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
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 20403137
correction, use Date instead on Now to get a good comparison
* now() includes time

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 Date() > DataMax Then
        MsgBox "Your time has expired! Please Register"
        DoCmd.Quit
    End If
End Function
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20403648
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

0
 
LVL 9

Author Closing Comment

by:Bob Stone
ID: 31412585
That worked perfectly.

Thanks =o)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question