473,508 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to automatically input the user name in a field when record is created?

5 New Member
Access 2007:

I'm trying to automatically input the username of the person who created a record in a form called Data Input.
I created a Login Form which works perfectly.
In my Data Input form, in my "User" field, I wrote CurrentUser() in default value of my field. Everytime I create a record it writes Admin, even if I change user in my Login Form.
How can I get my field to take the username that was entered in my Login form?

Thank you
May 15 '14 #1
9 14654
Seth Schrock
2,965 Recognized Expert Specialist
Since Access 2007 did away with the built-in user controls, the CurrentUser() function will always return Admin. What you need to do is create your own function to pull the user name from your login form. We would need to know how you are storing this information to be able to help you write the function, however.
May 15 '14 #2
mecrazie
5 New Member
I have a table with all the users and their passwords.
The Login form gets the information from this table.
However, I do not store the login information anywhere for now. I know there are many options but I wasn't sure how to proceed from there. You're saying that I should return the information from the login form to a new table (recording the entries of the users) in order for Access to know which user is creating the record in my form?
May 15 '14 #3
Seth Schrock
2,965 Recognized Expert Specialist
That is one way. There is also the option of hiding the login form which allows you to reference the value on the form. You can also use temporary variables (tempvars). This last option is what I use to store the login information. There is nothing wrong with the other options though.

If you do choose to do the TempVar method, then during your login process, you would need to create the TempVar.
Expand|Select|Wrap|Line Numbers
  1. TempVars.Add "User", your login information
You could then create a function to pull this information.
Expand|Select|Wrap|Line Numbers
  1. Public Function LoginInfo()
  2. LoginInfo = TempVars("User").Value
  3. End Function
Then you can just call this function in your default value property in your form.
May 15 '14 #4
mecrazie
5 New Member
Since I'm new to VBA, I understand the code you wrote, just don't know where to input it.
This is my OK button code in my Login Form :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. 'Check to see if data is entered into Username
  3.  
  4.     If IsNull(Me.txtUserID) Then
  5.         MsgBox "Please enter a UserID.", vbOKOnly, "Required Data"
  6.             Me.txtUserID.SetFocus
  7.         Exit Sub
  8.     End If
  9.  
  10. 'Check to see if data is entered into Password
  11.  
  12.     If IsNull(Me.txtPassword) Then
  13.         MsgBox "You must provide a password.", vbOKOnly, "Required Data"
  14.             Me.txtPassword.SetFocus
  15.         Exit Sub
  16.     End If
  17.  
  18. 'Lookup correct password for username entered in Username
  19.  
  20.     Me.PasswordLookup.Value = DLookup("[Password]", "User Login", "[UserID] ='" & Me.txtUserID & "'")
  21.  
  22.  
  23. 'Compare value of Password to PasswordLookup and
  24.  
  25.     If Me.txtPassword.Value = Me.PasswordLookup.Value Then
  26.  
  27.  
  28.         'Close logon form
  29.  
  30.         DoCmd.Close acForm, "Login Form", acSaveYes
  31.  
  32.     Else
  33.             MsgBox "Invalid Username/Password. Please try again.", vbOKOnly, "Invalid Entry!"
  34.         Me.txtPassword.SetFocus
  35.         Exit Sub
  36.     End If
  37. End Sub
Where do I enter your code?
By the way, you are life saver!

Thank you
May 15 '14 #5
Seth Schrock
2,965 Recognized Expert Specialist
Line 27 is where you would enter the first block of code. The second block would need entered in a regular code module (not in a form module or class module).
May 15 '14 #6
mecrazie
5 New Member
One last question.. what would be my login information for the first block of code. I get the following error code :
Compile Error :
Wrong number of arguments or invalid property assignment.

Here is the first block of code that I entered :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. 'Check to see if data is entered into Username
  3.  
  4.     If IsNull(Me.txtUserID) Then
  5.         MsgBox "Please enter a UserID.", vbOKOnly, "Required Data"
  6.             Me.txtUserID.SetFocus
  7.         Exit Sub
  8.     End If
  9.  
  10. 'Check to see if data is entered into Password
  11.  
  12.     If IsNull(Me.txtPassword) Then
  13.         MsgBox "You must provide a password.", vbOKOnly, "Required Data"
  14.             Me.txtPassword.SetFocus
  15.         Exit Sub
  16.     End If
  17.  
  18. 'Lookup correct password for username entered in Username
  19.  
  20.     Me.PasswordLookup.Value = DLookup("[Password]", "User Login", "[UserID] ='" & Me.txtUserID & "'")
  21.  
  22.  
  23. 'Compare value of Password to PasswordLookup and
  24.  
  25.     If Me.txtPassword.Value = Me.PasswordLookup.Value Then
  26.         TempVars.Add "User", "UserID", "Me.txtUserID"
  27.  
  28.  
  29.  
  30.         'Close logon form
  31.  
  32.         DoCmd.Close acForm, "Login Form", acSaveYes
  33.  
  34.     Else
  35.             MsgBox "Invalid Username/Password. Please try again.", vbOKOnly, "Invalid Entry!"
  36.         Me.txtPassword.SetFocus
  37.         Exit Sub
  38.     End If
  39. End Sub
  40.  
May 15 '14 #7
Seth Schrock
2,965 Recognized Expert Specialist
Drop the "UserID" and the double quotes around the control reference so that it is just
Expand|Select|Wrap|Line Numbers
  1. Tempvars.Add "User", Me.txtUserID
That names the tempvar "User" and gives it the value that is in Me.txtUserID. You can name it whatever you want. You would just have to change the reference in your other function. If you leave the double quotes around the control reference, then it will pass that text as a literal string so TempVar("User").Value would equal Me.txtUserID instead of the value that was in the textbox.
May 15 '14 #8
mecrazie
5 New Member
Run Time error '32538'
Can only store data and not an object.

I tried the following :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public Function LoginInfo()
  3. LoginInfo() = TempVars.Add("User").Value
  4. End Function
Doesn't give me the runtime error but it tells me that Microsoft Access can't find my function (in my form).

I'm sorry, it's probably really easy for others...
May 15 '14 #9
Seth Schrock
2,965 Recognized Expert Specialist
Just copy and paste what I have in the second block of code in post #4. That will fix the function.

In doing some research online, I discovered that you can't call custom functions in the Default Value property of controls. So we will just have to create the functionality of the default value in code. In the form's After Update event, add the following.
Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.    Me!User = LoginInfo
  3. End If
May 15 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1942
by: Nona Me | last post by:
Is there a program that can make automatically input forms and views using the table design information in mysql. for example: in mysql you have a table: Customer with the next field: Name,...
9
2416
by: Therese A. Sorna | last post by:
Hello all... I am using Access 2002. I have database set to add users names to the user level security when they are added to a particular table in a database, using the Catalog and...
7
2226
by: jballard | last post by:
Hello, I have a database set-up with a form and two subforms in it. I have one of the subforms (replacement parts) set-up where you can pick part numbers from a drop down box and also pick a...
5
1450
by: Don Sealer | last post by:
I'd like to have a name show up on a report. I have a report based on a query. When the report is activated it asks for a name. I input the name and the query sorts the data and displays it in...
15
2897
by: Tom Nowak | last post by:
I am writing a webapp in which a user is required to enter a login id and password on a login form. I have forms authenticaion coded in my web.config. Once the user is logged in, I want to use the...
3
1771
by: Funky | last post by:
Hi, I have developed an ASP.NET application which has been running in production for around 3 months without any major glitches. Recently, a user was attempting to upload a rather large CSV file...
7
3791
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
3
1759
by: lmeeson | last post by:
hi guys, I am totally new to coding so please bare with me. I have a file upload field in a form on a PHP page for users to upload pictures, i also have the following fields in my MySQL database ...
3
1641
by: cloa | last post by:
I have seen the answer for importing multiple spreadsheets though I haven't tried it. How do I include the spreadsheet filename as the first field for all rows. By the way how does this handle if the...
1
924
by: SeadooRider | last post by:
I have a database that has in the main table, FirstName, MiddleInitial, and LastName, among other fields. In my form, the lastname is a combobox that pulls from a POC table. The Firstname and MI...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.