What does Option Explicit do? <-- That's my question! I've made so many big usefull programs with API and so much more but I never knew why I had to put Option Explicit at the top of every single one of them! Thanx in advance for your help!
Printable View
What does Option Explicit do? <-- That's my question! I've made so many big usefull programs with API and so much more but I never knew why I had to put Option Explicit at the top of every single one of them! Thanx in advance for your help!
Adding Option Explicit to your app will raise an error if you try to use a variable that hasn't been declared.
For example...
Code:Option Explicit
Private Sub Form_Load()
s = "HELLO WORLD" ' causes error to be raised because s is not declared
End Sub
Code:
Private Sub Form_Load()
s = "HELLO WORLD" ' works fine because Option Explicit is not used
End Sub
Does it declare the variable if it's not declared! FOr Ex:
Code:Option Explicit
Function OpenQ(OpL As String)
s = "hello" '<-- would that generate a variable called S
End IF
Nope that code would cause an error because you didn't declare it, if you want to use variables in that way then you need to get rid of the Option Explicit statement.
If you don't use Option Explicit, it implicitly declares variant variables for you.
I would recommend to stick to the Option Explicit statement, because it warns you for unwanted variable declarations. A typo could be difficult to find, if no error is generated, but another variable is declared instead.
If you get tired of typing the Option Explicit statement, go to Tools --> Options and check the Require Variable Declaration checkbox. All new code modules will have the Option Explicit statement added automatically.
I know theres someway you can make it so that option explicit is automatically there for very form you make, how do you do this?
Thanks
;) Look at Frans C's post directly above yours. ;)