Full Version: Using Option Explicit- Having Trouble Declaring A Variable
UtterAccess Forums > Microsoft® Access > Access Forms
kevinlaw
Hello UA,
Oran the analyzer on my db and it came up for many forms that if I used Option Explicit the code would run faster (a few of my forms are 'code heavy' and this would be a good thing). Problem is on one of my command buttons the variable is not defined properly and I'm not sure how to do this. I've pasted the line below- does anyone know how to define this, so it works with Option Explicit?
StrPathName = CurrentProject.Path & "\Exports\ProjectData" & Format(Date, "YYYYMMDD") & ".xls"
Note: I don't have any code above this.
Also, is it correct to think that using Option Explicit is beneficial. Most of the headers of my form's module code are Option Compare Database (if it's obvious I'm not completely up on this, I'm self taught and seemed to have missed these most likely important issues...)
Many thanks for the help!
Jeff B.
Check Access HELP for more details on Option Explicit.
on't know if it runs faster, but having it declared saves you from some errors.
If you "have nothing above" the line of code with "StrPathName", then it isn't declared. Try something like:
Dim StrPathName As String
(and make sure to put the Option Explicit at the top of the module)
argeedblu
In the procedurewhere you use it you can declare
im StrPathName as String
You should always require variable declaration by having Option Explicit at the head of every module. In fact, the VBE options have a checkbox for "Require Variable Declaration" so that the directive is automatically inserted at the beginning of every new module.
Although the documentation you read suggests a performance reason for requiring variable declaration, it is even more important to prevent the accidental declaration of new variables simply because of a spelling error.
Glenn
CyberCow
Like this . . . .
Option Compare Database
Option Explicit
________________________________
Private Sub SomeSubName_On_Click()
Dim StrPathName As String
StrPathName = CurrentProject.Path & "\Exports\ProjectData" & Format(Date, "YYYYMMDD") & ".xls"

hope this helps
kevinlaw
That works great- Thanks CC. One question- is it a good idea to go back through all my forms and enter Option Explicit? It's a lot of work to make sure it all runs right after doing that, but if it speeds up the db it would be worth it. If it won't make much difference I'll pass on it. Just need a little expert advice- thanks!
kevinlaw
Ah, I see- thanks for the clarification. I didn't see your post when I'd responded to CC- they must have come in at the same time. I can definitely see the point of using Option Explicit now and will go through everything. Much appreciated.
argeedblu
It is definitely worthwhile to go back and add Option Explicit at the top of every module. It may take time to eliminate the errors that not having it may have caused but each of those errors is a disaster just waiting to happen.
Glenn
BananaRepublic
As Jeff B. & agreedblu already points out and I suspect CyberCow to agree also as well myself -
ven if there were no performance gains to be had, I'd still go back and put in Option Explicit. This will guarantee that your code will contain less bugs because without it, you can get unexpected results simply due to a typo or something equally insidious.
kevinlaw
Point well taken. It looks like it's worth the trouble so I'll take care of it. Thanks for the explanation.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.