Access unbound combobox sync with other form field after adding a record

I am building very simple database for inventory computer equipment. The data entry form has an unbound combo PCname for navigation. When a name is selected, its other fields (Make, Model etc) are sync'd. Works fine.

Now I want the form double for new record adding. i.e. if the user enters a name not on the list, the NotInList event fires and the user is asked if he wants to add the name. If so, I used acdataerradded response and docmd.sql to insert a new record to the "computers" table.

After that I want the form show the new record so the user can populate the other fields. But so far no luck. The rest of the form fails to sync with the new name in the combo. It works only if 1) I hit SHIFT F9 to requery manually. or 2) close and re-open the form.

I tried to put me.dirty = false and me!PCname.requery everywhere it fails to work. If I put the requery action in notinlist event it gives a 2118 error (you must save before you can requery).

I searched the web and found several people with the same problem w/o solution (most responders suggested adding dirty=false and/or requery, which apparantly won't work.

Note: AcDataErrAdded does make the new name onto the combo list but I need the rest of the form sync'd with it programmatically.
shutterhackerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shutterhackerAuthor Commented:
Code snippets:
----------------------------------------
Private Sub Form_Current()
Dim rs As Object
Set rs = Me.Recordset.Clone

If Not IsNull(Me!PCName) Then rs.FindFirst "PCID = " & Me!PCName
If Not rs.EOF Then
  Me.PCName = Me!PCID
End If
End Sub
----------------------------------------------
Private Sub Form_Load()
Me!PCName = PCName.ItemData(0)
End Sub

Private Sub PCName_AfterUpdate()
'Me!PCName.RowSource = "select pcid, pcname from computers order by pcname"
'Me!PCName.Requery

If IsNull(Me!PCName) Then Exit Sub

With Me.RecordsetClone
  .FindFirst "PCID = " & Me!PCName
  If Not .NoMatch Then
    If Me.Dirty Then Me.Dirty = False
    Me.Bookmark = .Bookmark
  End If
End With
End Sub
---------------------------------------------------
Private Sub PCName_NotInList(NewData As String, Response As Integer)

DoCmd.OpenForm "frmYesNo", , , , , acDialog, "This computer name does not exist. Create it?"
  If YesNo = 1 Then
    Response = acDataErrAdded
    DoCmd.RunSQL "insert into computers (pcname) values ('" & NewData & "')"
  Else
    Me!PCName = Null
    Exit Sub
  End If
End Sub
0
Jeffrey CoachmanMIS LiasonCommented:
Not following, ...can you clearly explain what you mean by :

I need the rest of the form sync'd with it programmatically.
0
shutterhackerAuthor Commented:
Hi boag2000,

Let's make it simple. I have a table Computers with fields PCID (autonumber as PK), PCName (text), Make (text, Dell HP etc).

I made a form with form record source being table Computers. The unbound combo has row source "select PCID, PCname from computers", bound column 1, widths 0",1". When the user picks a name from the combo, the Make of the selected PC is shown on the form for viewing/editing. It works perfectly if the user does not add a new record to the table.

When user enters a name not on the list into the combo, the NotInList event fires and asks the user if he wants to create a new record, if he clicks Yes, the new record with PCname field taken from NewData was inserted into the Computers table by docmd.runSQL. At this point I wish this new record's Make field (which is blank at this point) is on the form for the user to populate. But I cannot make the "Make" field sync'd with the newly added PCname item. Only way to get the underlying table requeried is by hitting SHIFT + F9. I wish this can be done by VBA.

The new PCName is added to the combo box list. The key issue is that the form's record source is NOT updated so if you navigate records by Pgup/Pgdn keys or scrolling mouse, or simply picking up the new PCName from the list, the Make field on the form stays at the value of an old record.


Thanks.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jeffrey CoachmanMIS LiasonCommented:
<Let's make it simple.>
That's what I like... nice and simple...

OK, now I understand.

You need a Requery command at the end of your code that adds the record.

FWIW, here is what I use
Access--Basic--Simple-ComboBoxNo.mdb
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbizupCommented:
If you want your combo box and form to be truly synchronized... ie: your unbound combo box and your data showing exactly the same record... use the recordsetclone in your not in list event for data additions instead of the RunSQL method (which does not immediately affect the data in the form).

The benefit of this method is that you don't have to requery the form or the combo, which will cause the selection in the unbound combo to go blank.

Private Sub PCName_NotInList(NewData As String, Response As Integer)
'DoCmd.OpenForm "frmYesNo", , , , , acDialog, "This computer name does not exist. Create it?"
YesNo = MsgBox("This computer name does not exist. Create it?", vbYesNo)
  If YesNo = vbYes Then
    Response = acDataErrAdded
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
    rs.AddNew
    rs!PCName = NewData
    rs.Update

    ' Use the above recordsetclone method instead of this action query.
    'DoCmd.RunSQL "insert into computers (pcname) values ('" & NewData & "')"
    
  Else
    Me!PCName = Null
    Exit Sub
  End If
End Sub

Open in new window

0
shutterhackerAuthor Commented:
mbizup and boag2000, sorry I am late coming back to this - had something popping up.

Both solutions worked. Thank you.
0
Jeffrey CoachmanMIS LiasonCommented:
Glad we could help...
;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.