Benefits of stDocName

willknapp

Registered User.
Local time
Today, 01:46
Joined
Aug 16, 2012
Messages
93
I took over an old Access project and have been going through the code. I have no formal development training - my skills come from a lot of experience.

Can someone explain the benefits of using "stDocName" in the following code?

Private Sub Command122_Click()
On Error GoTo Err_Command122_Click


Dim stDocName As String

stDocName = "rptInventory"
DoCmd.OpenReport stDocName, acPreview


Exit_Command122_Click:
Exit Sub


Err_Command122_Click:
MsgBox Err.Description
Resume Exit_Command122_Click


End Sub

My question is, why wouldn't you just use:

Docmd.OpenReport "rptInventory", acPreview

I'm sure there's a good reason, but like I said, I never had any formal training, so the benefit isn't immediately apparent.

Further, you'll notice that the previous developer didn't bother to provide meaningful names to any of the form controls - so, clearly, they took shortcuts in some places and not others. Since I'm not familiar with the benefits of applying the string to the DoCmd.OpenReport method, I don't know if this is one of those shortcuts that I should ignore, or if this is good programming practice that I should start using so future developers won't look on me with disdain.
 
Can someone explain the benefits of using "stDocName" in the following code? My question is, why wouldn't you just use:...

If the string is to be used more than once, then it would be beneficial to have it in a variable, defined only once.

Likewise, I have seen developers define such a variable in order to call:
Code:
DoCmd.Close acForm, stDocName
However, simpler to close form self is...
Code:
DoCmd.Close acForm, Me.Name
Further, you'll notice that the previous developer didn't bother to provide meaningful names to any of the form controls - so, clearly, they took shortcuts in some places and not others.

Shame shame shame... ;)
 
That's wizard code if I ever saw any. That means whoever 'wrote' that code used the wizard to generate it and that's what the wizard produced.

You are correct, you're code is equivalent. Now, here's a benefit of using a variable (stDocName) instead of the actual value--if you had 5 or 6 different form possiblities you wanted to open based on certain criteria using a variable here makes it easier. You would test all your conditions and assign the correct form to open to stDocName based on what your criteria dictated and then the final line, no matter what form you were opening would be:

DoCmd.OpenReport stDocName, acPreview
 
That's wizard code if I ever saw any. That means whoever 'wrote' that code used the wizard to generate it and that's what the wizard produced.

I figured it had to be something like that - the lack of meaningful control names tipped me off, but I just wanted to make sure I wasn't missing something. The form I'm working with has 7 tabs, each of which has anywhere from 10 to 25 command buttons that are simply used to open other forms - that's it - so I'll leave the variables out of these for the time being.

Thanks for your help!
 
Oh, and thanks for cluing me in on the fact that there's a "Code" tag to use...as you can see, I'm relatively new to these boards...
 
The form I'm working with has 7 tabs, each of which has anywhere from 10 to 25 command buttons that are simply used to open other forms - that's it - so I'll leave the variables out of these for the time being.

You may carefully work through renaming the controls.

1) Pick a control
2) See how many events are wired on it
3) Rename the control
4) Copy code from the old events, click the correct event on the rename control, paste in the code into the VBA editor upon that event
5) Then delete the now abandoned event in the VBA editor screen
6) Compile after you work through the events for one control in order to catch other places in the code which may have been referencing that control, and update those spots
 

Users who are viewing this thread

Back
Top Bottom