Option Explicit statement confusion

wtrimble

Registered User.
Local time
Today, 10:44
Joined
Nov 13, 2009
Messages
177
I'm new to VB and believe I have been writing code badly. But I am right now doing a Performance Analyzer on my database, because it is very slow and a couple forms have had error messages: "Not enough memory to open... close all programs" and not able to open. But after I did the Performance Analyzer it said to use an "Option Explicit statement" for several of my codes. Due to my no formal education in really any type of programming I often leave out the "Dim variable as String" which I believe is what the "Option Explicit statement" is referring to. However for some of the forms it recommends this for, the only code I have is:
Code:
Private Sub Image0_DblClick(Cancel As Integer)
CandO Me.Name, "xxfrmaddorviewinformation"
End Sub

Referenced from a module:
Code:
Public Function CandO(strClose As String, strFormName As String)
strFormName = xxfrmaddorviewinformationxx
    DoCmd.Close acForm, strClose, acSavePrompt
 
    DoCmd.OpenForm "xxfrmaddorviewinformationxx"
End Function

My question is, do I stil need to declare some variable for my intial code? even though it's one line calling a function. And if so, what should it be??

Thanks for any help or tips to my problem
 
The variables are already declared in the function header:
Public Function CandO(strClose As String, strFormName As String)

So no further declaration is necessary.
 
I guess a follow question also would be for the following code:

Code:
Private Sub Command10_Click()
Combo0 = Null
Combo2 = Null
Combo4 = Null
Child16.Requery
End Sub

do I need a "dim" statement here?
 
But what it means is you should have, in the General Declarations Section of ALL modules (form, report, standard, and class) -
Code:
Option Compare Database
Option Explicit  <-----Put this in there
 
Thanks boblarson, so is there any changes I can make to it to make an Option Explicit statement?
 
I guess a follow question also would be for the following code:

Code:
Private Sub Command10_Click()
Combo0 = Null
Combo2 = Null
Combo4 = Null
Child16.Requery
End Sub

do I need a "dim" statement here?

No, because you aren't using variables. HOWEVER, you SHOULD probably do this(as well as rename your controls to some better and more logical name as to what they are instead of using the default names of Combo0, Combo2):

Code:
Private Sub Command10_Click()
[COLOR="Red"]Me.[/COLOR]Combo0 = Null
[COLOR="red"]Me.[/COLOR]Combo2 = Null
[COLOR="red"]Me.[/COLOR]Combo4 = Null
[COLOR="red"]Me.[/COLOR]Child16[COLOR="red"].Form[/COLOR].Requery
End Sub
 
ah, gotcha. Does "me." just mean refer to the Current form, control....?
 
Thanks boblarson, so is there any changes I can make to it to make an Option Explicit statement?

All you need to do is to make sure that the words

OPTION EXPLICIT


are in the General Declarations section (just like I said). When you do Debug > COMPILE from the VBA window, if you are missing any declarations, Access will tell you; but ONLY if you have that OPTION EXPLICIT set.

You can go in to the VBA Window and then to TOOLS > OPTIONS > EDITOR and then select the check box that says "REQUIRE VARIABLE DECLARATION" and from that point out when you add a form, report, or module that has code, it will put that OPTION EXPLICIT at the top of your module automatically. It won't do it on ones that currently exist, so you have to do those manually, but it will do it for newly created ones.
 
ah, gotcha. Does "me." just mean refer to the Current form, control....?

Me. refers to the current CLASS OBJECT, which in this case is the form. You can also use the ME keyword when using code on a report (to refer to that report) or in a class module. You can't use it in a Standard Module.
 

Users who are viewing this thread

Back
Top Bottom