PDA

View Full Version : Adding a record to a combobox record source



mud2
09-23-2008, 07:43 PM
Now a new one. Ive solved this before, but can't remember how!;
I add a name into a combobox that isn't in its record source (table). This triggers a "notInList". I then pick up that name, construct an SQL and INSERT it into the record source. Fine, so far. BUT the combo box can't find this new value until I shut down and restart! I've requeried all over the place...no luck!
By the way, ACCESS'S Help suggests just adding the new name to the recordsource, but unfortunately ACCESS, knowing all, insists on inserting a semicolon at the end of ITS record source, and adding a new name results in an error..."...added after end..." I suppoe I could treat the recordsource as a string, use the MID function to remove the trailing semicolon...etc...but I'd rather update the underlying table.

nepotist
09-24-2008, 04:48 AM
You the new record to be shown so that you could add one of its field in to the combobox right...
Once you add the record
use the
docmd.gotorecord
command and then run to that should help you..

mud2
09-24-2008, 08:18 AM
The GoTorecord doesn't work...
I've also tried opening and closing the table...Nope! The data IS added, just that the combobox can't find it. ALSO, the warnings off doesn't work either!

CreganTur
09-24-2008, 08:54 AM
If the data you want to add to the listbox is recurring, then adding it to the underlying table would be your best bet. You would just need to reload your form before the new selection will show... at least that's how it should work.

mud2
09-24-2008, 03:58 PM
Your method would be the most straightforward..."but":
The combobox lists a series of choices, those that might be available now. But I can't think of what might become available, and want the USER to be able to add his/her/its other choices.
I'm sure there is a simple solution, buried, as usual, under a rock im some unidentified forest!

mud2
09-24-2008, 05:59 PM
I found it in A remote corner of Redwoods State Park>

So I gave up trying to make it "easy" for the user. Now he(?) must chose a "houskeeping" form that is dedicated to adding a record:
Form has a combobox...Combo0, and a text field Text11.
The user types his new choice into text11 and pressed Enter.

Sub text11_afterUpdate()
Dim Thisvalue as string
ThisValue = Text11.value
Text11.value = "" ' Just clears the box
Call AddToTable(Thisvalue)

Sub AddToTable(New_Entry)
Dim AddRecord as string
Dim ThisTable As String
ThisTable = Combo0.RecordSource
New_Entry = Chr(39) & New_Entry & Chr(39) ' make a literal
Addrecord = "Insert Into "& ThisTable & " Values " & "(" & New_Entry & ") ;"
DoCmd.Run Sql AddRecord
End Sub

The combobox is immediately updated, showing the user's entered New choice!

Seems too simple!

mud2
09-25-2008, 08:34 AM
In ACCESS 2003 and later, there is an "AddItem" function for Combo and list boxes. Very simple programming, but unfortunately it requires a "Value List" instead of a Table for a Row Source. A new can of worms, I'm sure! I'd rather stick with a table!
For anybody copying my solution, change the DoCmd.Run Sql to DoCmd.RunSql. The space (my mistyping) will cause an error.