Solved

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

Posted on 2016-08-10
4
82 Views
Last Modified: 2016-08-11
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
Comment
Question by:DColin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Accepted Solution

by:
Anders Ebro (Microsoft MVP) earned 251 total points
ID: 41749971
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
 
LVL 2

Assisted Solution

by:Antonio Salva Ripoll
Antonio Salva Ripoll earned 83 total points
ID: 41750013
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
 
LVL 18

Assisted Solution

by:xtermie
xtermie earned 83 total points
ID: 41750110
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
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 83 total points
ID: 41750833
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question