combobox not in list

james_lankford

Well-known Member
Joined
Jan 11, 2009
Messages
1,222
so let's say I have a combobox that is getting its data from a table of Colors

the table contains
Red
Green
Blue

so the combobox shows those values
the user goes to the combobox and enters Purple
the notinlist event gets triggered, I pop up a message, they say yes, I insert Purple into the Colors table and all is well
later the user goes to the combox and enters "______Purple"
no quotes, no underscores, I'm just showing that they're entering a bunch of spaces followed by some text
well, " Purple" is not in the colors table,
so the notinlist event gets triggered, I pop up a message, they say yes, I insert " Purple" into the Colors table and all is not well
I do not want " Purple" in the table
as far as I'm concerned its a duplicate of Purple
I also do not want a new entry of "____________________Orange"
but I cannot figure out how to do this

surely I can't be the first one with this problem, anyone got any ideas ?
 
Maybe something like this... ? (Based on Micron's suggestion of the DLookUp.)

VBA Code:
Private Sub cmbColor_NotInList(NewData As String, Response As Integer)
   
    ' from http://www.databasedev.co.uk/not_in_list.html
   
    Dim strSQL As String
    Dim i As Integer
    Dim Msg As String
   
    'Exit this sub if the combo box is cleared
    If NewData = "" Then Exit Sub
   
    If DCount("color", "color", "[color] = '" & Trim(NewData) & "'") > 0 Then
   
        Me.cmbColor.Value = Trim(NewData)
        Exit Sub
   
    End If
   
    Msg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
    Msg = Msg & "Do you want to add it?"
   
    i = MsgBox(Msg, vbQuestion + vbYesNo, "Unknown Book Category...")
    If i = vbYes Then
        strSQL = "Insert Into color ([color]) " & _
                 "values ('" & Trim(NewData) & "');"
        CurrentDb.Execute strSQL, dbFailOnError
        Response = acDataErrAdded
    Else
        Response = acDataErrContinue
    End If
   
End Sub


I would be careful with these kind of lookups, though, when using strings like that. This Google search should provide plenty of reading on ways of working around that.


I have a function called CSql() that I use for this purpose. I can maybe post it if this is something you'd be interested in using. Or you can come up with your own solution on that if you want.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top