Results 1 to 6 of 6

Option Explicit by default

  1. #1
    davegri is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    1,080

    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:	43 
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 01:48 PM.

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

  3. #3
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    3,931
    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.
    - "doesn't work" is no help. What's happening? Error messages? Where??
    - Use code tags for code/sql. Implement changes in copies of your database.
    Irregardless, ain't no such word as "reoccur".

  4. #4
    ridders52's Avatar
    ridders52 is offline Long time standing
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    1,663
    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 offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    3,931
    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
    ridders52's Avatar
    ridders52 is offline Long time standing
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    1,663
    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, 02:09 AM
  2. Access 2010: Option Explicit Statement
    By MarkA70 in forum Access
    Replies: 4
    Last Post: 02-15-2016, 06:43 AM
  3. Option Explicit default
    By Glenn_Suggs in forum Access
    Replies: 4
    Last Post: 02-14-2014, 11:41 AM
  4. option explicit issue acc2003 to acc2010
    By trevor40 in forum Programming
    Replies: 6
    Last Post: 02-14-2014, 11:04 AM
  5. option group default values
    By wlumpkin in forum Access
    Replies: 3
    Last Post: 02-15-2011, 02: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
  •  
Tech Forums: Microsoft Office Forums