Combo Box (Style = Dropdown list)

Posted on 2001-06-08
Last Modified: 2010-05-02

VB6(sp4) ADO

I'm trying to save the contents of a combo box (Style = Dropdown list) to a table through ADO, but I receive the message: "Field tbl_Action.DAuthor cannot be a zero-length string" if the user doesn't select an item from the combo box before attempting to save the record.

In other words, if the user clicks SAVE and the combo box doesn't have an item selected, the above message appears.

Here's what I need help with:

I need to allow the user to SKIP this combo box if they want to. In other words, if they don't intend to select an item from the combo list box, how do I avoid the error message above if (IsNull)?

Next, if the user selects an item from the combo box, I need to save that value to the table field.

Finally, how do I clear the choice the user may have selected from the combo box if he/she wants to remove the item before saving the record. You can't simply place the cursor in the box and hit the ESC key.


Question by:ADawn
LVL 50

Expert Comment

by:Ryan Chong
ID: 6167584
Hi, in your database (if is Access), set the field property to "Allow zero-length string"

To SKIP this:

if combo1.listindex = -1 then 'No item selected
   'Save to field

To clear user selection in combobox:

combo1.listindex= -1

'Hope will help.

Expert Comment

ID: 6167623
If you are using access, you should set allow zero length string property for this field to yes (In access).

Otherwise when doing rs.addnew

put condition

if comboName.text <> "" then
   rs("fieldname") = value
end if

If you want to clear the contents of the combobox then
have a small command button for clear


Private Sub Command1_Click()
Combo1.ListIndex = -1
End Sub

to clear the contents of button

or alternatively you could add the item

combo1.additem " "

to the bottom of list and allow people to select this value

Expert Comment

ID: 6167635

It looks like I have copied your code but I didn't (honest) I am just a slower typer.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Expert Comment

ID: 6167644

It looks like I have copied your code but I didn't (honest) I am just a slower typer.
LVL 50

Expert Comment

by:Ryan Chong
ID: 6167667
Hi damienm. It's ok.
LVL 18

Expert Comment

ID: 6167729
I think you should do

if ComboBox.Listindex >=0 then
   'save the value
   'nothing to save
end if


Expert Comment

ID: 6169456
changing the DB is not such a hot idea.

usually, you dont get to do this as some other gorilla is in charge of the DB, and doesnt like to change it.

I think the more ready answer is whether you have
a bound combo box or did you fill it manually.?

either case requires different solution.

do you have bound combo box?

Accepted Solution

Hornet241 earned 30 total points
ID: 6169958

Everyone has covered the Zero length string pretty well, for clearing the combo box though, if you do this

     Combo1.Listindex = -1

this will fire the Combo1_Click Event,

this will work with no Click event

     Combo1.Text = ""

but remember both will fire the Change Event


Expert Comment

ID: 6172519
Are you storing the text value that is selected in the combo box or the ItemData value?

If its the Itemdata value then just store a -1 in the field. Then when retreiving the value from the field, check for a -1 when setting to combo value. (If you load the data back into the form.)

If its the text then add an item to the combobox of "[No Selection]" and set it to the first item in the list. Then set the combo to ListIndex=0 to set this value.
Then just pass it into the database.

Plus, when looking through the data you will be able to easily see that someone skipped it. If it where blank you wouldn't know if it was skipped or just invalid data.


Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question