How to get my current user logon in MS Access forms

假装没事ソ 提交于 2019-12-25 02:28:25

问题


I have initial form logon to Validate user details to access my MS Access database.

Now I have a requirement in another form, If I choose same logon user from a combo box then command button should be enabled in the form so how I validate my initial logon user who logged in with another form Textbox in my access db.

So I researched about this but could not get the useful scenario - Can some please help me how I can acheive this details ..Thanks!


回答1:


After the user completes her login, hide rather than close the login form. With the form hidden, code in your other form can still read values from the login form.

To hide a form named frmLogin:

DoCmd.OpenForm "frmLogin",acNormal,,,,acHidden

or

DoCmd.OpenForm "frmLogin",acNormal,WindowMode:=acHidden

If the hidden frmLogin includes a text box named txtUserName, you can inspect its value like this:

Debug.Print Forms!frmLogin!txtUserName

I based this answer on the assumption you created your own security framework in your Access database application. But that point is unclear. If you're using Access ULS (user level security), which requires the older MDB format database, the CurrentUser() function which Matt mentioned will give you the user's Access security name. However, if that is the approach you're using, I don't understand why you would need a separate login form.

So your question is unclear. If none of the answers is satisfactory, please edit your question to include an explanation of the security/login strategy you're using.




回答2:


Whilst the API approach @Remou suggests works, I have moved to this technique

Public Function GetUserName() As String
    Dim wshNet As Object
    Set wshNet = CreateObject("WScript.Network")
    GetUserName = wshNet.UserName
    Set wshNet = Nothing
End Function

I believe it to be more robust than calling an API




回答3:


Record the logged on user in a public variable once validated and then you can test this against the "CurrentUser()" command to see if they match on next form.

NOTE: Only use this method if each user has their own copy of the front-end (do not use if everyone is sharing the same front-end, although with good practice they shouldn't be)



来源:https://stackoverflow.com/questions/8959082/how-to-get-my-current-user-logon-in-ms-access-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!