How to Call a Function

R

ryguy7272

I trying to run a function, but can’t quite figure out how to call it. The
Access Bible says that you have to add a function call to the Control Source
of a control, but I’m not sure how to do that. I guess the example in the
book used a Form to call the function. Do I need to call the function from a
Form? Can I call the function from a Table? I am trying to populate a Table
with IDs from each person’s computer (I think it is called system ID). Below
is the function:

'******************** Code Start **************************

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 = vbNullString
End If
End Function
'******************** Code End **************************

Thanks so much!!
Ryan---
 
D

Daniel Pineault

1. Copy and paste your function code into a module

2. where ever you need to use the function simply 'call it'. For Instance:
If you wanted to check the fOSUserName when a Form is opened using a form's
open event you'd code it like:

Private Sub Form_Open(Cancel As Integer)

If fOSUserName() = "ryguy7272" then
'All is good continue opening the form
Else
'Unauthorized Access
Cancel =True
End if

End Sub

--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.com/index.php
Please rate this post using the vote buttons if it was helpful.
 
R

ryguy7272

Yes, this does help, but I'm still not clear on how to use this function. I
want to confirm who logs into a DB. Does it make sense to have a list of
users in a Table, and then, let's say have a LoginID, which comes from the
system itself. Then get the DB to match the fields in the user listed in the
Table with the LoginID? Does this make sense? I guess I could get each user
to give me an ID, and then enter that into the system, but I would like to
automate this process as much as possible. Is there an example online that
explains how to do this. I have the Access 2007 Bible, but the books'
coverage of this topic is kind of weak.

Regards,
Ryan---
 
K

Klatuu

The code you have will return the Windows Login for the user currently logged
in to the workstation. You don't need a table unless you want to restrict
access to your database.

Let's say you want to have a control on a form that that shows the user
login. There are two different ways to do this. If you wanted to store the
user name in the form's recordset, you would have to use the form's Load
event to populate the control:

Me.txtUser = fOSUserName

But, if you only want to display it, you can use the control's Control
Source property. In the property dialog for the Control Source Property, you
would type in:
=fOSUserName()
The = and () are both required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top