Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-10
4
Medium Priority
?
90 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 9

Accepted Solution

by:
Anders Ebro (Microsoft MVP) earned 1004 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 332 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 332 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 332 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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 …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

722 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