Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set UserName as default value of a field?? 1

Status
Not open for further replies.

Joeclueless

Technical User
Jan 30, 2002
116
US
For the date, I used (Now()) as the default value and set the date format to general date. this seems to work fine.

How can I set a field in the table to have the UserName as the default value? Or is default value not a way to accomplish this?

Thanks again!

Joe
 
Hi

Before answering your actaul question, are you using Access security?

If yes, then UserName() will return the user name of the Access logged in user,

If no, do you want the Network User Id? Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Hi

OOPS, still asleep, should have been

If yes, then CurrentUser() will return the user name of the Access logged in user

sorry!!!!!!!! Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Hey Ken,

I'm not using Access security. And I believe that the Network User Id is what I'm looking for here.

Thanks for your response!

Thanks again!

Joe
 
Hi

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = &quot;&quot;
End If
End Function
Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Ken,

Thanks for the response! I'm having a bit of trouble using this code. I'm am trying to use the code in a form where the record source is the table that has 'user' as a field. I tried to paste in the code into the event procedure:



Private Sub user_Enter()
Private Declare Function apiGetUserName Lib &quot;advapi32.dll&quot; Alias _
&quot;GetUserNameA&quot; (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = &quot;&quot;
End If
End Function

And I get an error like &quot;Only comments may appear after End Sub, End Function or End Property......&quot;

I have a feeling that I'm going about this all wrong..

any Ideas?? Thanks again!

Joe
 
I also tried to put Ken's code into a module. then in the text box's enter event, had a macro named &quot;Name&quot; RunCode -Function Name &quot;fOSUserName ()&quot;

I guess I'm just not doing this right...

Thanks again!

Joe
 
Hi

You put the code into a Module.

In the before update event of the form put:

Me!User = fOSUserName()

Anotehr General point, be careful about how you name things, NAME is an Access reserved word, using it as a column of object names will lead to tears, make life easy for yourself, use a naming convention! Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Try putting the function declaration in General Declarations, or making it public if using in a module.
 
Ken,

I've gotten back to my project....

I tried to save the code as a module and I put
Me!User = fOSUserName()
in the before update event of the form.

I get the &quot;Only comments may appear after End Sub, End Function or End Property......&quot; error still.

I changed the names of the fields that had names that may be reserved by access. Maybe I need to have a macro run the code?

Also, the (Now()) default value expression does not work anymore.......
Thanks again!

Joe
 
Since this question now involves a module, I am continuing this in the Access Module (VBA Coding) Forum. Refer to:

thread705-456078 Thanks again!

Joe
 
This works if declared as a Public Function....

Thanks Ken!! Thanks again!

Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top