Variable read before written

I often use the following construct in my VBA code to track the progress of my code execution:

Public Sub Cmd3HideUnhide(sWksName As String, sHideUnhide As String)
   On Error GoTo ErrHandler
   Dim sProcNameCmd3 As String
   sProcNameCmd3 = sWksName & ".HideUnhide.CmdBtn3HideUnhide"

other code
ErrHandler:
       Call DspErrMsg(sProcNameCmd3)
End sub

When I run Project Analyzer it registers an error with the comment:

Variable read before written (along some path): sProcNameCmd3          in HideUnhide.Cmd3HideUnhide     Type: Logic

As I have declared the variable sProcNameCmd3, assigned a value to this variable and later in the same procedure use that variable in a procedure call, I do not understand why this is referred to as "Variable read before written". Surely assigning a value to a variable constitutes writing the variable?
I also do not understand what I should do to rectify the problem.

I shall be grateful to assistance in this matter.
Jos te BraakeDirectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
My guess would be that because it's used in the error handling routine, in theory it could be referenced anytime after the ON ERROR, which could be during or before the statement where you set its initial value.

Does this give the same error?

Public Sub Cmd3HideUnhide(sWksName As String, sHideUnhide As String)
   Dim sProcNameCmd3 As String
   sProcNameCmd3 = sWksName & ".HideUnhide.CmdBtn3HideUnhide"
   On Error GoTo ErrHandler

other code
ErrHandler:
       Call DspErrMsg(sProcNameCmd3)
End sub

Open in new window

0
Martin LissOlder than dirtCommented:
I don't see any problem if procedure DspErrMsg exits. BTW you don't need the "Call" and DspErrMsg sProcNameCmd3 should be enough.
0
Jos te BraakeDirectorAuthor Commented:
Bill, your suggestion solves the problem. By placing
On Error GoTo ErrHandler
after the declaration and assignment of a value to the variable the error does not come up.
I assume this option means that if an error occurs before the On Error statement that the execution will not be handled by the ErrHandler.

I also tried declaring the variable as a public variable at the module level. That also solved the problem.

Can anyone throw any light on why these changes solve the problem?
0
Bill PrewCommented:
Both changes solve the problem by having the variable declared before it could possibly be referenced on the ON ERROR handler, it's really as simple as that.  You used a variable in that handler, and as long as it is declare and initialized (written) before it could be encountered in the handler then the Project Analyzer is happy.  When it saw that you had made the ON ERROR routine active to that handler, and the variable referenced in the handler had not been initialized, it warned you about that as an possible problem.


»bp
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jos te BraakeDirectorAuthor Commented:
Thanks Bill. That solves my problem and gives me the understanding I was looking for.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.