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 ?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Any reason that you cannot just remove those spaces using Trim function?
 
Upvote 0
so if they enter _____________Orange
and in the notinlist event I do the insert with a trim, then Orange does go into the table
but when the notinlist event finishes the text displayed in the combobox still shows _________________Orange and immediately ANOTHER message pops up saying _________________Orange is not in the list
 
Upvote 0
so if they enter _____________Orange
and in the notinlist event I do the insert with a trim, then Orange does go into the table
but when the notinlist event finishes the text displayed in the combobox still shows _________________Orange and immediately ANOTHER message pops up saying _________________Orange is not in the list
It helps if others can see the code you used to populate the ComboBox. Probably they can suggest when and where to modified the code
 
Upvote 0
Agree with posting the code, or if you want to try something yourself, when you have added the value, requery the combo or make its value be the trimmed value.
Alternatively, you could hang the offenders by their eyelids and beat them until they blink. That is my favourite method of getting compliance.
 
Upvote 0
I also think if users insist on entering values with leading spaces its fine to let them see the not in list messages over and over again until they stop doing it ;)
 
Upvote 0
Another idea would be prevention space from being keyed in using Keypress event if the input is no more than single word .. perhaps.
 
Upvote 0
my database objects :
==============================
table = color

field
ID = autonumber, Primary Key

field
color = Short Text 255
Required = Yes
Allow Zero Length = No
Indexed = No
==============================
table = clothing

field
ID = autonumber, Primary Key

field
type = Short Text 255
Required = Yes
Allow Zero Length = No
Indexed = No
==============================
table = main

field
ID = autonmber, Primary Key

field
ColorID = Number, Long Integer

field
ClothingID = Number, Long Integer
==============================
INSERT INTO clothing ( type )
SELECT "pants" AS type

INSERT INTO clothing ( type )
SELECT "shoes" AS type

INSERT INTO clothing ( type )
SELECT "shirt" AS type

INSERT INTO clothing ( type )
SELECT "dress" AS type
==============================
INSERT INTO color ( color )
SELECT "red" AS color

INSERT INTO color ( color )
SELECT "green" AS color

INSERT INTO color ( color )
SELECT "blue" AS color
==============================
form = main
Row Source = SELECT ID, ColorID, ClothingID FROM main;

contains 2 combo boxes
1) cmbColor

Control Source = ColorID
Row Source = SELECT ID, color FROM color;
Bound Column = 1
Column Count = 2
Column Widths = 0";1"

2) cmbClothing
Control Source = ClothingID
Row Source = SELECT ID, type FROM clothing;
Bound Column = 1
Column Count = 2
Column Widths = 0";1"
============================================================================
my code for the NotInList event for the two comboboxes
notice I trim the NewData in the sql

VBA Code:
Option Compare Database
Option Explicit
'**************************************************************************
Private Sub cmbClothing_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
    
    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 clothing ([type]) " & _
                 "values ('" & Trim(NewData) & "');"
        CurrentDb.Execute strSQL, dbFailOnError
        Response = acDataErrAdded
    Else
        Response = acDataErrContinue
    End If
    
End Sub
'**************************************************************************
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
    
    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
'**************************************************************************

the the color table contains red, green, blue
the user enters ____________green in the color combobox
and the NotInList event triggers

NotInList_1.png



I click Yes and "green"
and IMMEDIATELY this pops up

NotInList_2.png



I click OK and look at the color table and see red, green, blue. green
green is in there twice
and it is the trimmed green
so the insert worked
but the combobox is still showing ___________green and I get that nasty 2nd popup message

if I set the index of the color field to "No Duplicates" then I still get the 2nd pop up, but the message changes to "Run Time error 3022: the changes were not successful, duplicate values blah blah blah", and the combobox is still showing ___________green
(obviously green was not inserted because of the no duplicates error)

if I set the index of the color field to "No Duplicates" and add an error handler in the NotInList event to catch err.number 3022 then I'm back to getting the pop up in the 2nd image "The text you entered isn't an item in the list" and the combobox still shows __________green

ideally what I would like is for the color field to be indexed, no duplicates
and if the user enters _________green the combobox just knows its green and the NotInList never fires
or if the NotInList event fires I attempt to insert a trimmed(______green) and catch the 3022 error and ignore it and do not get any 2nd pop up message

ok, I think that sums it up
is there a place where I can upload an access database that you would feel safe to download and see what's going on ?
 
Upvote 0
had to make a new account because I'm at work and can't automatically log in and I don't know my password
tried to reset my password but its been 30 minutes and the form won't send me the reset
and apparently the new account can't edit the post it just made LOL

I click Yes and "green"
and IMMEDIATELY this pops up

should say

I click Yes
and IMMEDIATELY this pops up
 
Upvote 0
I believe the logic is that before the control is allowed to update the nol event fires. At that point it is too late to trim leading spaces because NewData is what it is. If you could trim it's value in the code it will not agree with what the NewData parameter is, which was triggered by a value that wasn't in the list. Altering it will only add the duplicate value - unless you have constraints at the field level, which will raise another error. So my guesses are
- trim NewData then do a DLookup and see if you can exit/cancel the nol event if the value is found
- since Change and KeyUp events occur just before NotInList, trap a space - perhaps with Change event, something like
If Left([fieldNameHere],1) = Asc(32) Then [fieldNameHere] = Trim([fieldNameHere])
 
Upvote 0

Forum statistics

Threads
1,215,530
Messages
6,125,347
Members
449,220
Latest member
Edwin_SVRZ

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