Access VBA Code: How to read through the code

The code below works just fine but I need to understand how to manually follow the code and determine the answer without running the code. I have trouble doing this. Can someone do a step by step walk through the code changing the value as you go along.

Also is there a method to reading code that I can apply to code in general. Sometimes I can do this but most of the time I can't. I feel that there has to be some way of writing this down on paper as you move through the code.

What gets me is that "strWord1 = "One" ends up being " LCase(var3) " shouldn't it be " LCase(var1) " ??

If you use the Access file use the " Letters " button.



Private Sub cmdLetters_Click()
  Dim strWord1 As String, strWord2 As String, strWord3 As String
  strWord1 = "One"
   strWord2 = "Two"
    strWord3 = "Three"
    Call MyProc(strWord1, strWord2, strWord3)
    'ANSWER IS threeTWOone
End Sub

Private Sub MyProc(var3 As String, var2 As String, var1 As String)
lblBox.Caption = LCase(var1) & UCase(var2) & LCase(var3)
End Sub


Unit5-PracticeTest41.accdb
James CoatsComputer Info. Sys. StudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Nick67Commented:
You comment your code, like so

Private Sub cmdLetters_Click()
'dim 3 string variables
   Dim strWord1 As String, strWord2 As String, strWord3 As String
'set the variables
   strWord1 = "One"
    strWord2 = "Two"
     strWord3 = "Three"
'pass the variable into MyProc, which sets lblBox's caption property
     Call MyProc(strWord1, strWord2, strWord3)
     'ANSWER IS threeTWOone
 End Sub

 Private Sub MyProc(var3 As String, var2 As String, var1 As String)
'sub that requires three string variables to be passed in
'The declaration order of arguments and their naming scheme is bass-ackward
'That'll be confusing!

'sets  lblBox's caption
'LCase will force all text to lower case
'UCase will force all text to UPPER case
'& is the string concatenator
 lblBox.Caption = LCase(var1) & UCase(var2) & LCase(var3)
'no spaces or other padding are added
'we'll get just var1VAR2var3 for a result as lblBox's caption
 End Sub

Open in new window


Now, you passed in
Call MyProc(strWord1, strWord2, strWord3)
or  MyProc("One", "Two", "Three")

Look at the sub declaration!
 Private Sub MyProc(var3 As String, var2 As String, var1 As String)
The first argument becomes var3 = "One"
The second argument becomes var2 = "Two"
The last argument becomes var1 = "Three"
because that's how you declared it
Then you do
 lblBox.Caption = LCase(var1) & UCase(var2) & LCase(var3)
or
 lblBox.Caption = LCase("three") & UCase("Two") & LCase("one)
and get
"threeTWOone as a result.

Do you see it now?

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
dustockCommented:
This is pretty straight forward.  In MyProc the arguments are (var3, var2, var1) in that order.
So when you call myProc and pass in strWord1, strWord2, strWord3 they are strWord1 = var3, strWord2 = var2, strWord3=var1

Private Sub cmdLetters_Click()
  Dim strWord1 As String, strWord2 As String, strWord3 As String
  strWord1 = "One"
   strWord2 = "Two"
    strWord3 = "Three"
    Call MyProc(strWord1, strWord2, strWord3) - MyProc("One", "Two", "Three")
    'ANSWER IS threeTWOone
End Sub

Private Sub MyProc(var3 As String, var2 As String, var1 As String)  - var3 = "One", var2 = "Two", var1 = "Three"
  lblBox.Caption = LCase(var1) & UCase(var2) & LCase(var3) - lblBox.Caption = LCase("One") & UCase(Two) & LCase(Three) 
End Sub

Open in new window

James CoatsComputer Info. Sys. StudentAuthor Commented:
Nick,

Thank you this helps quite a lot.

Sam
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mwochnickCommented:
In general when it comes to code syntax (how the code is written) is important.  So in your example the order of the variables being passed to MyProc is important
Call MyProc(strWord1, strWord2, strWord3)

Open in new window

is the same as
Call MyProc("One", "Two", "Three")

Open in new window


so what happen behind the scenes when you make a call to a proc?
When the program begins to execute MyProc it initializes the three variable listed in the proc declaration with the data passed in to the proc from the caller, in your case var3 gets assigned the value "One" because they are both in the first position var2 gets assigned "Two" because they are in the second position and var1 gets assigned the value "Three" because they are in the 3rd position

A way to make this more clear would be to rewrite your code as follows
Private Sub cmdLetters_Click()
  Dim strWord1 As String, strWord2 As String, strWord3 As String
  strWord1 = "One"
  strWord2 = "Two"
  strWord3 = "Three"
 Call MyProc(strWord1, strWord2, strWord3)
 'ANSWER IS threeTWOone
End Sub

Private Sub MyProc(varPositionA As String, varPositionB As String, varPositionC As String)
  lblBox.Caption = LCase(varPositionC) & UCase(varPositionB) & LCase(varPositionA)
End Sub

Open in new window

This eliminates the confusion of thinking that the variable number of the variable name should line up.
Does this make sense?
James CoatsComputer Info. Sys. StudentAuthor Commented:
Dustock,

Sorry just saw your comment after giving points. Thank you for the response it also helps.

Sam
dustockCommented:
No problem, glad we were able to help.
PatHartmanCommented:
What this example tells us is that how you name variables can influence what the reader perceives.   It reminds me of when I was a trainee and spent hours reading code during a Christmas party because the original programmer had named a variable - Dept88.  Originally the variable only applied to department 88.  However, some time later, the variable was used for Departments 24 and 53 and I couldn't figure out how "If Dept88" was selecting these other two departments until sometime toward the end of the party it occurred to me to check the definition of the variable and found it to be an array with THREE values, not a string with just one value.
Nick67Commented:
As @PatHartman says, this was constructed to drive home the lesson that the ORDER in which you pass in arguments is the order they get consumed.  The naming scheme is irrelevant to how code consumes arguments.
The corollary is that your naming scheme is critically important to the debugging and maintenance of your code.
I don't know if your instructor is trying to drive that point home, but it's worth thinking about.

Think about this
Dim strWord1 As String, strWord2 As String, strWord3 As String
How stupid is this?
strWord1

Now, you had just declared that As String
Does the str really tell you anything you don't know or might lose sight of?
Some shops insist on it, but -- to me -- it's a waste of time and effort
Word1
Word2
Word3
Great! :( Not!
They are unique, but uniformitive.
And your naming scheme and declarations should make your code self-documenting.
Dim FirstLCase as String
Dim MiddleUCase as String
Dim SecondLCase as String
Now, those are some useful names

and in the sub
Private Sub MyProc(var3 As String, var2 As String, var1 As String)
 lblBox.Caption = LCase(var1) & UCase(var2) & LCase(var3)
 End Sub

What use are those names?
Even if we don't screw with their order -- which is admittedly stupid, but still --, this is much better

Private Sub MyProc(ThirdPart As String, SecondPart As String, FirstPart As String)
 lblBox.Caption = LCase(FirstPart) & UCase(SecondPart ) & LCase(ThirdPart)
 End Sub

So you have two lessons:
Arguments are consumed in the order declared and passed in.
Use an informative, meaningful and as-simple-as-practicable naming scheme.

By the way -- that means IMMEDIATELY renaming controls like label1 text2 combo3 form4 list6 to something meaningful!
James CoatsComputer Info. Sys. StudentAuthor Commented:
Nick67,

I can't tell you how helpful this has been from all of you. My instructor in what he has explained has not come anywhere near to being as helpful and informative as the instruction has been here on EE.

When he explains something it is confusing and when you ask questions such as what I have asked here I am told it is self evident and it needs no further instruction. I don't get how he is a so called "professional" instructor / teacher. To get anywhere in his class I have to go to YouTube or Experts Exchange.

This makes the test I am going to take a lot easier on my nerves. Now I have something to use that I know will allow me to solve a problem. I really can't thank you guys enough. I wish I could award all of you a 1000 points.

Sam
Nick67Commented:
Now I have something to use that I know will allow me to solve a problem.
it is against Terms of Use to have an Expert do your homework for you :)
And your instructor sounds like he has forgotten what it is likely to be a newbie and skill level of his students.
The code you posted was self-evident -- once you know how procedures consume arguments.
But you aren't going to google that up anywhere, that's too basic.

So that does leave you in the weeds a bit.

But learning technique and best practices from Experts, hey we're always up for that.
And learning in general.
We all get in the weeds when we inherit terribly documented, badly constructed code, and getting help to comprehend existing code is certainly fair game as is help in wrangling the syntax of functionality you are trying to create.

If your instructor DIDN'T name the variables the way you originally posted as an object lesson but foists crap like that on his students as a matter of course, well, then he is a contemptible excuse of an instructor

Base skills and things you should encounter
Normalization: http://www.phlonx.com/portfolio/nf/
One good discussion of best practices: http://www.experts-exchange.com/Database/MS_Access/Q_28581305.html
Expert discussion of naming conventions and then other things: http://www.experts-exchange.com/Database/MS_Access/Q_28141204.html
The Access 2003 VBA help file since everything since has been useless or worse: http://www.microsoft.com/en-us/download/details.aspx?id=19494
When you unpack that, make sure it goes on a drive letter folder (c:\someplace) and not a network share (\\someserver\someshare) or it won't work.

Want to see regression in action?
Search the 2003 file for filesystemobject.
Do the same for Access 2010/2013.
It's a little better now than it was even a few months ago, but the best Help still is the A2003 help


Best of luck!

Nick67
James CoatsComputer Info. Sys. StudentAuthor Commented:
Nick67,

Thanks again for the extra info. The code was not home work but a question on a test that I did not get correct and I wanted to understand it. What you guys posted helped a lot.

Thanks,

Sam
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
Microsoft Access

From novice to tech pro — start learning today.