PDA

View Full Version : Solved: Is it possible to add a new item to existing list in a combox box using vba?



wedd
11-16-2011, 10:29 AM
Is it possible to add a new item to existing list in a combox box using vba? I used a requery but it doesn't seem to wrk...I have a list in a combo box called: Apple, oranges, lemons and would like to add lime to the list. Have you any sample code how I can do this? Is it possible to do this using vba? I created comboxname.requery but it didn't perform the action I wanted...I received a message that I should display on the names in the combo box...


Thanks for your contributions...:friends:

orange
11-21-2011, 10:36 AM
This may be useful
http://www.techrepublic.com/blog/howdoi/how-do-i-add-items-to-an-access-combo-box-on-the-fly/201

wedd
11-22-2011, 05:52 AM
Thanks, Orange! But there are errors within the code...especially in the code to display the messagebox. I am fairly new to vba programming...though it does look great. I have in the combo box the following items: English, Maths and Science and I would like the user to have the option to add new items based on a messagebox.

wedd
11-22-2011, 07:47 AM
Unfortunately this code wouldn't work for me because it only functions for XP and older versions of Windows. I have Windows 7....

wedd
11-22-2011, 08:07 AM
I used this code and it worked very well for me.

Private Sub CboLocations_NotInList(NewData As String, Response As Integer)
Dim strTmp As String

'Get confirmation that this is not just a spelling error.
strTmp = "Add '" & NewData & "' as a new location?"
If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then

'Append the NewData as a record in the Categories table.
strTmp = "INSERT INTO TblCheckavailability ( Locations ) " & _
"SELECT """ & NewData & """ AS Locations;"
DBEngine(0)(0).Execute strTmp, dbFailOnError

'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub