Results 1 to 6 of 6
  1. #1
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,381

    Option Explicit by default

    Option Explicit should appear at the beginning of every code module in Access.
    When Option Explicit appears, you must explicitly declare all variables by using the Dim Statement. If you try to use an undeclared variable name, an error will occur at compile time.
    This will help you avoid spelling errors in your variable names.
    The VBA editor has an option to force Option Explicit to be declared for all modules: Tools > Options


    Click image for larger version. 

Name:	OptionExplicit.JPG 
Views:	96 
Size:	55.4 KB 
ID:	31878

    This is valid in Access 2016, not sure for previous versions.
    Last edited by davegri; 01-02-2018 at 02:48 PM.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,515
    This is a great option to use to help stop errors.

  3. #3
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,702
    This is valid in Access 2016, not sure for previous versions.
    This has been the case for as far back as I can recall. Quite certain it goes back to at least 2000 - probably 97 even.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,946
    As Micron says, this has definitely been an option since Access 2000.
    The mistake MS made was not setting this by default for all new databases.
    The reason appears to be for consistency with VBA Editor in Word/Excel etc where it isn't done by default

    The outcome for Access is lots of grief for inexperienced developers and for experienced developers taking over projects created without this option

    There is an interesting explanation here:
    https://stackoverflow.com/questions/...-to-be-defined

    The good news in that once this option is ticked, it applies for all future databases as well as for new modules in the current database.
    However it does not retrospectively add Option Explicit to existing modules - that has to be done manually

  5. #5
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,702
    I definitely don't agree with the assertion posted there that latter versions turned it on by default. Never was true IMHO and still isn't. I just had to replace my laptop and turned on the Office 365 trial, opened a new blank 2016 db, inserted a module and it isn't turned on.
    Funny, I've been doing the camel case thing for many years (mainly to avoid spaces or underscores) and thought the automatic capitalization to be a good indicator of a typo. Others didn't seem to think it was a safe bet for some reason, but I've always found it to be reliable.

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,946
    Glad you confirmed my memory was correct that Option Explicit has never been switched on by default.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Automatic Option Explicit ??
    By ksor in forum Access
    Replies: 2
    Last Post: 02-26-2017, 03:09 AM
  2. Access 2010: Option Explicit Statement
    By MarkA70 in forum Access
    Replies: 4
    Last Post: 02-15-2016, 07:43 AM
  3. Option Explicit default
    By Glenn_Suggs in forum Access
    Replies: 4
    Last Post: 02-14-2014, 12:41 PM
  4. option explicit issue acc2003 to acc2010
    By trevor40 in forum Programming
    Replies: 6
    Last Post: 02-14-2014, 12:04 PM
  5. option group default values
    By wlumpkin in forum Access
    Replies: 3
    Last Post: 02-15-2011, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums