Solved

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

Posted on 2016-08-10
4
62 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
4 Comments
 
LVL 7

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

830 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