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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
Hamed NasrRetired IT ProfessionalCommented:
Can you recreate the problem using a local database?

If so upload a sample.
ramromconsultant Commented:
When and where do you discover the -1?
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

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.
scover22Author Commented:
I am unable to duplicate the error in a standalone db.
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?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Just select your own post as the Solution: http://vimeo.com/35720633

SouthMod
EE Moderator
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
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.