Solved

Fix syntax error

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Excel Pivot Table and MS Access Pivot Table 4 84
CISCO WIFI 6 73
SUBFORM on ACCESS 2013 8 36
Access 2003 query, remove dupes 17 26
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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 …

752 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