Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables

Status
Not open for further replies.

Cndn

Programmer
Nov 22, 2001
22
0
0
CA
For some reason, the following code doesn't work, how come?

Form_load()
Dim Number as integer
Number = 2
End Sub

Command1_click
Number = Number + 2
msgbox Number
End Sub

This code also will not work

Form_load()
Dim Number as integer

Dim Number2 as integer
Number2 = 2

Dim Number3 as integer
Number3 = 3

End Sub

Command1_click
Number = Number2 + Number3
msgbox Number
End Sub


How come???
 
It's all to do with the scope of the variables. If you declare a variable in a procedure you can only use it within that procedure.

rgds
Andy
 
hello there

ur variable scope is private to a specific sub-proc
make ur variables public or form level. then ur above given code will execute properrly.

thanx

ComputerJin
 
Put
Option Explicit
at the top of every Form, Module or Class and the Compiler will tell when a variable is out of scope..."Variable Undefined". Go to Tools / Options / Editor and check "Require Variable Declaration". That will put the Option Explicit at the top ov every new module, Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top