Link to home
Start Free TrialLog in
Avatar of shutterhacker
shutterhackerFlag for United States of America

asked on

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.
Avatar of shutterhacker
shutterhacker
Flag of United States of America image

ASKER

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
Avatar of Jeffrey Coachman
Not following, ...can you clearly explain what you mean by :

I need the rest of the form sync'd with it programmatically.
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.
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America 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
SOLUTION
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
mbizup and boag2000, sorry I am late coming back to this - had something popping up.

Both solutions worked. Thank you.
Glad we could help...
;-)