• NotInList (2K)

    Author
    Topic
    #448419

    Edited by HansV to break EXTREMELY long lines – please heed the warning about tags!

    Good Morning Everyone!

    Here’s my situation:

    I have a form that updates User’s information. The recordsource is tbl_User. tbl_User has the following fields:
    pkUserID–Primary Key
    UserName–Name of User
    UserID–User’s Login ID
    fkDeptID–Foreign Key to tbl_Dept–ComboBox for the department the user is in
    fkUserRightsID–Foreign Key to tbl_AccessRights –ComboBox for the access permission the user has
    Active–yes/no–is this user active or not

    If a user has admin rights, they have access to frm_UpdateUserInformation.

    The purpose of this form is to ADD, MODIFY, or UPDATE user information. On the form I have a combo box which list’s the User’s by Name. If the ADMIN person is adding a NEW USER, I have the following code in the NotInList:

    Dim db As Database
    Dim sName As String
    Dim rs As Object
    
    Set db = CurrentDb
    
    'Ask the user if they want to add to the list
    If MsgBox("Are you sure you want to add this person?", vbYesNo + vbQuestion, _
      "Add new value?") = vbYes Then
    
    'The user clicked Yes - add the new value
    db.Execute "INSERT INTO tbl_User(UserName) VALUES (""" & NewData & """)", dbFailOnError
    
    'Tell Access you've added the new value
    Response = acDataErrAdded
    
    Else
    
    'The user clicked No - discard the new value
    Me.cboAddUser.Undo
    'Tell Access you've discarded the new value
    Response = acDataErrContinue
    
    End If
    
    db.Close
    Set db = Nothing
    Me.cboAddUser.Requery
    

    OK…so here are my problems

    1. The code to add the user works just fine. However, as the NEW USER is added, the ADMIN person won’t know what the NEW USER’S LOGINID will be, and shouldn’t have to ask. So I’m using the Global Address List and more code to locate the user and UPDATE the LOGINID field with the NEW USER’S ACCOUNT (which is their LoginID).
    2. I want the msg to the ADMIN person to include the NEW USER’S name they just typed in. For instance, in the combo box, the ADMIN person typed “Roberta Price”, I want the message to read: “Are you sure you want to add ROBERTA PRICE?”

    The following code is what I’m using to locate the NEW USER’S loginID

     Public Sub UpdateLoginID()
    Dim FName, LName, UserName, sLoginID As String
    
    UserName = Me.cboAddUser.Column(1)
    FName = Left([UserName], InStr(1, [UserName], " ") - 1)
    LName = IIf(InStr(InStr([UserName], " ") + 1, [UserName], " ")  0, Right([UserName], _
      Len([UserName]) - InStr(InStr([UserName], " ") + 1, "[UserName]", " ")), _
      Right([UserName], Len([UserName]) - InStr([UserName], " ")))
    
    'Locate the users login id based on first and last name
    Me.UserID = DLookup("[Account]", "[Global Address List]", "[First] = '" & _
      FName & "' and [Last] = '" & LName & "'")

    However, I keep running into different problems. 1) Since the user hasn’t been added to the list until the cbo has been exited, UpdateLoginID() won’t fire. If I add code to the AfterUpdate event of the cbo to hold the name, the NotInList works, but not the UpdateLoginID(). The system keeps telling me to SAVE, I do, and still nothing. I’ve tried requerying the cbo and still nothing.

    Can someone please provide me insight as to how to achieve what I’m attempting to do?

    Thanks in advance.

    Viewing 0 reply threads
    Author
    Replies
    • #1096488

      Good Afternoon!

      About the text of the message box: you can use NewData, just like you do in the SQL statement:

      If MsgBox(“Are you sure you want to add ” & NewData & “?”, vbYesNo + vbQuestion, …

      About the login ID: you could set it in the Before Update event of the form (if it is still empty). Or you could set it in the SQL statement that you execute in the On Not In List event.

      • #1096505

        Thanks Hans,
        adding NEWDATA worked just fine, but adding the code to the BeforeUpdate didn’t. Msgbox keeps asking me to add the new user but never adds the LoginID.

        I don’t know how to add the ” Call UpdateLoginID()” to the SQL statement. I would greatly appreciate it if you could lend your advice.

        Thanks again.

        • #1096514

          See if this code for the On Not In List event works:

          Dim db As Database
          Dim FName As String
          Dim LName As String
          Dim UserName As String
          Dim sLoginID As String
          Dim strSQL As String
          Dim intPos As Integer

          ' Ask the user if they want to add to the list
          If MsgBox("Are you sure you want to add this person?", _
          vbYesNo + vbQuestion, "Add new value?") = vbYes Then

          ' The user clicked Yes - add the new value

          ' Extract first and last name
          intPos = InStr(NewData, " ")
          FName = Left(NewData, intPos - 1)
          intPos = InStrRev(NewData, " ")
          LName = Mid(NewData, intPos + 1)

          ' Locate the users login id based on first and last name
          sLoginID = DLookup("[Account]", "[Global Address List]", _
          "[First] = '" & FName & "' and [Last] = '" & LName & "'")

          strSQL = "INSERT INTO tbl_User(UserName, UserID) VALUES ('" & _
          NewData & "','" & sLoginID & "')"
          Set db = CurrentDb
          db.Execute strSQL, dbFailOnError
          Set db = Nothing

          ' Tell Access you've added the new value
          Response = acDataErrAdded

          Else

          ' The user clicked No - discard the new value
          Me.cboAddUser.Undo
          ' Tell Access you've discarded the new value
          Response = acDataErrContinue

          End If

          • #1096517

            WONDERFUL!!!!!! WONDERFUL!!!!!

            Thank you so very very much!!!!

    Viewing 0 reply threads
    Reply To: NotInList (2K)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: