Solved

Fix syntax error

Posted on 2007-12-04
5
925 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 119

Expert Comment

by:Rey Obrero
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 119

Accepted Solution

by:
Rey Obrero earned 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now