Solved

access 2010 - getting user info using environ()

Posted on 2013-06-20
13
515 Views
Last Modified: 2013-06-28
I'm beginning work on a new database using Access 2010.  An idea I had was to record the user's information when a new record was created, or perhaps when it is changed.  Anyway, I was considering using Environ("USERNAME") and I've done a bit of research but can't figure out how to implement it so that it populates a field I have reserved for this in my table/form.

I think part of my problem is that I don't know VB.  

Could someone assist me with some advice built for a newb?  I've seen some code snippets on this, but I don't know where this code needs to go or the best way to trigger it to populate the record...

Thanks Experts!!
0
Comment
Question by:Nero83
  • 7
  • 6
13 Comments
 
LVL 75
ID: 39263951
1). Is your database in a Trusted  Location.

2) Is Macro Security set to Low?
0
 
LVL 75
ID: 39263973
1
2
0
 

Author Comment

by:Nero83
ID: 39263977
Thanks.

Yes and yes.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 75
ID: 39263982
and 3)
Open VBA Editor >> Tools>>References.
Are any shown as

**MISSING <SomeReferenceName>

?

If so, resolve this.

mx
0
 

Author Comment

by:Nero83
ID: 39264019
It's clean.
0
 
LVL 75
ID: 39264037
What is the exact error ?
0
 

Author Comment

by:Nero83
ID: 39264054
No error, I don't know how to set it up in the first place...sorry if I was unclear.  I've never used it before.  My issue is that there are many people who will be using it and my company has a lax security policy, and I was looking for a solution to record who does what besides people self-reporting their name on the record.
0
 
LVL 75
ID: 39264073
oops ... sorry, I completely misunderstood the Q - thought you were having the typical issue with Environ().

OK ... this is the basic idea in VBA Code
You use the Form's BeforeUpdate event, which would end up looking like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.NewRecord = True
    Me![YourUserCreatedNameField] =Environ ("UserName")
Else
   Me![YourUserLastUpdatedNameField] =Environ ("UserName")
End If

End Sub

Of course, use your actual Field Names in the underlying table or query driving the form.

mx
0
 

Author Comment

by:Nero83
ID: 39264087
Thanks much.  You always help me out with my Access issues.  I'll give it a try and Accept as Solution afterward...looks simple enough even for me.
0
 
LVL 75
ID: 39264109
OOPS ... left of a 'Then'

If Me.NewRecord = True Then

1
0
 

Author Comment

by:Nero83
ID: 39264130
OK, thanks.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 39264132
Sorry about all the confusion at first.  Whenever I see Environ() ... I immediately assume an issue.

FYI ... this is the better and safer approach to getting the User namne

http://access.mvps.org/access/api/api0008.htm

Put that code in a standard VBA Module, then call  fOSUsrName() instead of Environ()If Me.NewRecord = True Then
    Me![YourUserCreatedNameField] =  fOSUsrName()
Else
   Me![YourUserLastUpdatedNameField] =  fOSUsrName()
End If

End Sub
0
 

Author Closing Comment

by:Nero83
ID: 39285962
Perfect!  I've heard there are problems with Environ, so I like this function, too.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 …

860 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