Question:
Help with MS Access Macros?
topherG
2008-12-24 08:52:54 UTC
In my database I'm trying to create a splash screen which determines the windows login ID, checks to see if the user exists in the database, and opens the correct form depending on whether the condition is true or false.

I have a splash created with a timer interval of 2000 set. In the On Timer event I want to place a macro, but I'm not sure what condition to use on the macro.

Here is what I have that isn't working:
[Forms]![Splash]![txtUser] = DLookUp("[User ID]","[Analysts]","[User ID]=" & [Forms]![Splash]![txtUser])

The control on the form named txtUser has a control source of:
=Environ("Username")

This obtains the windows login ID.

Again, I'm trying to determine if the current user is in the database. If they are in the database - then open a form. Seems simple, but I can't get it right...

Please help!
Four answers:
ArmchairPilot
2008-12-24 09:39:34 UTC
I would use DCount to determine if the ID is in the table:



varUser = Environ("Username")

If (DCount("[User ID]","[Analysts]","[User ID]=" & varUser ) = 0) Then



' count = 0 and User ID is *not* in table



Else



' Count <> 0 (ie =1) and User ID *is* in table



End If



You should also put this code in the Open or Load event of the form and use the timer event to close it
2008-12-24 10:02:39 UTC
This is going to be a long answer with lots of code but it works because I have a database that does the exact thing you are trying to do.



First,



You can try to fix your own code...looks like the DLookup function needs a few more symbols.



try this:::::::: [Forms]![Splash]![txtUser] = DLookUp("[User ID]","[Analysts]","[User ID]='" & [Forms]![Splash]![txtUser] & "'")



If that doesn't work or you want a better approach....read below at your own risk.







Place the following code in a new module.



Option Explicit

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _

"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long



Function getTheUser() 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

getTheUser = Left$(strUserName, lngLen - 1)

Else

getTheUser = vbNullString

End If

End Function



I decided to build a class that will do all of the dirty work to find access levels, names, and empID's whenever the class is referenced. It cleans up the code on your main form by locating it to a different module.



Create a new class called UserClass....then paste the code below.

I use the dlookup function to pull names and id's out of tables.



Option Explicit



Dim userFirst As String

Dim userLast As String

Dim userID As String

Dim PositionID As String



Sub getInfo(ByVal uname As String)



'set the userID as the passed variable uname

userID = uname



'for empID use DLookup

PositionID = DLookup("[ID]", "ExcelList", "[EmployeeM2] = '" & uname & "'")



End Sub



'Then you need to set some properties for your class so you can reference them later.



Public Property Get PosID() as String

PosID = PositionID

End Property



Now, in your mainform you can reference your class



'this should be declared at the top with the global declarations

Public newuser As UserClass



'this is the final piece of code that finds the username

'instantiate the class

Set newuser = New UserClass

'use the getTheUser function to return the windows logon id

'run the main class procedure to find all the required info.

newuser.getInfo (getTheUser)





then to find the right form just use your property.



If newuser.PosID = "someusername" then

docmd.openform "this form"

End If



Its not the most simple way to go about linking usernames up with forms but it runs clean and is easily reusable.
dougan
2016-10-18 14:01:25 UTC
one million. confident 2. no longer proper 3. some community admins disable activity scheduler. determine it is useful. each and every so often, you will possibly desire to click on the examine container, to easily run once you're logged on. 4. advise you get carry of and attempt "Macro Scheduler" unfastened trial get carry of from MJT internet. i take advantage of the pro version, that facilitates you to transform the bat report right into a .exe and time table the macros to run at distinctive situations. actually properly actually worth the money.
2008-12-26 05:41:31 UTC
sorry boss really i dont no


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...