Link to home
Start Free TrialLog in
Avatar of Karen Schaefer
Karen SchaeferFlag for United States of America

asked on

AddToCombo code from EE (Smart Combo Article) to Fill Multiple fields

I found the following code on EE - works great except I am unable to populate multiple fields with the code as is.

https://www.experts-exchange.com/Microsoft/Development/MS_Access/A_1787-Access-Tips-Tricks-The-Smart-Combo.html

Looking for assistance to modify the code to handle the updating of two fields on the adding of a new value from a combo box. (ie. lastname, firstname)  current only allows for populating 1 field with the entire value typed in the the combo that is to be updated.

Option Compare Database
Option Explicit

Public Function AddToCombo(addFormName As String, controlName As String, _
    NewData As String) As Integer

    ' Add a new record to a table by calling a form, and then
    ' requery the calling form. Designed to be called from
    ' NotInList event procedures
    '
    '   addFormName - the form to be opened to add a record
    '   controlName - the control on the add form that matches
    '       the displayed info in the calling combo box
    '   newData - the data as supplied by the calling combo box

    On Error GoTo HandleErr

    ' First, confirm the user really wants to enter a new record.
    If MsgBox("Add new value to List?", vbQuestion + vbYesNo, _
     "Warning") = vbNo Then
        AddToCombo = acDataErrContinue
        Exit Function
    End If

    ' Open up the data add form in dialog mode, feeding it
    ' the name of the control and data to use
    DoCmd.OpenForm FormName:=addFormName, _
     DataMode:=acAdd, _
     WindowMode:=acDialog, _
     OpenArgs:=controlName & ";" & NewData

    ' Before control returns to the calling form,
    ' tell it we've added the value
    AddToCombo = acDataErrAdded

ExitHere:
    Exit Function

HandleErr:
    MsgBox "Error " & Err.Number & ": " & Err.Description, , _
     "AddToCombo"
    Resume ExitHere
End Function

Public Function CheckOpenArgs(frm As Form)

    ' Designed to be called on loading a new form
    ' Checks the OpenArgs and if it finds a string of
    ' the form
    '   controlname;value
    ' loads that value into that control

    Dim controlName As String
    Dim controlValue As String
    Dim semiColonPosition As Integer

    On Error GoTo HandleErr

    If IsNull(frm.OpenArgs) Then
        Exit Function
    Else
        semiColonPosition = InStr(frm.OpenArgs, ";")
        
        ' Was there a semi-colon?
        If semiColonPosition > 0 Then
            controlName = Left(frm.OpenArgs, semiColonPosition - 1)
            controlValue = Mid(frm.OpenArgs, semiColonPosition + 1)
            ' Possibly this OpenArgs belongs to someone else
            ' and just looks like ours. Set the error handling
            ' to just ignore any errors on the next line.
            On Error Resume Next
            frm.Form(controlName) = controlValue
        End If
    End If
    
ExitHere:
    Exit Function
    
HandleErr:
    MsgBox "Error " & Err.Number & ": " & Err.Description, , _
        "CheckOpenArgs()"
    Resume ExitHere
End Function

Open in new window

Private Sub Form_Load()
   On Error GoTo Form_Load_Error
    
    CheckOpenArgs Me
    Me.DatasheetFontHeight = 9

   On Error GoTo 0
   Exit Sub

Form_Load_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of VBA Document Form_frmSchoolContacts_sub"
End Sub

Open in new window


As you can see here is my attempt - w/o success.
Private Sub ContactRecID_NotInList(NewData As String, Response As Integer)

Dim Lastname As String
Dim FirstName As String

    Lastname = Left(NewData, InStr(NewData, ",") - 1)
    FirstName = Right(Trim([NewData]), Len(Trim([NewData])) - InStr(1, [NewData], " "))

   Response = AddToCombo( _
     "frmContactInfo", "Lname", Lastname)

    Response = AddToCombo( _
     "frmContactInfo", "Fname", FirstName)

End Sub

Open in new window

Thanks,
K
Avatar of harfang
harfang
Flag of Switzerland image

This is funny: I just finished recommending this article in your previous question, https://www.experts-exchange.com/questions/27775773/Not-In-list-prevent-final-error-message-update-combo.html

I'll create a small demo based on that idea.

(°v°)
Here you go, I hope you will find it useful. I will add it to the article as a comment; it provides another example of the technique. Incidentally, there are vote buttons at the bottom of each article. Please click on “yes” if you found the article useful, it's often the only feedback an author gets!

Cheers!
(°v°)
Q-27776325.accdb
Avatar of Karen Schaefer

ASKER

Markus,

Thanks for you great solution, however, when I rename fields to match my database I have an issue with the actual Args to set the LastName -(fieldName: LName)

It breaks on the "    Me.LName = Trim(strArgs(0))"  

I get error msg - "Can't assign a value to this object"

Note: I tried it with & w/o the "Me" syntax - What am I missing?

Karen
Private Sub Form_Open(Cancel As Integer)
    
    Dim strArgs() As String
   
   On Error GoTo Form_Open_Error
       CheckOpenArgs Me

    If IsNull(Me.OpenArgs) Then
        Cancel = True
        Exit Sub
    End If
    strArgs = Split(Me.OpenArgs, ",")
    Me.LName = Trim(strArgs(0))
    If UBound(strArgs) Then FName = Trim(strArgs(1))

   On Error GoTo 0
   Exit Sub

Form_Open_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Open of VBA Document Form_frmContactInfo"

End Sub

Open in new window

I also tried adding the following to the Data entry Form for the Contacts - it works great in populating the fields with correct data, however, now I am back to the default error msg popping up again and the combo not being updated correctly.

User generated image
I added a temp field to the Contact form to hold the combo value, then I populate the FirstName, Lastname using the parsing code.
Private Sub Form_Load()
   On Error GoTo Form_Load_Error

    CheckOpenArgs Me
    
    If IsNull(Me.TxtName) Then
        Exit Sub
    Else
        Me.LName = Left(Me.TxtName, InStr(Me.TxtName, ",") - 1)
        Me.FName = Mid(Me.TxtName, InStr(Me.TxtName, ",") + 1)
        Me.Title.SetFocus
    End If
    Me.TxtName.Value = Null
   On Error GoTo 0
   Exit Sub

Form_Load_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of VBA Document Form_frmContactInfo"
End Sub

Open in new window


on Close of the contact form I am getting the message,  I know it has something to do with the way Access handles the dialog form.

Thanks
« when I rename fields to match my database... »

You need to do this only in cmdOK_Click:
    With CurrentDb("AAA").OpenRecordset     ' AAA = your table name
        .AddNew
        !BBB = txtLastName                  ' BBB = your last name field
        !CCC = txtFirstName                 ' CCC = your first name field
        .Update
        .Bookmark = .LastModified
        txtNewID = !DDD                     ' DDD = your ID field
    End With

Open in new window

The line you mention, “Me.txtLastName = Trim(strArgs(0))”, refers to a text box on the form, called txtLastName. You don't need to rename it, and your form shouldn't be based on a table at all, so Me.LName should throw a compile error.

You seem to be mixing two solutions... don't do that. I see for instance the line “CheckOpenArgs Me”, which comes from another solution, not mine. Please try again from the sample I said, creating from scratch instead of trying to incorporate this into what you have.

Good luck!
(°v°)
Thanks for you input, however, I am more confused then ever.  Would you be will to use a sample of my database to modify my existing forms to get it to work.  Keep in mind that it is not just contacts I will be add new data there are several combos that will need this functionality.

K
You will have to maintain your database, so it's important that you understand how it works. The ability to add a new record from the combo box is really only a “nice to have” feature, not an essential feature.

If you want, upload a database with one of your forms or even a test form similar to one of yours along with some minimal relevant data, and I will implement it on that form. Make sure to remove any sensitive information (and useless information as well, please) and to compact the sample database.

Cheers!
(°v°)
I appreciated any help you can offer.  Here is the file  I forgot to disable the auto launch of the school form - use hold+Shift.

Open form  = FrmSchoolInfo
Select a School Name
Click on the Contact Tab
Enter a new name:  Woodson, Woody
Click yes to allow addition and see what happens.

Also left in the Grant Tab.
Add new name to Grant = "Woodson Technical"
click yes to allow add.

Feel free to make any changes to the code to accomplish the task.

I REALLY APPRECIATED YOUR HELP ON THIS.  yes I mean to shout - I would love to shout it from the roof tops if I could.

Thanks
K
Sample.zip
ASKER CERTIFIED SOLUTION
Avatar of harfang
harfang
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANKS
wish i could give you a triple A+ for all your hard work.

THANK YOU, THANK YOU.

YOU WORKED WONDERS.

Karen
You are welcome; I'm glad I had a little free time to see this through. Thanks also for the comment in the article; it's almost embarrassing!

Success with your project!
(°v°)