Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 98
  • Last Modified:

What is the meaning of this VBA code snippet from an Access form

Hi Experts,

Please refer to the below code, what does the 'If [n2] = [correct password] Then' line mean?

I can understand the rest but what are the two [n2] and [correct password] values reffering to?

If [n2] = [correct password] Then

   DoCmd.OpenForm "Employee", acNormal
   DoCmd.Close acForm, "LogInForm"

Else

   MsgBox("Password Error")

End If
0
DColin
Asked:
DColin
4 Solutions
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
You *CAN* use that notation to refer to controls or fields on your form.

I would not recommend it, simply because the benefit of using the me. notation is so much greater.
If you one day rename the field/control the above code will fail at RUNTIME (I.e. when your users are using it)
But if the code instead had been written like
if me.n2=me.CorrectPassword then

Open in new window

and you renamed n2 to txt2, then the code would fail at COMPILE time, so you as developer can catch the error way before it shows up at a user.
0
 
Antonio Salva RipollCommented:
Hi DColin.

in your code
If [n2] = [correct password] Then

Open in new window


[n2]  and [correct password] can be either controls or field names, or a combination of controls and field names.
You must check in your form Recordsource property looking for this fields names, if you don't meet them, then check all control names in your form.

If they are controls, then the correct way to reference them is what Anders wrote, using the 'Me.' notation.
If Me.[n2] = Me.[correct password] Then

Open in new window


The line is checking if the value of [n2] is equal to [correct password], if they are equal then closes the Login form and opens the Employee form.

Best regards.

Antonio.
0
 
xtermieCommented:
n2 is probably a parameter you enter and correct password is preset value that you compare n2 to in order to open the Employee form when a correct password is provided
0
 
Jeffrey CoachmanCommented:
DColin,

This is why descriptively named controls are so important.
"n2", should, perhaps, be renamed to: "AttemptedPassword"
..and also add comments, ...so future developers can follow the logic:

So the code would be more "understandable" as:

'If the password the user entered is the same as the correct password, ...Then...
If [AttemptedPassword] = [correct password] Then
   'Open the Emplyees form in the normal mode
   DoCmd.OpenForm "Employee", acNormal
   'Close the Login form
   DoCmd.Close acForm, "LogInForm"
'Or else
Else
   'Display the phrase "Password Error" to the user
   MsgBox("Password Error")
End If
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now