Solved

Fix syntax error

Posted on 2007-12-04
5
941 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

623 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