Option Explicit ? (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 12:03
Joined
Sep 21, 2011
Messages
14,288
Hi all,
We see time and time again, new members not using Option Explicit and using variables which do not exist.
I was trying to help someone on another forum, who had done the same.

This led me to wonder......Is there any reason NOT to use Option Explicit ?
I would have thought it would be made the default option, not the opposite?
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:03
Joined
Jan 23, 2006
Messages
15,379
I don't know the history of Option Explicit, but have also wondered why it is not set as Default. Seems more issues/frustration would be prevented if it was included by default.
 

isladogs

MVP / VIP
Local time
Today, 12:03
Joined
Jan 14, 2017
Messages
18,219
I'm not aware of any reason for omitting it
It has long been a request that MS make this the default in all new databases.
In fact it seems logical that it should be the default option in all VBA enabled applications such as PowerPoint / Excel / Word.

Perhaps the reason for not doing so dates back to when other companies could purchase VBA for use in their own apps e.g. AutoCAD?
 

KitaYama

Well-known member
Local time
Today, 20:03
Joined
Jan 6, 2022
Messages
1,541
dates back to when other companies could purchase VBA for use in their own apps e.g. AutoCAD
I work with 3 CAD software and 2 of them use VBA for automating tasks, both of them has Option Explicit as the default. When I add a new macro, option explicit is sitting there....
 

sonic8

AWF VIP
Local time
Today, 13:03
Joined
Oct 27, 2015
Messages
998
Is there any reason NOT to use Option Explicit ?
You do not need to bother declaring variables.
Envision a newbie casual VBA user who you want to enable to do simple stuff in VBA with the minimal knowledge and effort possible. - Then this might have looked like a sensible idea from Microsoft's perspective when designing the predecessors to the VBA language.

Of course, I disagree with this line of thinking. Nonetheless, I acknowledge the rationale behind it.

Edit/PS: As to why it was never changed: If your software is installed on millions of computers you need a very strong case to change default values which might annoy a significant part of your users.
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:03
Joined
Sep 21, 2011
Messages
14,288
Edit/PS: As to why it was never changed: If your software is installed on millions of computers you need a very strong case to change default values which might annoy a significant part of your users.
Tell me about it. MS have just put the formula bar in Excel for Android at the bottom of the screen.
Having been used to it being at the top for many years (and still at the top in Windows) it catches me out every time. :mad:
 

isladogs

MVP / VIP
Local time
Today, 12:03
Joined
Jan 14, 2017
Messages
18,219
One obvious solution would be for this to be set via a checkbox in the General section of Access Options so it then applies to all databases.
Similarly, I wish I could set Overlapping Windows as the default option so it always applies rather than set it each time
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:03
Joined
Feb 28, 2001
Messages
27,179
As to WHY to not use Option Explicit, the first versions of BASIC (Beginner's All-purpose Symbolic Instruction Code) were for ... wait for it... beginner programmers - who didn't understand issues with declarations. And no, I'm not kidding about the expansion of the acronym.

Those early versions were, in many cases, real-time interpretive as opposed to PCode or machine code compilers. In fact, in the 1960s, the old Digital Equipment Corp. (DEC) BASIC went both ways. DEC VAX BASIC was still that way in the 2010 time frame. You could give it a text file containing your code and have it compiled to machine code as a MAIN module OR a module of subs, OR you could just start typing - with the understanding that loops and IF statements were illegal in that "just start typing" mode, but you could do an otherwise complex expression in multiple statements.

Therefore, one answer to "why not use Option Explicit" is "tradition." Plus the obvious fact that as long as you were reasonably careful about uninitialized variables, you could write a valid program VERY quickly. AND they were very big on variable type hints, such as $ for strings, % for integers, # for floating... so data typing existed even without using Option Explicit.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:03
Joined
Feb 19, 2002
Messages
43,266
MS doesn't change certain things because they could break existing apps. In this case, I don't believe it would so there isn't any reason not to.

I think there are lots of Options that I would like to be able to set universally. Like the option to ALWAYS default AllowZeroLengthString to NO! and to always set the default for integers to null.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 12:03
Joined
Jul 9, 2003
Messages
16,282
I wish I could set Overlapping Windows as the default option so it always applies rather than set it each time
I have a selection of boilerplate databases which I base my projects on. They are set up just how I like. When I want to start a new project I make copy and use that. All the settings are there, ready and waiting.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:03
Joined
Feb 19, 2002
Messages
43,266
I do the same things but the defaults for tables are hardwired and you can't fix them with the template database method.

My other request regarding defaults is that MS separate the "all databases" from "this database" to make it clearer how the options work. PLUS, the "all databases" options should only apply to new databases or new objects in existing databases.
 

Isaac

Lifelong Learner
Local time
Today, 04:03
Joined
Mar 14, 2017
Messages
8,777
Let's face it, it wouldn't be the only thing in a long list of things regarding Access where the "worst worse practice" is the default and the best practice must be achieved through affirmative actions of some kind.

But that's been MS approach the whole time - make it as easy to use as possible (rather than as likely as possible to be used well) ... big difference.

You constantly see advice having to be given to newcomers: "As soon as you start your database, change these 20__ things before doing anything else".
 

isladogs

MVP / VIP
Local time
Today, 12:03
Joined
Jan 14, 2017
Messages
18,219
I have a selection of boilerplate databases which I base my projects on. They are set up just how I like. When I want to start a new project I make copy and use that. All the settings are there, ready and waiting.
Yes I usually do that as well, but my point was that it shouldn't be necessary to use that approach.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:03
Joined
Feb 19, 2002
Messages
43,266
It not like MS actually listens to the Access MVPs. Instead they create abominations like multi-value fields and random things like how scrolling works or the default for ZLS.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:03
Joined
Jan 20, 2009
Messages
12,852
One obvious solution would be for this to be set via a checkbox in the General section of Access Options so it then applies to all databases.
Require Variable Declaration setting in the VBA editor will put Option Explicit on every new module. Am I missing something in what you are saying?
 

Isaac

Lifelong Learner
Local time
Today, 04:03
Joined
Mar 14, 2017
Messages
8,777
Require Variable Declaration setting in the VBA editor will put Option Explicit on every new module. Am I missing something in what you are saying?
i think he meant at the client install level, across multiple databases. (?)
 

isladogs

MVP / VIP
Local time
Today, 12:03
Joined
Jan 14, 2017
Messages
18,219
Require Variable Declaration setting in the VBA editor will put Option Explicit on every new module. Am I missing something in what you are saying?
Yes. It can be done by setting that option once and once only, so it then applies to every new database but for new users its obscure
Similarly there should be a user option to e.g. set display to overlapping windows ALWAYS, also to disable subdatasheets etc
 
Last edited:

Isaac

Lifelong Learner
Local time
Today, 04:03
Joined
Mar 14, 2017
Messages
8,777
Yes. It could/should be done by setting that option once and once only, so it then applies to every new database
Similarly user option to set display to overlapping windows ALWAYS, also to disable subdatasheets etc
Like it does in excel!
 

isladogs

MVP / VIP
Local time
Today, 12:03
Joined
Jan 14, 2017
Messages
18,219
I've modified my previous post to clarify what I meant
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:03
Joined
Sep 12, 2006
Messages
15,653
One obvious solution would be for this to be set via a checkbox in the General section of Access Options so it then applies to all databases.
Similarly, I wish I could set Overlapping Windows as the default option so it always applies rather than set it each time

Hi Colin

You can just set overlapping windows in a startup code
try this.

use value 0 for tabs, and 1 for overlapping.
there's a fair bit of test stuff in here you can remove.

I store the user's required setting in some sort of startup config file (ini file, as there's nice support for ini files avaiable)

Code:
Function ExistsDBProperty(strPropName As String) As Boolean
On Error Resume Next

Dim db As dao.Database
Dim prp As dao.Property

'  Set db = DBEngine(0)(0)
  Set db = CurrentDb
  
    Set prp = db.Properties(strPropName)

    If Not prp Is Nothing Then
        ExistsDBProperty = True
    Else
        ExistsDBProperty = False
    End If

    Set prp = Nothing
    Set db = Nothing

End Function


Sub setMdiMode(setmode As Long)
Dim db As Database
Dim prp As Property
Dim version As String
Dim legend As String

'  Set db = DBEngine(0)(0)

    On Error GoTo fail
    Set db = CurrentDb

    If ExistsDBProperty("UseMDIMode") = False Then
        Set prp = db.CreateProperty("UseMDIMode", dbByte, 0)
        db.Properties.Append prp
        'MsgBox "Property Created"
    Else
        'MsgBox "Property Already Exists"
    End If


    version = Application.version

    Set prp = db.Properties("UseMDIMode")
    If prp.Value <> setmode Then
        prp.Value = setmode
    
        Select Case setmode
        Case 0: legend = "Tabbed Windows"
        Case 1: legend = "Overlapping Windows"
        End Select

'check the access version .          
        'If version >= "12" Then
            MsgBox "You have selected the option for " & legend & ". This setting will take effect the " & _
                "next time you use this database. ", vbInformation, "Setting Changed"
        'End If
    Else
        'MsgBk ox "Property Value already set to: " & prp.Value
    End If
    Exit Sub




fail:
    MsgBox "Error setting the database property for Overlapping Windows. " & vbCrLf & vbCrLf & _
        "Error: " & err & "   Desc: " & err.Description

End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom