PDA

View Full Version : [SOLVED:] Module Level Variable Loses Value



BrI
08-09-2017, 10:00 AM
I'm extracting a line of text into a variable in the final sub in a module that contains several subs.

As there are several subs between the one where the value for variable is obtained (last sub) and the one where I want to use it (first sub)- I thought I would use a module level variable.

I declared a module level variable above all subs (I believe this is correct method) as below but it still loses its value when the first/initial sub resumes. Also tried using "Public" declaration.


Dim strText as String

All subs begin below

When I step through the code, I see that after the last sub is run it steps to the "End Sub" line in prior subs instead of just stopping. (FYI all variables are being passed ByVal).

How can I get the variable to keep its value? It's there in the last sub but gone when the first sub resumes.

Paul_Hossler
08-09-2017, 11:26 AM
1. Can you attach a small WB with just the sub calls?

2. Do you reset the global variable in a sub (just checking)

3. Is the global variable passed as a sub parameter (changes to ByVal parameters do not get passed back)

This works



Option Explicit

Dim strText As String

Sub Upper()
strText = vbNullString
MsgBox strText
strText = "Upper"
Middle
MsgBox strText
End Sub

Sub Middle()
strText = strText & " -- Middle"
Lower
End Sub

Sub Lower()
strText = strText & " -- Lower"
End Sub

mdmackillop
08-09-2017, 11:35 AM
As there are several subs between the one where the value for variable is obtained (last sub) and the one where I want to use it (first sub)
This confuses me. You need to use it before it's been created?

D_Marcel
08-09-2017, 11:46 AM
Sorry if I'm misunderstanding, but have you tried:

Public strText as String

Instead of:

Dim strText as String

In my current project I'm using global variables declaring them as in the first example, and all of them works fine. I have now four modules, two containing the main subroutines, one containing auxiliary subroutines and the last one containing public functions.

BrI
08-09-2017, 12:38 PM
Thanks, maybe the outline below will help explain.

I have tried "Public" vs. "Dim"

When I step through, after Sub 3, I get brought to the "End Sub" line in Sub 2 before going to the resume spot in Sub 1 - FYI



Dim strTEXT as String 'Module level variable


Sub 1

Call Sub2(ByVal var1)

'This sub ** resumes here ** after all other calls/subs are complete
' But the strTEXT module level variable set in Sub 3 is blank/nothing

MsgBox strTEXT -----> blank/nothing

End Sub

'------------------------------------


Sub 2 (var1a)

Call Sub3 (ByVal var2)

End Sub


'------------------------------------


Sub 3 (var2a)

strTEXT = ABCD

Msgbox strTEXT ------> correctly shows ABCD

End Sub

mdmackillop
08-09-2017, 12:53 PM
I get brought to the "End Sub" line in Sub 2
This is correct, Sub 3 is included in Sub 2; Sub 2 must end after Sub 3 is complete

Try

Dim strTEXT As String 'Module level variable

Sub aa()
var1 = "Test"
Call bb(var1)
'This sub resumes here after all other calls/subs are complete
' But the strTEXT variable set in Sub 3 is blank/nothing
MsgBox strTEXT & " - aa" '-----> blank/nothing
End Sub


Sub bb(var1a)
var2 = "Trial"
Call cc(var2)
End Sub

Sub cc(var2a)
strTEXT = "ABCD"
MsgBox strTEXT & " - cc" '------> correctly shows ABCD
End Sub

BrI
08-09-2017, 01:04 PM
Thanks your sample is working.

So, at least I know concept is correct, I need to go back and review code.

Paul_Hossler
08-10-2017, 07:25 AM
You seem to pass parameters to the sub but don't use them

ByVal will not return an updated value to a calling sub -- why are you using it?



Option Explicit
Dim strTEXT As String 'Module level variable

Sub Sub1()

strTEXT = ""
MsgBox "1 -- " & strTEXT ' -----> blank/nothing

Call Sub2

MsgBox "3 -- " & strTEXT ' -----> ABCD"

End Sub

Sub Sub2()
Call Sub3
End Sub

Sub Sub3()

strTEXT = "ABCD"

MsgBox "2 -- " & strTEXT ' -----> ABCD
End Sub

BrI
08-10-2017, 08:34 AM
I am passing parameters but am only using them in the receiving subs. Doing this as there are a few sequential steps involved - wanted to put in separate subs to better organize. Maybe there is a better way - kind of new to passing values.

In any case, I have resolved the issue by giving the problem variable a new name in the last step in the final sub - still don't know how or where it lost its value, will probably come to light at some point.

Thanks for the assistance.

offthelip
08-10-2017, 10:18 AM
your problem is the ABCD is a variable which is blank so the code is working correctly try:
change


Sub 3 (var2a)

strTEXT = ABCD

Msgbox strTEXT ------> correctly shows ABCD

End Sub

to:

Sub 3 (var2a)

strTEXT = "ABCD"

Msgbox strTEXT ------> correctly shows ABCD

End Sub