compilation paradox 
Author Message
 compilation paradox

I have a script that repeats the same three lines of code at two different
places. The script works fine that way. However, if I put the aforementioned
three lines of code into a "sub", and replace the aforementioned two
instances of that code with calls to the sub, I get an error either at
compile time or at runtime. Can anybody give me a clue as to what might be
going on?
If it helps, this is the script:

Dim cell(3, 5) As Object
Dim storage As Object
Dim stackTop(3) As Integer

Private Sub CommandButton1_Click()
Call react(1)
End Sub

Private Sub CommandButton2_Click()
Call react(2)
End Sub

Private Sub CommandButton3_Click()
Call react(3)
End Sub

Private Sub UserForm_Initialize()
'''''''''''''''''''''''''''''''''
Set cell(1, 1) = TextBox1
Set cell(1, 2) = TextBox2
Set cell(1, 3) = TextBox3
Set cell(1, 4) = TextBox4
Set cell(1, 5) = TextBox5
Set cell(2, 1) = TextBox6
Set cell(2, 2) = TextBox7
Set cell(2, 3) = TextBox8
Set cell(2, 4) = TextBox9
Set cell(2, 5) = TextBox10
Set cell(3, 1) = TextBox11
Set cell(3, 2) = TextBox12
Set cell(3, 3) = TextBox13
Set cell(3, 4) = TextBox14
Set cell(3, 5) = TextBox15

Set storage = TextBox16

For i = 1 To 5
cell(1, i).Value = Val(i)
Next i

For j = 2 To 3
For i = 1 To 5
cell(j, i).Value = ""
Next i
Next j

storage.Value = ""

stackTop(1) = 1
stackTop(2) = 6
stackTop(3) = 6
''''''''''''''''''''
End Sub

Public Sub react(j As Integer)
''''''''''''''''''''''''''''''
i = stackTop(j)
stored = storage.Value

If stored = "" Then
    If i < 6 Then
        storage.Value = cell(j, i).Value
        cell(j, i).Value = ""
        stackTop(j) = stackTop(j) + 1
    End If

ElseIf i = 6 Then
    cell(j, i - 1).Value = stored
    storage.Value = ""
    stackTop(j) = stackTop(j) - 1

ElseIf Val(stored) < Val(cell(j, i).Value) Then
    cell(j, i - 1).Value = stored
    storage.Value = ""
    stackTop(j) = stackTop(j) - 1

End If
''''''''''''''''''''''''''''''
End Sub

Public Sub codeblock(ByVal j As Integer, ByVal i As Integer)
cell(j, i - 1).Value = stored
storage.Value = ""
stackTop(j) = stackTop(j) - 1
End Sub

'NOTE: Replacing the repeated code in "react"
'with a call to "codeblock" results in an error
'at compile time.



Wed, 18 May 2005 02:49:23 GMT  
 compilation paradox
Hi, Elaine,

The only thing I found I needed to make the code work was to put the declaration

    Dim stored As Variant

at the top, along with the other global variables (or else pass it to codeblock
as a parameter). Until you do that, the variable "stored" is undefined in
codeblock (technically, it's automatically declared but never initialized).

Problems like this are easily avoided by putting Option Explicit at the top of
the module, or setting the option "Require variable declaration" in Tools |
Options to put it there automatically.

--
Regards,
Jay Freedman
Microsoft Word MVP        Word MVP FAQ site: http://www.mvps.org/word

Quote:

> I have a script that repeats the same three lines of code at two
> different places. The script works fine that way. However, if I put
> the aforementioned three lines of code into a "sub", and replace the
> aforementioned two instances of that code with calls to the sub, I
> get an error either at compile time or at runtime. Can anybody give
> me a clue as to what might be going on?
> If it helps, this is the script:

> Dim cell(3, 5) As Object
> Dim storage As Object
> Dim stackTop(3) As Integer

> Private Sub CommandButton1_Click()
> Call react(1)
> End Sub

> Private Sub CommandButton2_Click()
> Call react(2)
> End Sub

> Private Sub CommandButton3_Click()
> Call react(3)
> End Sub

> Private Sub UserForm_Initialize()
> '''''''''''''''''''''''''''''''''
> Set cell(1, 1) = TextBox1
> Set cell(1, 2) = TextBox2
> Set cell(1, 3) = TextBox3
> Set cell(1, 4) = TextBox4
> Set cell(1, 5) = TextBox5
> Set cell(2, 1) = TextBox6
> Set cell(2, 2) = TextBox7
> Set cell(2, 3) = TextBox8
> Set cell(2, 4) = TextBox9
> Set cell(2, 5) = TextBox10
> Set cell(3, 1) = TextBox11
> Set cell(3, 2) = TextBox12
> Set cell(3, 3) = TextBox13
> Set cell(3, 4) = TextBox14
> Set cell(3, 5) = TextBox15

> Set storage = TextBox16

> For i = 1 To 5
> cell(1, i).Value = Val(i)
> Next i

> For j = 2 To 3
> For i = 1 To 5
> cell(j, i).Value = ""
> Next i
> Next j

> storage.Value = ""

> stackTop(1) = 1
> stackTop(2) = 6
> stackTop(3) = 6
> ''''''''''''''''''''
> End Sub

> Public Sub react(j As Integer)
> ''''''''''''''''''''''''''''''
> i = stackTop(j)
> stored = storage.Value

> If stored = "" Then
>     If i < 6 Then
>         storage.Value = cell(j, i).Value
>         cell(j, i).Value = ""
>         stackTop(j) = stackTop(j) + 1
>     End If

> ElseIf i = 6 Then
>     cell(j, i - 1).Value = stored
>     storage.Value = ""
>     stackTop(j) = stackTop(j) - 1

> ElseIf Val(stored) < Val(cell(j, i).Value) Then
>     cell(j, i - 1).Value = stored
>     storage.Value = ""
>     stackTop(j) = stackTop(j) - 1

> End If
> ''''''''''''''''''''''''''''''
> End Sub

> Public Sub codeblock(ByVal j As Integer, ByVal i As Integer)
> cell(j, i - 1).Value = stored
> storage.Value = ""
> stackTop(j) = stackTop(j) - 1
> End Sub

> 'NOTE: Replacing the repeated code in "react"
> 'with a call to "codeblock" results in an error
> 'at compile time.



Wed, 18 May 2005 06:04:44 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. 16-bit compilation in 32-bit compilation

2. SOA_GLOB compilation error : Help !

3. global conditional compilation constants in Access 2002

4. Compilation Error

5. conditional compilation constants

6. references and compilation problem

7. Compilation error expected =

8. Compilation error/Automation error

9. Compilation Error 424

10. ACC2000: Compilation Question

11. Access 97: MDE ignores Conditional Compilation Arguments

12. access mde compilation failure

 

 
Powered by phpBB® Forum Software