[RESOLVED] Global Variable Not Defined Error
Could someone plz tell me what I need to change?
When I put this function in my program it causes any reference to a global variable to return a "variable not defined" error.
Option Explicit
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd) + Low
End Function
Dim x As Long
Private Sub Form_Load()
x = 2 ' i get a variable not defined error here
Caption = x
End Sub
[510 byte] By [
jalak7] at [2007-11-20 10:14:39]

# 1 Re: [RESOLVED] Global Variable Not Defined Error
Because you added an extra End Function!
Option Explicit
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd) + Low
End Function
Private Sub Form_Load()
MsgBox Rand(2, 7)
End Sub
# 2 Re: [RESOLVED] Global Variable Not Defined Error
It still returns the same error msg though. I realized after I posted that I added an extra "End Function". I took it out. It didn't change anything.
jalak7 at 2007-11-9 19:36:29 >

# 3 Re: [RESOLVED] Global Variable Not Defined Error
Ok. I figured out that putting the function at the end of my program makes it so I don't get the variable error. I don't know why though...
jalak7 at 2007-11-9 19:37:28 >

# 4 Re: [RESOLVED] Global Variable Not Defined Error
NO. You must have taken out the Option Explicit at the top.
Private Sub Form_Load()
dim x as Integer
x = 2 ' i get a variable not defined error here
me.Caption = x
End Sub
# 5 Re: [RESOLVED] Global Variable Not Defined Error
I didn't remove the Option Explicit.
The code I posted in my first post brings back a "variable not defined" error. But if you take the same code and just switch the Function Rand, and the Sub For Load, then it works fine. I tried it just now, and made sure that Option Explicit was in there.
jalak7 at 2007-11-9 19:39:24 >

# 6 Re: [RESOLVED] Global Variable Not Defined Error
If I am reading your original post correctly, you can't put a DIM statement between functions/subs. Put the DIM statement before the first function/sub and that should solve your problem. All of your public/module-level declarations/dims must come before the first routine in your form/module/class.
# 7 Re: [RESOLVED] Global Variable Not Defined Error
Don't edit your first post and replace the code. That confuses people.
I answered the changed code above.
# 8 Re: [RESOLVED] Global Variable Not Defined Error
LaVolpe, Ty. I didn't know that. That was why it wasn't working right.
jalak7 at 2007-11-9 19:42:36 >

# 9 Re: [RESOLVED] Global Variable Not Defined Error
dglienna, sry. I realized right away that I posted the code wrong in my first post and quickly tried to edit the extra "end function" out before anybody read it, but you were too quick for me.
jalak7 at 2007-11-9 19:43:28 >
