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. -
Option Compare Database
-
Public intTotal, intCurrent as Integer
-
Public Moves(1 To 500) as String
-
Option Explicit
-
-
Private Sub Load_OnClick()
-
' Initializes Variables
-
End Sub
-
-
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
' Attempts to use Moves(intCurrent) but they've been uninitialized.
-
End Sub
-
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.
16 9251
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. -
Option Compare Database
-
Public intTotal, intCurrent as Integer
-
Public Moves(1 To 500) as String
-
Option Explicit
-
-
Private Sub Load_OnClick()
-
' Initializes Variables
-
End Sub
-
-
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
' Attempts to use Moves(intCurrent) but they've been uninitialized.
-
End Sub
-
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. - Public Moves() As String ' Declare Dynamic, Public array.
- Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
'After initializing intCurrent in OnClick()
-
Redim Moves(intCurrent)
-
End Sub
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.
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.
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.
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.
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.
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.
-
Option Compare Database
-
Dim BW As Boolean
-
Dim FileSystem, FileStream As Object
-
Dim Color() As Byte
-
Dim Group() As Byte
-
Dim Moves() As String
-
Dim movecount, movetotal As Integer
-
Dim temp As String
-
Dim GNum As Integer
-
Option Explicit
-
-
Function PlaceStone(ByRef p As Image)
-
Dim R, C, T As Byte
-
-
Select Case Mid(p.Name, 1, 1)
-
Case "A"
-
R = 1
-
Case "B"
-
R = 2
-
Case "C"
-
R = 3
-
Case "D"
-
R = 4
-
Case "E"
-
R = 5
-
Case "F"
-
R = 6
-
Case "G"
-
R = 7
-
Case "H"
-
R = 8
-
Case "I"
-
R = 9
-
Case "J"
-
R = 10
-
Case "K"
-
R = 11
-
Case "L"
-
R = 12
-
Case "M"
-
R = 13
-
Case "N"
-
R = 14
-
Case "O"
-
R = 15
-
Case "P"
-
R = 16
-
Case "Q"
-
R = 17
-
Case "R"
-
R = 18
-
Case "S"
-
R = 19
-
End Select
-
-
Select Case Mid(p.Name, 2, 1)
-
Case "A"
-
C = 1
-
Case "B"
-
C = 2
-
Case "C"
-
C = 3
-
Case "D"
-
C = 4
-
Case "E"
-
C = 5
-
Case "F"
-
C = 6
-
Case "G"
-
C = 7
-
Case "H"
-
C = 8
-
Case "I"
-
C = 9
-
Case "J"
-
C = 10
-
Case "K"
-
C = 11
-
Case "L"
-
C = 12
-
Case "M"
-
C = 13
-
Case "N"
-
C = 14
-
Case "O"
-
C = 15
-
Case "P"
-
C = 16
-
Case "Q"
-
C = 17
-
Case "R"
-
C = 18
-
Case "S"
-
C = 19
-
End Select
-
-
If BW Then
-
T = 1 ' White
-
Else
-
T = 2 ' Black
-
End If
-
-
Color(R, C) = T
-
-
If p.Picture = "(none)" Then
-
If BW Then
-
p.Picture = CurrentProject.Path & "\White.bmp"
-
Else
-
p.Picture = CurrentProject.Path & "\Black.bmp"
-
End If
-
BW = Not BW
-
Marker.Visible = True
-
Marker.Top = p.Top
-
Marker.Left = p.Left
-
End If
-
-
End Function
-
-
Private Sub but_Load_Click()
-
Dim tint As Integer
-
Dim ctl As Control
-
-
ReDim Color(1 To 19, 1 To 19)
-
ReDim Group(1 To 19, 1 To 19)
-
ReDim Moves(1 To 500)
-
GNum = 1
-
movecount = 2
-
movetotal = 1
-
-
If Dir(FileName) = "" Then
-
MsgBox "No such file.", vbOKOnly
-
End
-
End If
-
-
Set FileSystem = CreateObject("Scripting.FileSystemObject")
-
Set FileStream = FileSystem.OpenTextFile(FileName, 1, 0)
-
-
temp = FileStream.readline()
-
-
temp = FileStream.readline()
-
Handicap = Mid(temp, 4, 1)
-
Ruleset = Mid(temp, 15, InStr(15, temp, "]") - 15)
-
tint = InStr(1, temp, "KM[") + 3
-
Komi = Mid(temp, tint, InStr(tint, temp, "]") - tint)
-
-
temp = FileStream.readline()
-
BPlayer = Mid(temp, 4, InStr(1, temp, "]") - 4)
-
tint = InStr(1, temp, "BR[") + 3
-
BPlayer = BPlayer & " - " & Mid(temp, tint, InStr(tint, temp, "]") - tint)
-
tint = InStr(1, temp, "PW[") + 3
-
WPlayer = Mid(temp, tint, InStr(tint, temp, "]") - tint)
-
tint = InStr(1, temp, "WR[") + 3
-
WPlayer = WPlayer & " - " & Mid(temp, tint, InStr(tint, temp, "]") - tint)
-
-
Do
-
temp = FileStream.readline()
-
If FileStream.atendofstream = True Then End
-
Loop Until InStr(1, temp, "RE[") > 0
-
tint = InStr(1, temp, "RE[") + 3
-
Result = Mid(temp, tint, InStr(tint, temp, "]") - tint)
-
-
If Handicap >= 2 Then
-
BW = True
-
Else
-
BW = False
-
End If
-
-
Select Case Handicap
-
Case 2
-
DP.Visible = True
-
DP.Picture = CurrentProject.Path & "\Black.bmp"
-
Color(4, 16) = 2
-
Group(4, 16) = GNum
-
GNum = GNum + 1
-
-
PD.Visible = True
-
PD.Picture = CurrentProject.Path & "\Black.bmp"
-
Color(16, 4) = 2
-
Group(16, 4) = GNum
-
GNum = GNum + 1
-
Case 3
-
Case 4
-
Case 5
-
Case 6
-
Case 7
-
Case 8
-
Case 9
-
End Select
-
-
temp = FileStream.readline()
-
Do
-
If Mid(temp, 1, 1) = ";" Then
-
If Mid(temp, 4, 1) = "]" Then
-
Moves(movetotal) = ""
-
Else
-
Moves(movetotal) = UCase(Mid(temp, 5, 1)) & UCase(Mid(temp, 4, 1))
-
End If
-
movetotal = movetotal + 1
-
End If
-
temp = FileStream.readline()
-
Loop Until FileStream.atendofstream
-
-
movetotal = movetotal - 1
-
-
For Each ctl In Form.Controls
-
If ctl.Properties("Name") = Moves(1) And Len(ctl.Properties("Name") = 2) Then
-
PlaceStone ctl
-
End
-
End If
-
Next ctl
-
-
End Sub
-
-
Private Sub but_New_Click()
-
DoCmd.Close acForm, "frm_Go"
-
DoCmd.OpenForm "frm_Go"
-
End Sub
-
-
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
If KeyCode = 39 And movecount <= movetotal Then
-
Dim ctl As Control
-
If Moves(movecount) = "" Then
-
movecount = movecount + 1
-
BW = Not BW
-
End
-
End If
-
For Each ctl In Form.Controls
-
If ctl.Properties("Name") = Moves(movecount) And Len(ctl.Properties("Name") = 2) Then
-
PlaceStone ctl
-
movecount = movecount + 1
-
End
-
End If
-
Next ctl
-
End If
-
End Sub
-
-
Private Sub Form_KeyPress(KeyAscii As Integer)
-
If Form.ActiveControl.Properties("Name") = "FileName" Then End
-
If KeyAscii = 113 Then DoCmd.Quit
-
End Sub
-
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.
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.
NeoPa 32,498
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. -
Option Compare Database
-
Public intTotal, intCurrent as Integer
-
Public Moves(1 To 500) as String
-
Option Explicit
-
-
Private Sub Load_OnClick()
-
' Initializes Variables
-
End Sub
-
-
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
-
' Attempts to use Moves(intCurrent) but they've been uninitialized.
-
End Sub
-
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
NeoPa 32,498
Expert Mod 16PB
To get around this restriction, consider some of the functionality into a general purpose module.
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.
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.
NeoPa 32,498
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.
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.
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
|
8 posts
views
Thread by lawrence |
last post: by
|
17 posts
views
Thread by MLH |
last post: by
|
33 posts
views
Thread by MLH |
last post: by
|
122 posts
views
Thread by Einar |
last post: by
|
9 posts
views
Thread by CDMAPoster |
last post: by
|
5 posts
views
Thread by Sandman |
last post: by
| | | | | | | | | | | |