Solved

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

Posted on 2016-08-10
4
43 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 5

Accepted Solution

by:
Anders Ebro (Microsoft MVP) earned 251 total points
Comment Utility
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
Comment Utility
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 17

Assisted Solution

by:xtermie
xtermie earned 83 total points
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now