473,586 Members | 2,817 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 14700
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
1949
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, Address, ZipCode, City the 'automatic form creator' should create a input form with the fields Name, Address, ZipCode, City, add a submit and reset...
9
2422
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 ActiveConnection commands. I am getting errors when I try to add a name that is already in the database. I was wondering if anyone knew of a way to have it...
7
2231
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 description of the part from a drop down box. These are two separate fields. I want to be able to pick the part number and have it put in the...
5
1461
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 the report. How can I get that name to appear in the report, like a caption? It has to be able to change each time a different name is entered....
15
2909
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 login id in other forms of the app. I will eventually save a record to a SQL database, and I want the login id to be automatically entered in a...
3
1776
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 and ran into a strange problem. After roughly 3 minutes of processing, the application reprompted the user for their user name and password (NT...
7
3800
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 database search that I am unable to figure out how to access. (The search is implemented in Php/MySQL.) The user enters search values for: name,...
3
1765
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 "image_folder" - this contains a default value of the image folder in my root folder "image_path" - this automatically combines the image_...
3
1643
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 workbooks are sometimes formated as one worksheet and sometimes two worksheets. The format is generally very similar. Function Impo_allExcel() ...
1
928
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 are set as =!(1) etc. After selecting the last name, the firstname and MI are populated correctly. However, when I look at the table or any...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
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 we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.