Combo box listIndex = -1 when entry selected

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"
  Me!employer.SetFocus
  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.
scover22Asked:
Who is Participating?
 
scover22Author Commented:
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?
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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?
0
 
hnasrCommented:
Can you recreate the problem using a local database?

If so upload a sample.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
ramromconsultant Commented:
When and where do you discover the -1?
0
 
scover22Author Commented:
So your data is hosted in SQL Server, and you're doing this in Access?
  Yes

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?
  NO

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

Is the value of strEmplValue always correct?
  YES

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.
0
 
scover22Author Commented:
I am unable to duplicate the error in a standalone db.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Just select your own post as the Solution: http://vimeo.com/35720633

SouthMod
EE Moderator
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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

http://msdn.microsoft.com/en-us/library/office/aa196573(v=office.11).aspx
0
 
scover22Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.