Solved

Combo Box (Style = Dropdown list)

Posted on 2001-06-08
9
420 Views
Last Modified: 2010-05-02
Hey,

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.

Thanks,

ADawn
0
Comment
Question by:ADawn
9 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
   'SKIP
else
   'Save to field
endif

To clear user selection in combobox:

combo1.listindex= -1

'Hope will help.
0
 
LVL 2

Expert Comment

by:damienm
Comment Utility
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

with

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
0
 
LVL 2

Expert Comment

by:damienm
Comment Utility
ryancys,

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

Expert Comment

by:damienm
Comment Utility
ryancys,

It looks like I have copied your code but I didn't (honest) I am just a slower typer.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Hi damienm. It's ok.
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
I think you should do

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

0
 
LVL 3

Expert Comment

by:TheAnswerMan
Comment Utility
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?
0
 
LVL 3

Accepted Solution

by:
Hornet241 earned 30 total points
Comment Utility

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



0
 
LVL 1

Expert Comment

by:HelixDaKat
Comment Utility
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.

HelixDaKat
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now