Results 1 to 11 of 11
  1. #1
    Join Date
    May 2006
    Posts
    178

    Unanswered: how do I save the user name in a table?

    Hey guys,

    Ive taken your advice and used the getuser() function. Now my form displays the user name which is what i wanted...now the next step is something i need your help on.......

    I want to save the user name in a user table ONLY if it doesnt already exists, can anyone help me with this please.


    Aboo

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Code:
    INSERT INTO myTable (userName) 
    VALUES (getuser()) 
    WHERE NOT EXISTS (SELECT NULL FROM myTable WHERE userName = getuser())
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    May 2006
    Posts
    178
    Thanks pootle

    i take it that

    in

    INSERT INTO myTable (userName)

    the (username) is the field

  4. #4
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Also I think in one of the examples in the code bank, there's some code which checks to see if the LoginID (getuser()) exists in the table and if it doesn't, prompts to enter that user in the new table (along with allowing the user to enter their name, etc.)

    I think I put this type of code in a few of the MSAccess games I designed in the code bank downloads. (I think the Dice game.) Otherwise check the getuser download in the code bank. I might have put it in that.

    Usually you also would want to have the new recognized user enter their name, department, etc.. when you're checking to see if that user exists and they don't and you want to add them to your SysUsers type table. (If the new recognized user isn't in the SysUser type table, I usually open a form whereby the default value of the LoginID = getuser() and the form allows them to enter their other information.)
    Last edited by pkstormy; 02-28-09 at 22:22.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  5. #5
    Join Date
    May 2006
    Posts
    178
    PK,

    I think im going blind.. i cant see it..


    Aboo

  6. #6
    Join Date
    May 2006
    Posts
    178
    is it this?

    Code:
    Private Sub Form_Open(Cancel As Integer)
    DoCmd.Maximize
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    Dim strSQL As String
    strSQL = "Select * from WhoTried where LoginID = '" & GetUser() & "'"
    rs.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    If rs.EOF And rs.BOF Then
        rs.AddNew
        rs!LoginID = GetUser()
        rs.Update
        rs.Close
        Set rs = Nothing
    Else
        rs!TimesTried = rs!TimesTried + 1
        rs.Update
        rs.Close
        Set rs = Nothing
    End If
    DoCmd.OpenForm "MainForm"
    End Sub

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    looks like it
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    May 2006
    Posts
    178
    lol....

    can someone please translate...lol i know basic levels... but im very weak..


    Thanks

    Aboo

  9. #9
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Code:
    Private Sub Form_Open(Cancel As Integer)
    DoCmd.Maximize 'maximise the form
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    Dim strSQL As String
    'find if this userid has logged in before in the 'whotried' table
    strSQL = "Select * from WhoTried where LoginID = '" & GetUser() & "'"
    rs.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    If rs.EOF And rs.BOF Then 'we didn't find a record, so
        rs.AddNew 'add one
        rs!LoginID = GetUser()
        rs.Update
        rs.Close
        Set rs = Nothing
    Else 'otherwise we found the user so add one to the number of times they logged on
        rs!TimesTried = rs!TimesTried + 1
        rs.Update
        rs.Close
        Set rs = Nothing
    End If
    'having completed that move to the mainform
    DoCmd.OpenForm "MainForm"
    End Sub
    I'd rather be riding on the Tiger 800 or the Norton

  10. #10
    Join Date
    May 2006
    Posts
    178
    Hey Healdem,

    Thanks for your explaination.

    I dont want to count the number of times a person logs in. all iw ant to do is check if this ID exists, if it doesnt then just add it to the table.

    how would i amend this code to accomodate that? i really struggle with coding..


    Thanks

    Aboo

  11. #11
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    personally I still don't understand why you feel the need to validate the userid.. that should be handled by your network trolls security... IE how they restrict access to the application to specified users or usergroups.

    so
    look at the code above and try to understand what it is doing
    having done that try to work out if there is anything there that is relevant to your problem.

    ie that fragment of code attempts to find if a user is already defined in the system, if they are it adds one to the a counter, if not it adds a record

    make an effort to apply it to your requirements

    if you hit a problem then by all means come back, but at least make an effort, thats the only way you are going to learn from this
    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
  •