Clicky

Hi
trying to compile an application,
got an error

"Run Time error"
Variable Not Defined.

Highlighted on this word "DownLoadFolder"

But when i put comment on following line then it is working fine.
"OPTION EXPLICIT"

so what is happaning..
will it effect to other function if i put comment on OPTION EXPLICIT

in the application there is couples of function.

asked 12/08/2011 03:04

crystal_Tech's gravatar image

crystal_Tech ♦♦


5 Answers:

option explict have many declaration
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Option Explicit

Private Declare Sub Sleep Lib "kernel32.dll" ( _
    ByVal ms As Long)

Dim con As New ADODB.Connection      ''''local connection
Dim rs As New ADODB.Recordset        ''''Local Recordset
Dim RsLS As New ADODB.Recordset  'Local Recordset FOR LOADING SHEET
Dim RsUpdate As New ADODB.Recordset
Dim RsGDMUpdate As New ADODB.Recordset
Dim RetVal
Dim RmtDrv
Dim BNo, LNo

Const WM_CLOSE = &H10
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

''''''''''''''''''this all for client side application
Dim GetAName, GetAName1, LocalFile, RemoteFile As String
Dim BName, BName1, BName2, BName3
        
'''''FOR CHECKING FILE SIZE
    Dim strMyDir As String
    Dim strOldSize As String
link

answered

crystal_Tech's gravatar image

crystal_Tech

By using Option Explicit, you are forced to declare a variable before you can use it.

For example:

    Dim someVar As Integer ' <-- declare it
    someVar = 411 ' <-- use it

Without Option Explicit, you can use a variable without declaring it:

    someValue = someVar * 3 ' <-- "someValue" wasn't declared so it gets created

This may seem like a convenient mechanism, but it can lead to subtle bugs that are hard to find.

The main cause of this is TYPOS.  If you mistype your variable name, it won't cause an error since a new variable with that name will be created:

    anotherValue = somValue * 4 ' <-- I wanted "someValue" but typed "somValue"

In the above line, "somValue" would have an initial value of zero (since it was just created) instead of the value you were expecting to find in "someValue".

Turning on Option Explicity catches this kind of error and can save a lot of heartache.
   
link

answered 2011-12-08 at 23:10:13

Idle_Mind's gravatar image

Idle_Mind

Try to avoid declaring a variable without assigning it a data type, as you have in:

Dim BName, BName1, BName2, BName3

It will default to the "Variant" data type which takes up more memory than you may need. If these were strings, for example, use this instead:

Dim BName As String, BName1 As String, BName2 As String, BName3 As String

For integers:

Dim i
Dim i As Integer

makes a big difference. the first declare will take up 16 bytes (a Variant), while the second declare will take up 2 bytes (an Integer). Multiply this by the many, many variables your program uses, and you can see where you program's memory footprint can get way bigger than it needs to be.
link

answered 2011-12-08 at 23:11:10

VBClassicGuy's gravatar image

VBClassicGuy

try it with:
'------
Option Explicit

Private Declare Sub Sleep Lib "kernel32.dll" ( _
    ByVal ms As Long)

Dim con As New ADODB.Connection      ''''local connection
Dim rs As New ADODB.Recordset        ''''Local Recordset
Dim RsLS As New ADODB.Recordset  'Local Recordset FOR LOADING SHEET
Dim RsUpdate As New ADODB.Recordset
Dim RsGDMUpdate As New ADODB.Recordset
Dim RetVal
Dim RmtDrv
Dim BNo, LNo
Dim DownLoadFolder

Const WM_CLOSE = &H10
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

''''''''''''''''''this all for client side application
Dim GetAName, GetAName1, LocalFile, RemoteFile As String
Dim BName, BName1, BName2, BName3
       
'''''FOR CHECKING FILE SIZE
    Dim strMyDir As String
    Dim strOldSize As String

'---------



if that does not work then we need to know the contents of the highlighted line.
link

answered 2011-12-09 at 06:16:09

Surone1's gravatar image

Surone1

Another advantage of using Option Explicit is that if you name your variables with a mix of upper and lower case letters (as you and most people do) it will immediately point out that you have spelled a variable name wrong. For example with your variable 'DownLoadFolder' if you typed 'downloadfolder' and you were using Option Explicit, VB would automatically change it to 'DownLoadFolder', however if you accidentally typed 'dowmloadfolder' it would remain like that and you would know it was a typo. Without Option Explicit if you typed 'DowmLoadFolder' (or dowmloadfolder) it might take you quite a while to figure out where your problem was when your program didn't work correctly.


----------------------------------------------------------------------------------------------------------------------------------------------------------
My Articles:
Using the VB6 Debugger • Automatic Insertion of Procedure Names
A Textbox ActiveX Control That Limits Input to Numbers • Spell Check a Textbox
Improved Formatting Tags

Marty - MVP 2009, 2010, 2011
link

answered 2011-12-09 at 08:38:53

MartinLiss's gravatar image

MartinLiss

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×2

Asked: 12/08/2011 03:04

Seen: 266 times

Last updated: 12/15/2011 01:48