Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / May 2008

Tip: Looking for answers? Try searching our database.

How to Call a Function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ryguy7272 - 22 May 2008 21:14 GMT
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---

Signature

RyGuy

Daniel Pineault - 22 May 2008 22:46 GMT
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

Signature

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.

> 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
[quoted text clipped - 26 lines]
> Thanks so much!!
> Ryan---
ryguy7272 - 22 May 2008 23:01 GMT
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---

Signature

RyGuy

> 1. Copy and paste your function code into a module
>
[quoted text clipped - 43 lines]
> > Thanks so much!!
> > Ryan---
Klatuu - 22 May 2008 23:20 GMT
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.
Signature

Dave Hargis, Microsoft Access MVP

> 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
[quoted text clipped - 56 lines]
> > > Thanks so much!!
> > > Ryan---
ryguy7272 - 23 May 2008 00:46 GMT
Thank you so much!  Just what I was looking for!!

Regards,
Ryan---

Signature

RyGuy

> 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
[quoted text clipped - 73 lines]
> > > > Thanks so much!!
> > > > Ryan---
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.