Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 8th, 2007, 06:38 PM
Authorized User
 
Join Date: Aug 2006
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Name

Hello:

Okay, I'm trying to pull the user login and found some code which I thought would do it, but ain't.

My code is (declared in a module):
Code:
Option Compare Database

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

    Public Function CNames(UserOrComputer As Byte) As String
    'UserorComputer; 1=User, anything else = computer
    Dim NBuffer As String
    Dim Buffsize As Long
    Dim wOK As Long

    Buffsize = 256
    NBuffer = Space$(Buffsize)

    If UserOrComputer = 1 Then
        wOK = api_GetUserName(NBuffer, Buffsize)
         CNames = Trim$(NBuffer)
    Else
        wOK = api_GetComputerName(NBuffer, Buffsize)
        CNames = Trim$(NBuffer)
    End If
End Function
I then am testing in a simple form with:
Code:
Private Sub Form_Open(Cancel As Integer)

If GetUserNameA = "" Then GetUserNameA = "'Houston, we have a problem'"

If GetComputerNameA = "" Then GetComputerNameA = "VOID"

MsgBox "Your user name is " & GetUserNameA & " and your computer names is " & GetComputerNameA & "."
End Sub
However, I'm getting the houston we have a problem and void instead of the computer name and user name. Any thoughts?

Thanks in advance.

Arholly

 
Old February 9th, 2007, 03:02 AM
Authorized User
 
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Yes I would like to do the same except have this fire when the database is opened so that the database can check whether that person has the appropriate privileges to access the database. I can't figure it out either and am not that good with making API calls.

 
Old February 9th, 2007, 08:42 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I use these functions (in a module) to get the same information:

Function sU()
sU = (Environ$("Username"))
End Function

Function sWS()
sWS = (Environ$("Computername"))
End Function

Then when I need the Username or Workstation name, I just call sU() and sWS().

HTH


mmcdonal
 
Old February 9th, 2007, 10:19 AM
Authorized User
 
Join Date: Aug 2006
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. That works a lot better than what I was using. I was wondering something though. I want to log when a user opens the DB. I can do it through running a RunSQL Macro when the DB opens, but it asks the user if it is ok to append to the table. I don't want it to ask, I just want it to do it and not have the user know. How can I do this?

Thanks again for all of your help.

Arholly

 
Old February 9th, 2007, 10:46 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You can disable those warnings with:

DoCmd.SetWarnings False

and reenable them with:

DoCmd.SetWarnings True

Put this before and after the code that inserts the login. I use this method too, but I do it with ADO so there are no warnings.

I also put the logout date and time in with the main form's On Close event.

HTH

BTW - what's a macro? =)


mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Session ID swapping from user to user... greenwar VS.NET 2002/2003 3 September 7th, 2007 08:44 AM
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
login failed for user domain\user babakwx SQL Server 2000 2 May 30th, 2006 12:28 PM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Run DTS with a windows user, not a SQL user Kalli SQL Server DTS 1 September 15th, 2005 11:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.