Combo box listIndex = -1 when entry selected

Posted on 2012-09-21
Medium Priority
Last Modified: 2012-09-28
I have a combo box on form1 portion of form with combo box that lists employer names in my demographic table. When I select an entry, I store the ListIndex in a table, go to form2. When form2 is closed, the selected value is restored in the combo box on form1 using the ListIndex.  For most selected values this works fine, but for a range of values the ListIndex is set to -1.  For example, list of values starting at 39 This list was generated in SQL Query Analyzer but it is the same list that appears in the combo box. When A-Team is selected the ListIndex = 41, but when A.A. Pesce Glass Co is selected, ListIndex = -1.  ListIndex is -1 for all values selected until the value Athena Bus Service LLC is selected. It has ListIndex = 711. list starting at 707 listindex.  Here are the lines of code I'm using when the Replace Employer button is clicked.  The name of the combo box is "employer".  The value is stored in the table correctly.  The question is why are some values assigned ListIndex= -1 and some assigned the correct ListIndex.  The problem occurs whether I replace the value or simply close form2.
Private Sub replaceEMP_Click()
Dim varLIEmployer As variant
Dim strEmplValue as string
Dim strUserID as string

If IsNull(Me!employer) Or IsEmpty(Me!employer) Then
  MsgBox "Select an employer value"
  Exit Sub
End If
varLIEmployer = Me!employer.ListIndex
strEmplValue = Me!employer.Value

DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE SessionData SET indexEmployer = " & varLIFindEmployer & _
             " WHERE userid = '" & strUserid & "'"
DoCmd.SetWarnings True
DoCmd.OpenForm "Find and Replace (REPLACE EMPLOYER)", acNormal, , "employer = '" & g_strEmplValue & "'"

End Sub

Thanks for your help.
Question by:scover22
LVL 85
ID: 38424451
So your data is hosted in SQL Server, and you're doing this in Access?

How is your Employer Combobox filled? Are you setting this to the name of a query, or are you using a SELECT statement, using AddItem, etc etc?

Do you have the Combo set to Multiselect?

Is this a BOUND combo (i.e. has a ControlSource)?

Is the value of strEmplValue always correct?
LVL 31

Expert Comment

ID: 38424597
Can you recreate the problem using a local database?

If so upload a sample.
LVL 17

Expert Comment

ID: 38424745
When and where do you discover the -1?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 38424802
So your data is hosted in SQL Server, and you're doing this in Access?

How is your Employer Combobox filled? Are you setting this to the name of a query, or are you using a SELECT statement, using AddItem, etc etc?
  Row Source is the following SELECT query
  SELECT DISTINCT ce_nc_demographic.employer FROM ce_nc_demographic WHERE (((ce_nc_demographic.employer) Is Not Null And (ce_nc_demographic.employer)<>"")) ORDER BY ce_nc_demographic.employer;

Do you have the Combo set to Multiselect?

Is this a BOUND combo (i.e. has a ControlSource)?

Is the value of strEmplValue always correct?

I've made a little progress by checking the ListIndex in the AfterUpdate event on the employer combo box.  The ListIndex is correct at that time, but changes to -1 when the Replace Employer button is clicked.  Now I'm storing the ListIndex in the SessionData table in the AfterUpdate event and it is correct.  When I activate form1, I lookup the ListIndex in the table and set employer.ItemData to that number. The value of the item now correctly appears in the combo box, but when I click on the dropdown list, the list is positioned at the first item in the list, not the item displayed in the list. I want the list to be positioned at the item named in the list. I tried setting the ListIndex property since help says it is read/write long, but I get a 7777 error.

I'll see if I can recreate this in a standalone db so I can upload it.

Author Comment

ID: 38425066
I am unable to duplicate the error in a standalone db.

Accepted Solution

scover22 earned 0 total points
ID: 38425085
I changed the query behind the employer combo box to trim the values in the list (ie. emp: Trim([employer]) ).  This fixed the problem.  How do I close this post without awarding points?
LVL 85
ID: 38425983
Just select your own post as the Solution: http://vimeo.com/35720633

EE Moderator
LVL 85
ID: 38425986
FWIW, I've never used the .ListIndex property to set the value of a list/combo. I've always used the Selected property:

Me.ListBox.Selected(MyIndex) = True


Author Closing Comment

ID: 38444066
The solution was unrelated to the question as posted. Still no explanation as to why certain values caused ListIndex to be set to -1, but since Trim on the values in the list worked to solve the problem, I suspect there was some anomaly in the data which doesn't surprise me.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

840 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