Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: User Logon ID

  1. #1
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139

    Unanswered: User Logon ID

    Hello;

    There are many previous posts on this subject, however I was unable to find one that gives instructions to beginners like me. I am using the following code from a post on this forum (in fact have tried a few including the Dev Ashis (sp?) one also) but have been unable to get them working. The one listed below returns no errors but returns no ID either. I know I don't have it set up right and that's what I need help with. Could someone let me know where to put the code. Right now when I put the code in the OnLoad function of the form, I get an error (I figure because when I set the form OnLoad properties to "Event Procedure" it starts a new Private Sub. When I leave the Event Procedure out I get no errors. I have an unbound text box to display the ID and the default value of the text box "=GetUser()".

    Thanks...
    Larry

    Option Compare Database
    Option Explicit

    Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
    (ByVal lpBuffer As String, nSize As Long) As Long
    Public Function GetUser() As String

    On Error GoTo ErrorRoutineErr

    Dim st As String
    Dim slCnt As Long
    Dim slDL As Long
    Dim slPos As Single
    Dim slUserName As String
    Dim txtUsername As String

    'Get the users name
    slCnt = 199
    st = String(200, 0)
    slDL = GetUserName(st, slCnt)
    slUserName = Left(st, slCnt) & slCnt
    slPos = InStr(1, slUserName, Chr(0))
    If slPos > 0 Then
    txtUsername = Left(slUserName, slPos - 1)
    GetUser = txtUsername
    Else
    GetUser = ""
    End If

    ErrorRoutineResume:
    Exit Function
    ErrorRoutineErr:
    MsgBox "Stats.Form1.UserName" & Err & Error()
    GetUser = Str(Err)
    Resume ErrorRoutineResume
    End Function

  2. #2
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Look at your conditional and what it's testing for... Basically if there's a Null character you strip it out and return the resulting name else you return an empty name ...

    Can I suggest a rethink of your logic ...
    Back to Access ... ADO is not the way to go for speed ...

  3. #3
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139
    Quote Originally Posted by M Owen
    Look at your conditional and what it's testing for... Basically if there's a Null character you strip it out and return the resulting name else you return an empty name ...

    Can I suggest a rethink of your logic ...
    Thanks for the reply... its not my logic, I copied this code off the forum. I assumed it would work. I do not know VBA. I am simply looking for some help on setting up some security on my DB. I think the point of my post was 1) get the Login password thing working for me, and 2) advise that there may be others out there that would benefit from a complete soup to nuts approach on setting up security on their DB's. For someone who does not know VBA, the code available in the forum is not clear as to how to implement such if you are not an experienced Access developer or know VBA. There are those like me that can certainly copy code but if one does not know where to put it, and how to call it, it is of little use. Now I guess one could say if you need to copy code you have no business developing a DB, however someday I would hope to be proficient in Access, but for now I have to learn somehow. This forum has taught me a lot and I am grateful, however some of the replies are better suited for the experienced user, not the learner. Although the replies are suited for the poster, as has been said many times, the reply also benefits others.

    Please don’t interpret this as a flame, just some thoughts of I’m sure others like myself who are trying desperately to learn from experienced users like those that reply often. They give generously and it is greatly appreciated! I have seen many posts and replies on the subject of Logon user ID but I have found none so far that provide the code, and advise how to get it to work. Since there were many post on the subject I assume many could benefit from a soup to nuts reply of how to set up security, and such a reply could be referenced in future posts/replies.

    Thank you in advance for any help…

    Larry

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    mike's answer is 100%
    here is additional comment (but still not a fix on a silver platter)

    'make slPos the position of the first null character in slUserName, or zero if not there
    slPos = InStr(1, slUserName, Chr(0))

    'if a null character was found
    If slPos > 0 Then

    'make txtUserName everything up to the first null character
    txtUsername = Left(slUserName, slPos - 1)

    'send that value back to whoever called the routine
    GetUser = txtUsername

    'else if there were zero null characters
    Else

    'send an empty username back to whoever called the routine
    GetUser = ""

    '(?why would you want to do this?)
    '(we already know slUserName contains zero null characters or we would not have got to this point),
    'if slUserName contains zero characters then is is already "" so the above line just wastes a couple of clock cycles to do nothing.
    'if slUserName contains one or more characters, odds-on it's the user name
    'maybe the calling routine would like to know the value of slUserName


    izy

    AFTERTHOUGHT
    use Left$() which is almost twice as fast as Left() when playing with text
    Last edited by izyrider; 02-19-06 at 12:46.
    currently using SS 2008R2

  5. #5
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by Larryg
    Thanks for the reply... its not my logic, I copied this code off the forum. I assumed it would work. I do not know VBA. I am simply looking for some help on setting up some security on my DB. I think the point of my post was 1) get the Login password thing working for me, and 2) advise that there may be others out there that would benefit from a complete soup to nuts approach on setting up security on their DB's. For someone who does not know VBA, the code available in the forum is not clear as to how to implement such if you are not an experienced Access developer or know VBA. There are those like me that can certainly copy code but if one does not know where to put it, and how to call it, it is of little use. Now I guess one could say if you need to copy code you have no business developing a DB, however someday I would hope to be proficient in Access, but for now I have to learn somehow. This forum has taught me a lot and I am grateful, however some of the replies are better suited for the experienced user, not the learner. Although the replies are suited for the poster, as has been said many times, the reply also benefits others.

    Please don’t interpret this as a flame, just some thoughts of I’m sure others like myself who are trying desperately to learn from experienced users like those that reply often. They give generously and it is greatly appreciated! I have seen many posts and replies on the subject of Logon user ID but I have found none so far that provide the code, and advise how to get it to work. Since there were many post on the subject I assume many could benefit from a soup to nuts reply of how to set up security, and such a reply could be referenced in future posts/replies.

    Thank you in advance for any help…

    Larry
    Larry,

    Your point of view I perfectly understand ... I was there once myself (LONG AGO ...). However, you asked what was wrong with the code (or at least why it wasn't working ...) which both Izy and myself answered. As for your implementation issue: How do you want the user to have to login?

    You can set users and groups in the security area and then each user MUST login ...

    You can create your own login prompt with backing security checking (mostly VBA) ...

    So, (ball back in your court) what do you want to do?

    P.S. - There is no "right" way ...

    P.P.S - I do both. Use the built in accounts for the ID and then use my own custom security protocol.
    Back to Access ... ADO is not the way to go for speed ...

  6. #6
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139
    Hello... and thank you for replying;

    Ideally what I would like, based on the majority of the recommendations in the forum, is to have the API function working. However for someone of my experience level that seemed way to involved based on the documentation available. I kept looking and found something I thought I could get to work (the posted code). When I received replies stating the code would not work as listed, I thought it doesn't look like I will get any of it to work.

    Some of the posts I have seen state to put the code in a Module. I tried that and nothing happened. But that is part of the problem I was trying to get across in my posts (what I would like to see thread). For most, telling them to put the code in a module assumes they know what to do at that point. Perhaps there is nothing to do, however I'll bet that is not the case. Its sort of like developing code... leave one comma out, or add one space where there shouldn't be one and all code just returns some ugly error. My suggestion to add detail for the sake of those like me, trying desperately to learn from the experienced developers, would be greatly appreciated. I know that perhaps I am not doing a great job using the written word to explain my situation and the situations of others in my position, but all one has to do is look at the number of views on some of the issues, and recall the number of times the responders address the same issues over and over again.

    I for one don’t need anyone to do my work for me, but I would like some help with the overall concept. I want to learn to fish, not be handed a fish. Take the security issue for instance. I would like to see a base document listing the code, an explanation of where to put the code (module, form, control, etc), and an explanation of how to call the code to function (develop a form with a text box bound or unbound, etc). Now I know that may seem to some as requesting the fish, however once I get a piece of code working and see and work with setting it up following instructions, that to me is learning. I would not need to seek assistance on many aspects similar in nature from that point. I would not expect someone to sit down and answer every question in this fashion, hence my suggestion of a base document. A base document would only need to be written once and could be referred to over and over again, and each instance would be complete with needed instructions - IF needed. Had something like this existed presently, we would not be having this conversation. Perhaps its just me, but partial explanations only raise more questions. I understand there are many ways of addressing a situation and that is why there are so many different code examples, but if there is one that works, at least it could get someone going and they could tweak it later.

    At this point I got off topic from my original request. If someone wants to address it great, but at this point I am offering what I think would make this forum better. Teddy asked, and given the help I have received from this forum, this was the only way I could give back if you will. All is not gloom and doom. This forum does good things! My comments were only meant to improve the generosity of those that give. Someday I hope to be able to return the help that I have been given. Six months ago I couldn’t even spell Access as the saying goes, however with the help of education from many generous people I have a working database. I am at the tweaking stage, but I’m sure there is much more that I could, and want, to learn.

    Thank you...

  7. #7
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by Larryg
    Hello... and thank you for replying;

    Ideally what I would like, based on the majority of the recommendations in the forum, is to have the API function working. However for someone of my experience level that seemed way to involved based on the documentation available. I kept looking and found something I thought I could get to work (the posted code). When I received replies stating the code would not work as listed, I thought it doesn't look like I will get any of it to work.

    Some of the posts I have seen state to put the code in a Module. I tried that and nothing happened. But that is part of the problem I was trying to get across in my posts (what I would like to see thread). For most, telling them to put the code in a module assumes they know what to do at that point. Perhaps there is nothing to do, however I'll bet that is not the case. Its sort of like developing code... leave one comma out, or add one space where there shouldn't be one and all code just returns some ugly error. My suggestion to add detail for the sake of those like me, trying desperately to learn from the experienced developers, would be greatly appreciated. I know that perhaps I am not doing a great job using the written word to explain my situation and the situations of others in my position, but all one has to do is look at the number of views on some of the issues, and recall the number of times the responders address the same issues over and over again.

    I for one don’t need anyone to do my work for me, but I would like some help with the overall concept. I want to learn to fish, not be handed a fish. Take the security issue for instance. I would like to see a base document listing the code, an explanation of where to put the code (module, form, control, etc), and an explanation of how to call the code to function (develop a form with a text box bound or unbound, etc). Now I know that may seem to some as requesting the fish, however once I get a piece of code working and see and work with setting it up following instructions, that to me is learning. I would not need to seek assistance on many aspects similar in nature from that point. I would not expect someone to sit down and answer every question in this fashion, hence my suggestion of a base document. A base document would only need to be written once and could be referred to over and over again, and each instance would be complete with needed instructions - IF needed. Had something like this existed presently, we would not be having this conversation. Perhaps its just me, but partial explanations only raise more questions. I understand there are many ways of addressing a situation and that is why there are so many different code examples, but if there is one that works, at least it could get someone going and they could tweak it later.

    At this point I got off topic from my original request. If someone wants to address it great, but at this point I am offering what I think would make this forum better. Teddy asked, and given the help I have received from this forum, this was the only way I could give back if you will. All is not gloom and doom. This forum does good things! My comments were only meant to improve the generosity of those that give. Someday I hope to be able to return the help that I have been given. Six months ago I couldn’t even spell Access as the saying goes, however with the help of education from many generous people I have a working database. I am at the tweaking stage, but I’m sure there is much more that I could, and want, to learn.

    Thank you...
    Well to simplify this a bit for you Larry ... The posted code CAN BE called from the Load event of a "startup" form (one that is run upon initial entry into Access) or even the Open event of the form OR even in the timer event of the form (if you wanted to have a slight delay before asking for the login ID) ...

    It comes down to HOW you want it to work ... Determine that, then ask where to put it for how you want it ...
    Back to Access ... ADO is not the way to go for speed ...

  8. #8
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139
    Thanks to M Owen and those that have replied...

    Just a follow up... I abandoned the previously posted code in favor of the attached method. After some trial and error I did get this method transferred over to my database and got it working. I believe I found this on this forum however I have been struggling with this for some time now and have been to so many places I forget exactly where I got it from. This seems to work pretty well, and as far as my inexperienced eyes can see it uses the XP logon id as many of the experienced users here recommend. Perhaps someone can take a peek at it and see if there are any issues with it I (and others) should be aware of.

    Additionally, a point in reference to my previous posts about not knowing how and where to place the code and how to call it, and the importance of helpful instructions. Until I started into converting this to work in my database I had seen posts that instructed me to place the code in a Module. I did that with a previous piece of code and that appeared to do nothing. Upon adapting the new code and functions I see now that there are different kinds of Modules. I have no clue what the difference is but I do know that simply placing some code in a “Module” just doesn’t get the job done. I copied the two pieces of code into Modules and could not get it to work, it wasn’t till I copied over all the modules that I got it to work. I assume that some Modules are used at startup and others are not used until called upon?

    The sample database containing all the necessary code and examples allowed me to convert it into what I needed to suite my individuals needs. This sample database is something I would recommend for a reference for those wanting to set up some security in their own database. Now its probably not sophisticated enough for the experienced user, however it suited my needs with a little tweaking. I can also see some potential for further tweaking down the road. This sample was 1k better as a tool for learning than all the books I have seen.

    So again... thanks to all those that responded and I hope I have been able to successfully convey my thoughts about the kind of help some of us beginners could use.

    Larry
    Attached Files Attached Files

  9. #9
    Join Date
    Aug 2002
    Location
    Northampton, England
    Posts
    266
    Try this function. I know it works as I have just tried it.
    Copy and paste it into a module. Place an unbound text box on a form and in the text box put =GetNTUser()

    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


    Public Function GetNTUser() As String
    Dim strUserName As String
    'Create a buffer
    strUserName = String(100, Chr$(0))
    'Get user name
    GetUserName strUserName, 100
    'Strip the rest of the buffer
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    GetNTUser = strUserName
    End Function

    You could also try this function.

    Public Function GetUserName() As String
    Dim wshNet As Object
    Set wshNet = CreateObject("WScript.Network")
    GetUserName = wshNet.username
    Set wshNet = Nothing
    End Function
    Again, put this line in an unbound text box =GetUserName()

    You will need to set a reference to: Windows Script Host Object Model for this function.

    David

  10. #10
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by Larryg
    ...I believe I found this on this forum however I have been struggling with this for some time now and have been to so many places I forget exactly where I got it from.
    .....yup you sure did, I'm pretty certain its a snippet posted by PKStormy.
    I'd rather be riding on the Tiger 800 or the Norton

  11. #11
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    now we're having fun! here's another sort of module to add to the mix.

    attached class late-binds to WSH (sidestepping DJN's reference issue) and produces user login, domain, computer.

    import the class into your db.
    from your db:
    File/GetExternalData/Import
    (locate attached file)
    ...Modules
    ...izyUserClass
    ...OK

    use as follows:

    dim strLogin as string
    dim strDomain as string
    dim strComputer as string
    dim myUser as new izyUserClass
    with myUser
    strLogin = .login
    strDomain = .domain
    strComputer = .computer
    end with
    set myUser = nothing


    no need to reference scripting because of the late bind, but you need scripting on the target machine.


    izy
    Attached Files Attached Files
    currently using SS 2008R2

  12. #12
    Join Date
    Nov 2004
    Location
    Bangor, ME USA
    Posts
    44
    Larry;

    how is your DB set up? is it on 1 machine w/ 1 user at a time or is it on a network as a front end (Forms/Reports/Queries/Code Modules/Macros) and Back end (Data tables only) Front end links to the tables in the back end [in this way if your forms are corrupted, your data is still there because it was never really in with your forms]

    1 user vs multi users at the same time has an effect too.

    Access security is OK, (I use it myself, for now - I am trying to find ways to do the same in a SQL server environment now for my little learning project) for setting up users and groups and for permissions (who can do what to which tables [i.e. a sales team member has no business in an R & D table])

    The security wizard can also help you.

    See my problem was a while back, I was asking the same question you are now because I have about 20 people right now (we want to make available to 800+ hence SQL and PHP over INTRAnet) simultanious users and when I work at night I can not do updates if someone is in the database. so I set up a table which lists:
    a. the database user name (the one you created for a user in Access security (permissions also assigned in security - every user has to be able to read/write/update this table).
    b. the network User Name (set up by the informations Systems people for a NETWORK logon (usually not the sane as db name but you can make it that wy if you wanted).
    c. their computer name (if set up).
    d. the network domain (my organization has more than one).
    and e: the time of #12:00 AM# (in code, a time or date is surrounded by # signs)

    Then when the database is started it runs the auto exec macro Maximizes the database window, runs the code for my database open procedure, and maximizes again) [i found without the 2nd maximize my form is in a window instead of full screen.

    My Autoexec macro looks like
    Maximize
    Runcode >>> Function Name >> =dbopencode() [this is how you call the function.

    when loking for my answer, someone suggested the environ function to get the information'

    In your textbox try thiss for a datasource (not sure if it will work but what the hell try anyway) >>> Environ("Username")

    that will give you the network (XP login name if not on a network) of the user. If you want the name you assigned try =currentuser (this works in a module but not really sure if it will work straight as a datasource for a textbax like you want.

    Like you Larry, I am learning. I have worked on access 97 - 2003 but only in the last couple years have been learning VBA coding. It is good to know.

    HTH in your project

    Kevin M Carman

    PS if you wopuld like my table and open and close code I will be happy to provide it for you - The close code requires a Command button to be clicked as the red X does not case the close code to execute [anyone that can help on this point would be helpful to me as I have a few idiots that can not click a Large Grey button with red Close && Exit written on it to properly close my database (for a true list of who is still on)] Thx

  13. #13
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by Kmcarman72
    PS if you wopuld like my table and open and close code I will be happy to provide it for you - The close code requires a Command button to be clicked as the red X does not case the close code to execute [anyone that can help on this point would be helpful to me as I have a few idiots that can not click a Large Grey button with red Close && Exit written on it to properly close my database (for a true list of who is still on)] Thx
    That's because the "Escape" menu button (small X in right corner of the form) invokes the OnClose event of the form (the Form_Close proc) ... So, to get your closing code to run from either button, you now know where it has to be called/run from ...
    Back to Access ... ADO is not the way to go for speed ...

  14. #14
    Join Date
    Aug 2005
    Location
    Delaware
    Posts
    139
    Quote Originally Posted by Kmcarman72
    Larry;

    how is your DB set up? is it on 1 machine w/ 1 user at a time or is it on a network as a front end (Forms/Reports/Queries/Code Modules/Macros) and Back end (Data tables only) Front end links to the tables in the back end [in this way if your forms are corrupted, your data is still there because it was never really in with your forms]


    Like you Larry, I am learning. I have worked on access 97 - 2003 but only in the last couple years have been learning VBA coding. It is good to know.

    HTH in your project

    Kevin M Carman

    Thx
    Hello Kevin...

    I have 11 users right now and don't antisipate many more if any. The security code I posted is working great. I do use a front end and a back end design. I also use a "test" version for making updates, improvements and other while the users work on a "production" version. I also use the windows logon info in my queries so that the users can only print reports for themselves, not others. Security is not a real big problem but the fact that I could use their windows logon to select reports was a huge help.

    As I see and work with more code in the form of a working example I find that is helping me a greaqt deal in understanding and learning VBA. I hope to be self sufficient at some point. However that is a ways off yet.

    Larry

  15. #15
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by Larryg
    ......As I see and work with more code in the form of a working example I find that is helping me a greaqt deal in understanding and learning VBA. I hope to be self sufficient at some point....Larry
    ...and hopefuly contribute some of that knowledge back to this forum
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •