469,327 Members | 1,226 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Global variables not retaining their value.

Rabbit
12,516 Expert Mod 8TB
I have Public variables defined in the form module. An on click event for a command button initializes these variables. An on key press form event uses these variables to step sequentially through an array but there is always an error and it looks like the variables become uninitialized.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public intTotal, intCurrent as Integer
  3. Public Moves(1 To 500) as String
  4. Option Explicit
  5.  
  6. Private Sub Load_OnClick()
  7. ' Initializes Variables
  8. End Sub
  9.  
  10. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  11. ' Attempts to use Moves(intCurrent) but they've been uninitialized.
  12. End Sub
  13.  
I am using VBA 6 and MS Access 2003.
I realize I could probably store the information in a table and use hidden fields but I would rather not. I've also tried to put the variables in a non-form module but I come up against the same problem.
Jan 29 '07 #1
16 8985
ADezii
8,800 Expert 8TB
I have Public variables defined in the form module. An on click event for a command button initializes these variables. An on key press form event uses these variables to step sequentially through an array but there is always an error and it looks like the variables become uninitialized.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public intTotal, intCurrent as Integer
  3. Public Moves(1 To 500) as String
  4. Option Explicit
  5.  
  6. Private Sub Load_OnClick()
  7. ' Initializes Variables
  8. End Sub
  9.  
  10. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  11. ' Attempts to use Moves(intCurrent) but they've been uninitialized.
  12. End Sub
  13.  
I am using VBA 6 and MS Access 2003.
I realize I could probably store the information in a table and use hidden fields but I would rather not. I've also tried to put the variables in a non-form module but I come up against the same problem.
If you dimension the Variables Publically in a Standard (not Form) Code Module, and initialize them within a Click() Event of any Command Button on any Form, they will not lose their values. Maybe the problem lies within the Array not being initialized. Try declaring the Array Publically in a Standard Code Module as a Dynamic Array, and Redimension it (Redim) in the KeyDown() Event. Hope this helps.

Expand|Select|Wrap|Line Numbers
  1. Public Moves() As String    ' Declare Dynamic, Public array.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  2.   'After initializing intCurrent in OnClick()
  3.   Redim Moves(intCurrent)
  4. End Sub
Jan 30 '07 #2
Rabbit
12,516 Expert Mod 8TB
I don't think it's the array. Both the integer variables become uninitialized as well. The weird thing is that this is the only time it has happened. I've used global variables previously and they've kept their values. This time they don't.
Jan 30 '07 #3
Rabbit
12,516 Expert Mod 8TB
Let me clarify. I meant that I have used global variables for a different form. I didn't mean to say that the variables used to work and now they don't.
Jan 30 '07 #4
ADezii
8,800 Expert 8TB
I don't think it's the array. Both the integer variables become uninitialized as well. The weird thing is that this is the only time it has happened. I've used global variables previously and they've kept their values. This time they don't.
They will retain their values unless explicitly modified or the app is terminated - that is the problem working with them. I'v seen this happen several times before and the individuals did not have Require Variable Declaration checked in the Editor Tab of the Options Dialog (VBA IDE). If the variable in the local procedure is mispelled, you will not get an Error since this Option is turned off. It would then revert to being a Local Variable within the OnClick() Event and would lose its value once the Event has terminated. I know I am reaching but I have seen this happen.
Jan 30 '07 #5
Rabbit
12,516 Expert Mod 8TB
They will retain their values unless explicitly modified or the app is terminated - that is the problem working with them. I'v seen this happen several times before and the individuals did not have Require Variable Declaration checked in the Editor Tab of the Options Dialog (VBA IDE). If the variable in the local procedure is mispelled, you will not get an Error since this Option is turned off. It would then revert to being a Local Variable within the OnClick() Event and would lose its value once the Event has terminated. I know I am reaching but I have seen this happen.
The option is checked, the spelling is correct, and the code compiles. It's still not working. I have no idea what's wrong with it.
Jan 30 '07 #6
ADezii
8,800 Expert 8TB
The option is checked, the spelling is correct, and the code compiles. It's still not working. I have no idea what's wrong with it.
We are 'really' running out of options. Is the code too extensive to post?. I would love to attempt to duplicate your current scenario and give it a go myself.
Jan 30 '07 #7
Rabbit
12,516 Expert Mod 8TB
The code is a bit long, I don't know what the size limit is for a post. But I think the main problem is that it reads in text from a file of the user's choosing and uses that file to initialize the variables. I'll post what code I can and trim down what I have to.

Some more info about the database just in case it has any bearing on the problem. There are other forms in there with their own code but most of them are contained within themselves. There is only one non-form module and that module only contains one function.
Jan 30 '07 #8
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Dim BW As Boolean
  3. Dim FileSystem, FileStream As Object
  4. Dim Color() As Byte
  5. Dim Group() As Byte
  6. Dim Moves() As String
  7. Dim movecount, movetotal As Integer
  8. Dim temp As String
  9. Dim GNum As Integer
  10. Option Explicit
  11.  
  12. Function PlaceStone(ByRef p As Image)
  13. Dim R, C, T As Byte
  14.  
  15. Select Case Mid(p.Name, 1, 1)
  16.     Case "A"
  17.         R = 1
  18.     Case "B"
  19.         R = 2
  20.     Case "C"
  21.         R = 3
  22.     Case "D"
  23.         R = 4
  24.     Case "E"
  25.         R = 5
  26.     Case "F"
  27.         R = 6
  28.     Case "G"
  29.         R = 7
  30.     Case "H"
  31.         R = 8
  32.     Case "I"
  33.         R = 9
  34.     Case "J"
  35.         R = 10
  36.     Case "K"
  37.         R = 11
  38.     Case "L"
  39.         R = 12
  40.     Case "M"
  41.         R = 13
  42.     Case "N"
  43.         R = 14
  44.     Case "O"
  45.         R = 15
  46.     Case "P"
  47.         R = 16
  48.     Case "Q"
  49.         R = 17
  50.     Case "R"
  51.         R = 18
  52.     Case "S"
  53.         R = 19
  54. End Select
  55.  
  56. Select Case Mid(p.Name, 2, 1)
  57.     Case "A"
  58.         C = 1
  59.     Case "B"
  60.         C = 2
  61.     Case "C"
  62.         C = 3
  63.     Case "D"
  64.         C = 4
  65.     Case "E"
  66.         C = 5
  67.     Case "F"
  68.         C = 6
  69.     Case "G"
  70.         C = 7
  71.     Case "H"
  72.         C = 8
  73.     Case "I"
  74.         C = 9
  75.     Case "J"
  76.         C = 10
  77.     Case "K"
  78.         C = 11
  79.     Case "L"
  80.         C = 12
  81.     Case "M"
  82.         C = 13
  83.     Case "N"
  84.         C = 14
  85.     Case "O"
  86.         C = 15
  87.     Case "P"
  88.         C = 16
  89.     Case "Q"
  90.         C = 17
  91.     Case "R"
  92.         C = 18
  93.     Case "S"
  94.         C = 19
  95. End Select
  96.  
  97. If BW Then
  98.     T = 1 ' White
  99. Else
  100.     T = 2 ' Black
  101. End If
  102.  
  103. Color(R, C) = T
  104.  
  105. If p.Picture = "(none)" Then
  106.     If BW Then
  107.         p.Picture = CurrentProject.Path & "\White.bmp"
  108.     Else
  109.         p.Picture = CurrentProject.Path & "\Black.bmp"
  110.     End If
  111.     BW = Not BW
  112.     Marker.Visible = True
  113.     Marker.Top = p.Top
  114.     Marker.Left = p.Left
  115. End If
  116.  
  117. End Function
  118.  
  119. Private Sub but_Load_Click()
  120. Dim tint As Integer
  121. Dim ctl As Control
  122.  
  123. ReDim Color(1 To 19, 1 To 19)
  124. ReDim Group(1 To 19, 1 To 19)
  125. ReDim Moves(1 To 500)
  126. GNum = 1
  127. movecount = 2
  128. movetotal = 1
  129.  
  130. If Dir(FileName) = "" Then
  131.     MsgBox "No such file.", vbOKOnly
  132.     End
  133. End If
  134.  
  135. Set FileSystem = CreateObject("Scripting.FileSystemObject")
  136. Set FileStream = FileSystem.OpenTextFile(FileName, 1, 0)
  137.  
  138. temp = FileStream.readline()
  139.  
  140. temp = FileStream.readline()
  141. Handicap = Mid(temp, 4, 1)
  142. Ruleset = Mid(temp, 15, InStr(15, temp, "]") - 15)
  143. tint = InStr(1, temp, "KM[") + 3
  144. Komi = Mid(temp, tint, InStr(tint, temp, "]") - tint)
  145.  
  146. temp = FileStream.readline()
  147. BPlayer = Mid(temp, 4, InStr(1, temp, "]") - 4)
  148. tint = InStr(1, temp, "BR[") + 3
  149. BPlayer = BPlayer & " - " & Mid(temp, tint, InStr(tint, temp, "]") - tint)
  150. tint = InStr(1, temp, "PW[") + 3
  151. WPlayer = Mid(temp, tint, InStr(tint, temp, "]") - tint)
  152. tint = InStr(1, temp, "WR[") + 3
  153. WPlayer = WPlayer & " - " & Mid(temp, tint, InStr(tint, temp, "]") - tint)
  154.  
  155. Do
  156.     temp = FileStream.readline()
  157.     If FileStream.atendofstream = True Then End
  158. Loop Until InStr(1, temp, "RE[") > 0
  159. tint = InStr(1, temp, "RE[") + 3
  160. Result = Mid(temp, tint, InStr(tint, temp, "]") - tint)
  161.  
  162. If Handicap >= 2 Then
  163.     BW = True
  164. Else
  165.     BW = False
  166. End If
  167.  
  168. Select Case Handicap
  169.     Case 2
  170.         DP.Visible = True
  171.         DP.Picture = CurrentProject.Path & "\Black.bmp"
  172.         Color(4, 16) = 2
  173.         Group(4, 16) = GNum
  174.         GNum = GNum + 1
  175.  
  176.         PD.Visible = True
  177.         PD.Picture = CurrentProject.Path & "\Black.bmp"
  178.         Color(16, 4) = 2
  179.         Group(16, 4) = GNum
  180.         GNum = GNum + 1
  181.     Case 3
  182.     Case 4
  183.     Case 5
  184.     Case 6
  185.     Case 7
  186.     Case 8
  187.     Case 9
  188. End Select
  189.  
  190. temp = FileStream.readline()
  191. Do
  192.     If Mid(temp, 1, 1) = ";" Then
  193.         If Mid(temp, 4, 1) = "]" Then
  194.             Moves(movetotal) = ""
  195.         Else
  196.             Moves(movetotal) = UCase(Mid(temp, 5, 1)) & UCase(Mid(temp, 4, 1))
  197.         End If
  198.         movetotal = movetotal + 1
  199.     End If
  200.     temp = FileStream.readline()
  201. Loop Until FileStream.atendofstream
  202.  
  203. movetotal = movetotal - 1
  204.  
  205. For Each ctl In Form.Controls
  206.     If ctl.Properties("Name") = Moves(1) And Len(ctl.Properties("Name") = 2) Then
  207.         PlaceStone ctl
  208.         End
  209.     End If
  210. Next ctl
  211.  
  212. End Sub
  213.  
  214. Private Sub but_New_Click()
  215. DoCmd.Close acForm, "frm_Go"
  216. DoCmd.OpenForm "frm_Go"
  217. End Sub
  218.  
  219. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  220. If KeyCode = 39 And movecount <= movetotal Then
  221.     Dim ctl As Control
  222.     If Moves(movecount) = "" Then
  223.         movecount = movecount + 1
  224.         BW = Not BW
  225.         End
  226.     End If
  227.     For Each ctl In Form.Controls
  228.         If ctl.Properties("Name") = Moves(movecount) And Len(ctl.Properties("Name") = 2) Then
  229.             PlaceStone ctl
  230.             movecount = movecount + 1
  231.             End
  232.         End If
  233.     Next ctl
  234. End If
  235. End Sub
  236.  
  237. Private Sub Form_KeyPress(KeyAscii As Integer)
  238. If Form.ActiveControl.Properties("Name") = "FileName" Then End
  239. If KeyAscii = 113 Then DoCmd.Quit
  240. End Sub
  241.  
Jan 30 '07 #9
Rabbit
12,516 Expert Mod 8TB
Looks like I can paste the whole code. The form uses 361 unbound images that are named according to it's position in a grid. Ever heard of Go? This code is supposed to step sequentially through an "sgf" file which holds a list of positions.
Jan 30 '07 #10
Rabbit
12,516 Expert Mod 8TB
The code is unfinished and I have made a lot of changes since my original posting trying to get it to work so you'll have to forgive the coarseness and inefficiencies.
Jan 30 '07 #11
NeoPa
32,181 Expert Mod 16PB
I have Public variables defined in the form module. An on click event for a command button initializes these variables. An on key press form event uses these variables to step sequentially through an array but there is always an error and it looks like the variables become uninitialized.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public intTotal, intCurrent as Integer
  3. Public Moves(1 To 500) as String
  4. Option Explicit
  5.  
  6. Private Sub Load_OnClick()
  7. ' Initializes Variables
  8. End Sub
  9.  
  10. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  11. ' Attempts to use Moves(intCurrent) but they've been uninitialized.
  12. End Sub
  13.  
I am using VBA 6 and MS Access 2003.
I realize I could probably store the information in a table and use hidden fields but I would rather not. I've also tried to put the variables in a non-form module but I come up against the same problem.
If you try to compile the project with this form as you have it you get the message :
Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules
Jan 30 '07 #12
NeoPa
32,181 Expert Mod 16PB
To get around this restriction, consider some of the functionality into a general purpose module.
Jan 30 '07 #13
Rabbit
12,516 Expert Mod 8TB
Yes, I did get that problem at which point I moved the variables into a general module.

That wasn't the problem however.

The problem is that the variables are losing their values after being initialized.

What I did recently was to Dim the variables and moved them back into the form module because I don't think I need public variables after all.

And the variables are still becoming uninitialized.
Jan 30 '07 #14
Rabbit
12,516 Expert Mod 8TB
I figured out the problem. I was using End to exit the sub/function when it found the matching image rather than an Exit For or GoTo. I guess when I do this, whatever was initialized in the global variables loses their value.
Jan 31 '07 #15
NeoPa
32,181 Expert Mod 16PB
Yes. That's exactly right. End finishes off the code execution completely.
End Statement

The End statement syntax has these forms:

Statement Description
  • End Terminates execution immediately. Never required by itself but may be placed anywhere in a procedure to end code execution, close files opened with the Open statement and to clear variables.
Jan 31 '07 #16
Killer42
8,435 Expert 8TB
I figured out the problem. I was using End to exit the sub/function when it found the matching image rather than an Exit For or GoTo. I guess when I do this, whatever was initialized in the global variables loses their value.
Try Exit Sub or Exit Function.
Jan 31 '07 #17

Post your reply

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

Similar topics

10 posts views Thread by Matt | last post: by
4 posts views Thread by Andrew V. Romero | last post: by
122 posts views Thread by Einar | last post: by
5 posts views Thread by Sandman | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.