Link to home
Start Free TrialLog in
Avatar of KPres
KPres

asked on

MS Access: Use NotInList with with a hidden key column

How do I use the OnNotInList event to add a record to a table underlying a combobox rowsource when the first column is a hidden column containing the key field?

The problem is that if the user types a value into the combobox, and I add that value to the underlying record, when access goes to check whether the value has been added it looks for the users entry (which is a string value) in the ID field.

I've tried  having the NotInList subroutine add the record to the underlying table, then changing the value of the newly created key, so that when vba runs the "response = acDataAdded" line,
it will check for the new key instead of the string the user typed, but that doesn't work for some reason.

I don't want to open a new form.  I just want to add the record behind the scenes.

Can anybody help?


Here's my code:

Private Sub cboCompanyID_NotInList(NewData As String, Response As Integer)

    Dim RS As Recordset

    Set RS = CurrentDb.OpenRecordset("tblCompanies", dbOpenDynaset)
    With RS
        .AddNew
        .Fields("Company") = NewData
        .Update
    End With
    cboCompanyID = RS.Fields("ID")
    Response = acDataErrAdded
   
Exit Sub
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

what is the structure of table tblCompanies? post the field names and data type
Avatar of KPres
KPres

ASKER

I'm sorry.  I should have explained better.  There are two tables at work.  The form is bound to a table called tblBidders, however the combobox uses tblCompanies as it's rowsource

tblCompanies
field1: ID......primary key (long integer, autonumber)
field2: Company....name of the company (string)
several other fields.....(not relevant)

tblBidders
field1: ID......primary key (long integer, autonumber)
field2: CompanyID.......foreign key (long integer)
several other fields.....(not relevant)

The combobox's rowsource displays [Company] but is bound to [ID] (hidden), both from tblCompanies.  The combobox's source is [ID] from tblBidders.

I hope this makes clear what I'm trying to accomplish
Avatar of KPres

ASKER

"The combobox's source is [ID] from tblBidders."

This should read "the combobox's source is [CompanyID] from tblBidders"

so what is the problem, are you getting any error?

ASKER CERTIFIED SOLUTION
Avatar of Helen Feddema
Helen Feddema
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