dbTalk Databases Forums  

Can I Set the User Login as Default Value for myLogID in a Table?

comp.databases.ms-access comp.databases.ms-access


Discuss Can I Set the User Login as Default Value for myLogID in a Table? in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Patrick A
 
Posts: n/a

Default Can I Set the User Login as Default Value for myLogID in a Table? - 04-23-2010 , 04:35 PM






All,

I have a formless db with a table containing a myLoginID column.

I would like to use the currently logged-in user's ID as the default
value for said column when my (outside) VB app writes a record to the
DB.

I would like to avoid doing this in VB code if possible.

I understand Access did not used to support the use of a custom
function a Default Value - is that still true?

If so, is there another way to do this?

If not, what am I doing wrong? My function code is below.

I can't save the table when I use UserName() as the default.

Thanks,

Patrick

Function UserName() 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
UserName = Left$(strUserName, lngLen - 1)
Else
UserName = vbNullString
End If
End Function

If not,

Reply With Quote
  #2  
Old   
Salad
 
Posts: n/a

Default Re: Can I Set the User Login as Default Value for myLogID in a Table? - 04-23-2010 , 06:19 PM






Patrick A wrote:

Quote:
All,

I have a formless db with a table containing a myLoginID column.

I would like to use the currently logged-in user's ID as the default
value for said column when my (outside) VB app writes a record to the
DB.

I would like to avoid doing this in VB code if possible.

I understand Access did not used to support the use of a custom
function a Default Value - is that still true?

If so, is there another way to do this?

If not, what am I doing wrong? My function code is below.

I can't save the table when I use UserName() as the default.

Thanks,

Patrick

Function UserName() 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
UserName = Left$(strUserName, lngLen - 1)
Else
UserName = vbNullString
End If
End Function

If not,
I created a code module and entered the following BEFORE the function,
at the top of the code module. Did you compile your app?

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

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

Reply With Quote
  #3  
Old   
Patrick A
 
Posts: n/a

Default Re: Can I Set the User Login as Default Value for myLogID in a Table? - 04-26-2010 , 05:32 PM



Salad,

Thanks, yes, I did. The function returns the username to a Query, I
just can't get it to return the username as a default in a table.

Here's my compelte code, which I compiled...

Option Compare Database
Option Explicit
Public Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As
Long

Function UserName() 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
UserName = Left$(strUserName, lngLen - 1)
Else
UserName = vbNullString
End If
End Function

On Apr 23, 7:19*pm, Salad <sa... (AT) oilandvinegar (DOT) com> wrote:
Quote:
Patrick A wrote:
All,

I have a formless db with a table containing a myLoginID column.

I would like to use the currently logged-in user's ID as the default
value for said column when my (outside) VB app writes a record to the
DB.

I would like to avoid doing this in VB code if possible.

I understand Access did not used to support the use of a custom
function a Default Value - is that still true?

If so, is there another way to do this?

If not, what am I doing wrong? *My function code is below.

I can't save the table when I use UserName() as the default.

Thanks,

Patrick

* * Function UserName() 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
* * * * * * UserName = Left$(strUserName, lngLen - 1)
* * * * Else
* * * * * * UserName = vbNullString
* * * * End If
* * End Function

If not,

I created a code module and entered the following BEFORE the function,
at the top of the code module. *Did you compile your app?

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

Seehttp://www.mvps.org/access/api/api0008.htm- Hide quoted text -

- Show quoted text -

Reply With Quote
  #4  
Old   
Salad
 
Posts: n/a

Default Re: Can I Set the User Login as Default Value for myLogID in a Table? - 04-26-2010 , 05:59 PM



Patrick A wrote:
Quote:
Salad,

Thanks, yes, I did. The function returns the username to a Query, I
just can't get it to return the username as a default in a table.
Is there a need to set the default value using a UDF in the table? If
you can't use a form, use a query.

Create Table1
Create Query1 (Select F1, F2, GetUserName() AS UserN From Table1)
Use Query1 for input or as recordsource for forms/reports.

Use Query1. You can't use UDFs as the default values of a table.

Reply With Quote
  #5  
Old   
Patrick A
 
Posts: n/a

Default Re: Can I Set the User Login as Default Value for myLogID in a Table? - 04-28-2010 , 03:46 PM



Thanks Salad, I'll add it via the query. Just trying to avoid any
unnecessary code.

On Apr 26, 6:59*pm, Salad <sa... (AT) oilandvinegar (DOT) com> wrote:
Quote:
Patrick A wrote:
Salad,

Thanks, yes, I did. *The function returns the username to a Query, I
just can't get it to return the username as *a default in a table.

Is there a need to set the default value using a UDF in the table? *If
you can't use a form, use a query.

Create Table1
Create Query1 (Select F1, F2, GetUserName() AS UserN From *Table1)
Use Query1 for input or as recordsource for forms/reports.

Use Query1. *You can't use UDFs as the default values of a table.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.