scover22
asked on
Combo box listIndex = -1 when entry selected
I have a combo box on form1 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, 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. . 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.
__________________________
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.
Can you recreate the problem using a local database?
If so upload a sample.
If so upload a sample.
When and where do you discover the -1?
ASKER
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.emplo yer) Is Not Null And (ce_nc_demographic.employe r)<>"")) 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.
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
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.
ASKER
I am unable to duplicate the error in a standalone db.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(MyInde x) = True
http://msdn.microsoft.com/en-us/library/office/aa196573(v=office.11).aspx
Me.ListBox.Selected(MyInde
http://msdn.microsoft.com/en-us/library/office/aa196573(v=office.11).aspx
ASKER
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.
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?