Log in

View Full Version : auto fill



Scooter172
01-23-2011, 08:28 PM
I have a cell on a form that I want to auto fill with user name. How is this done?

domfootwear
01-23-2011, 08:37 PM
I have a cell on a form that I want to auto fill with user name. How is this done?

Try this code:


Application.UserName

domfootwear
01-23-2011, 09:00 PM
I have a cell on a form that I want to auto fill with user name. How is this done?

If in Access VBA, you can try below code:


YourTextBox=CurrentUser()

Scooter172
01-23-2011, 09:11 PM
If in Access VBA, you can try below code:


YourTextBox=CurrentUser()

I used an expression =CurrentUser()

But when it enters the data it enters Admin. does this change once I secure and give permissions to the Database. I have a User Sign On. Shouldn't be entering that instead of admin.

domfootwear
01-23-2011, 09:16 PM
I used an expression =CurrentUser()

But when it enters the data it enters Admin. does this change once I secure and give permissions to the Database. I have a User Sign On. Shouldn't be entering that instead of admin.
You can check below links:
http://www.databasedev.co.uk/login.html

Scooter172
01-24-2011, 12:59 PM
the sign on for the network is what I want to use. Not a given username and password. Ideally I want this to recognize what that network user name is and if the person is logged on to network it will allow user access and enter that name in the appropriate cell.

hansup
01-25-2011, 09:04 AM
See fOSUserName function at www.mvps.org
Get Login name (http://www.mvps.org/access/api/api0008.htm)

Then, for example, if your form includes a text box control named txtUser, you could set its Control Source property to the following:

=fOSUserName()

L@ja
01-25-2011, 09:06 AM
I usually use this code:
'get winusr & mashine name
Set wshshell = CreateObject("WScript.Shell")
Set objenv = wshshell.Environment("Process")
globals.PcID = objenv("COMPUTERNAME")
globals.tmpfolder = objenv("TEMP")
Me.txt_winusername = objenv("USERNAME")

Scooter172
01-25-2011, 12:25 PM
when I use these two:banghead: the result in the text box is #Name?

hansup
01-25-2011, 12:56 PM
I don't know where the problem is. fOSUserName() works for me with Access 2003. See if the MDB in the attached Zip helps clarify anything.

Scooter172
01-25-2011, 01:14 PM
I don't know where the problem is. fOSUserName() works for me with Access 2003. See if the MDB in the attached Zip helps clarify anything.
Thank you Hansup..I found the issue.

hansup
01-25-2011, 01:19 PM
What was the issue?

Someone else may benefit in the future if you're willing to share. :-)

Scooter172
01-25-2011, 01:22 PM
I had set it up as a private function and not from a module.. I am new to access, and sometimes the things I have learned about excel get in the way. Still learning. Thank you for your help!:beerchug:

hansup
01-25-2011, 01:36 PM
Thanks for responding; you may have saved someone else grief.

When you get an error, like #Name, for a control it can be helpful to switch to the Immediate Window (Ctrl+g) and evaluate the control source expression there:

? fOSUserName
Hans

If the control source references other controls on the form, you can add a Debug button to the form:

Private Sub cmdDebug_Click()
Stop
End Sub

Then clicking the button would allow you to reference the values of any of your form's controls:

? Me.txtSomeTextBoxName

Eric58132
01-25-2011, 01:38 PM
YAY! I've always been one to ask questions on this forum, but I think I have a solution that is far quicker than what has been posed here (and I even learned it from here too, a very long time ago).

Set the default value of your text box to be :

Environ("USERNAME")

just as it is written above. It will then display the loginID the user signed onto their computer with.

hansup
01-25-2011, 01:47 PM
Set the default value of your text box to be :

Environ("USERNAME")

just as it is written above. It will then display the loginID the user signed onto their computer with.

Does that actually work for you, Eric? The reason I ask is because I get the #Name? error using Environ in the control source. I think that may be because Environ() is a "sandboxed" function and therefore not available in this context. There may be a registry hack to un-sandbox it, but I wouldn't use it because Environ is insecure ... it can be easily fudged if the user opens a command prompt window, changes the environment variable, and then starts up Access from within the command session.

Eric58132
01-25-2011, 01:56 PM
I hadn't thought of being able to spoof off a different userID through that process, but when I use it as the Default Value (not control source) I do not have any problems.

With the instance where I used Environ("USERNAME"), I was designing an unbound form, and the text box which housed this value was hidden. What I really like about unbound forms is that you prevent records from being half-edited, etc.


You are likely right in that the security breach potential exists with this method, but for the skill level of the users in my DB it doesn't really pose much of an issue.

hansup
01-25-2011, 02:10 PM
Interesting. You're right, I tried it as Control Source instead of Default Value. But it didn't work as Default Value either until I went to Macro->Security and applied an option to unblock unsafe expressions. Then it worked as either Control Source or Default Value.

Hmmm ... not too sure I really want to leave unsafe expressions unblocked, though. I don't fully understand the implications, but I'm guessing Microsoft has a reason for calling them unsafe.:eek: :eek:

I won't argue with your point about being able to trust yours users. But think I'll stick with fOSUserName() because it's more secure and just so darn easy to use ... all I need is that function, which I add to nearly every Access application I create.

Regards,
Hans

Scooter172
01-25-2011, 10:52 PM
This enters correctly in the form but How to I get it to enter that information now in the text box to the Table in which I want to record that data?

hansup
01-26-2011, 08:08 AM
You could insert the username into a new record with a SQL INSERT statement.

Dim strSql As String
strSql = "INSERT INTO YourTable (user_name_field) " & _
"VALUES ('" & fOSUserName() & "');"
CurrentDb.Execute strSql, dbFailonerror

However I got no clue whether that approach fits your situation. Where do you want to store the user name: in a new record; current record in the form's record source; somewhere else?

When do you want to store the user name? Once each time the form is run? Whenever something else happens ... what?

Scooter172
01-26-2011, 09:56 AM
The form will be used to enter data into the table. we will have multiple people entering into Data base at the same time. Therefore I want to have the information that is "AutoEntered" in form to fill the table from which it derived. so the short answer is in a New Record entry each time a New record is created. That way I can track who entered the data. I hope this clarifies this need. Column name on Table is - Created By

hansup
01-26-2011, 11:02 AM
The best I can suggest is to see whether you can leverage Allen Browne's approach for what you need.

Creating an Audit Log (http://allenbrowne.com/appaudit.html)

Scooter172
02-10-2011, 03:42 PM
I want this to fill in and record on a table each time the form is updated. Currently it fills in but the table does not record the entry.

L@ja
02-11-2011, 05:41 PM
it's depend on which type you are using to record data to table...
one of them:
form datasource are binded to table, so record created automatically when move to next record
another one method:
form btn has the following code:


sub btn_click()
dim rs as recordset
set rs=currentdb.openrecordset("mytablename")
rs.addnew
rs("createdon")=now
rs("createdby")=me.txt_usrname
rs.update
rs.close
end sub


hope help you...