By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

Variable not defined?!?

P: 5
Hey, I'm using Vb6 and I have a problem with my code but I don't know why.
Here it is:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. 'Opacity -------------------------
  4. Private Declare Function GetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  5. Private Declare Function SetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  6. Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
  7.  
  8. Private Const GWL_EXSTYLE = (-20)
  9. Private Const WS_EX_LAYERED = &H80000
  10. Private Const LWA_ALPHA = &H2&
  11. '--------------------------------------
  12.  
  13. Private Function Opacity(Value As Byte, Frm As Form)
  14. On Error GoTo ErrorHandler
  15.  
  16. Dim MaxVal As Byte, MinVal As Byte
  17.  
  18. MinVal = 20: MaxVal = 255
  19.  
  20. If Value > MaxVal Then Value = MaxVal
  21. If Value < MinVal Then Value = MinVal
  22.  
  23. SetWindowLongA Frm.hwnd, GWL_EXSTYLE, GetWindowLongA(Frm.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
  24. SetLayeredWindowAttributes Frm.hwnd, 0, Value, LWA_ALPHA
  25.  
  26. ErrorHandler:   Exit Function
  27. End Function
  28.  
  29. Private Sub hsbOpacity_Change()
  30. Opacity hsbOpacity.Value, Me
  31. End Sub
  32.  
  33. Private Sub Text1_Change()
  34. n = Text1
  35. For i = 0 To n - 1
  36. NewStr = NewStr + Text2.Text + ";"
  37. Next i
  38. Text3 = NewStr
  39. End Sub
  40.  
The problem is when I run the program it says "Variable not defined" and highlights "n =" under "Private Sub Text1_Change()" but when I test this part of the code on it's own, it works fine, the other part of the code is for the opacity of the form and this works ok too.
When I put the code together I get the problem, Anyone know a solution?

Thanks, Darren.
Jan 21 '08 #1
Share this Question
Share on Google+
5 Replies


P: 5
I was looking at random code and it gave me an idea, so I tried:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_Change()
  2. Dim n As Variant
  3. Dim i As Variant
  4. Dim NewStr As String
  5. n = Text1
  6. For i = 0 To n - 1
  7. NewStr = NewStr + Text2.Text + ";"
  8. Next i
  9. Text3 = NewStr
  10. End Sub
And it has worked.
Jan 21 '08 #2

kadghar
Expert 100+
P: 1,295
Hey, I'm using Vb6 and I have a problem with my code but I don't know why.
(...)

The problem is when I run the program it says "Variable not defined" and highlights "n =" under "Private Sub Text1_Change()" but when I test this part of the code on it's own, it works fine, the other part of the code is for the opacity of the form and this works ok too.
When I put the code together I get the problem, Anyone know a solution?

Thanks, Darren.
sure, when you write Option Explicit, it forces you to declare any variable used in the code. when you run only a part, the 'option explicit' isnt activated so you dont have to define 'n'. The way to solve it is:
Expand|Select|Wrap|Line Numbers
  1. Remove the line 'Option Explicit' (i dont recomend you doing this, since i think using option explicit is a good habit), or
  2. Declare 'n':  Dim n As Long  - that will do (yeah!, that's better).
Please check in your VB help file the types of variables to see which one fits your code better.

HTH
Jan 21 '08 #3

Expert 5K+
P: 8,434
Yes, you should always use Option Explicit. It prevents a lot of weird problems that can be very difficult to track down. Because when you mistype a variable name, this causes VB to stop and tell you. without this option turned on, VB will just create a new variable with the new name, and continue on.

Think about this. Let's assume that for some reason, you create a variable called nnnnnnnn. Then later, somewhere else in your code, you see this line...
Expand|Select|Wrap|Line Numbers
  1. For Counter = 1 To nnnnnnn
Unless you've got very sharp eyes, you won't spot any difference (trust me, there is one). If you have set Option Explicit for this module, VB will refuse to compile, because the variable nnnnnnn doesn't exist. If you don't, it will create a new local variable at that point. The new variable will have value 0, so the loop will never execute. You can waste days, desperately trying to figure out why your FOR loop never accomplishes anything.


kadghar, I have to take exception to your statement "when you run only a part, the 'option explicit' isnt[sic] activated". Although I can't guarantee it 100%, I'm pretty sure this is not true. If the option is there, it takes effect for that module.
Jan 22 '08 #4

kadghar
Expert 100+
P: 1,295
...
kadghar, I have to take exception to your statement "when you run only a part, the 'option explicit' isnt[sic] activated". Although I can't guarantee it 100%, I'm pretty sure this is not true. If the option is there, it takes effect for that module.
Yes, sorry about that.
Jan 22 '08 #5

P: 5
That's for the info guys, I'll remember this for future use.
Jan 22 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.