Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading the array number from a combo box

Status
Not open for further replies.

rickyoswald

Programmer
Jun 12, 2004
59
0
0
GB
How do I read the currently selected array number of a combo box? i.e. if I have the 4th item selected the array value would be '3'. I thought it was cboBox(x)... but have been told to use cboBox.item(x) however the only property I can find similar to that is .itemdata (which does not work).
 
You are looking for the ListIndex property.

e.g cboBox.Listindex
 
Hi,

Dim x as Integer
x = Combobox.ListIndex

Will give you the "Array" value.

If you want to get the item data use:

x = ComboBox.ItemData(ComboBox.ListIndex)

HTH

--
William
 
rickyoswald,

MsgBox Combo1.List(Combo1.ListIndex)

Vladk
 
cboBox.list(x)

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
too slow

sorry all

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Matt you have said this before but for some reason this property (method or whatever) does not seem to exist for me. There is a .ItemData however but neither do the job I wish.

The code I currently have is:

'On button click
Private Sub cmdMaintainRemove_Click()
'Only do somthing if this check is passed
If check = True Then
'Set 'i' equal to that of the array value of the currently selected item
i = cboMaintainList.ListIndex
'Move to the same location in the table
rstPersonTitle.Move i
'Delete the item
rstPersonTitle.Delete
'Update the recordset
rstPerson.Update
End If


End Sub
 
Ok add the following after the i = line...
Code:
debug.print i;" "; cboMaintainList.list(i);" "; cboMaintainList.itemdata(i);" ";  cboMaintainList.listindex
and after the rstpersonTitle.move line


add
Code:
debug.print i; " "; rstPersonTitle!<FieldName>

where <field name> is the name of the filed that the combobox list is filled from

You are relying on the order of records in the recordset matching the order in the combobox. This may not be true at all times.

Do you have a key or ID in the table. If you do, I would consider storing that in itemdata property of the combo box. This will provide a real link between the combo and dataset.

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
also
Code:
rstPerson.Update
is this correct? or shoudl it be rstPersonTitle?

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Indeed it should! However, still no changes (no errors and no delete).
Instead of checking the array value would it be simpler to search for the text (currently selected in to combo box ) in the table?
 
yep, it would

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Okay I'll get on it. I'll probably be back hassling you in a few mins tho ;)
 
So far everything I have works fine. I don't need any more control over the combo box for it to function as I wish currently. However if I need to add anything to the rest of the code to make the delete work then I will obviously amend it.
It is only the 'cmdMaintainRemove_Click' that does not work, it currently gives me the runtime error 3201: Either BOF or EOF is True, or the current record has been deleted...

Private Sub cmdMaintainAdd_Click()
cboMaintainList.AddItem txtMaintainAdd.Text

If check = True Then
With rstPersonTitle
.AddNew
![Title] = txtMaintainAdd.Text
.Update
End With
End If

If check = False Then
With rstWorkTitle
.AddNew
![Title] = txtMaintainAdd.Text
.Update
End With
End If

End Sub

Private Sub cmdMaintainPerson_Click()
cboMaintainList.Clear
With rstPersonTitle
i = 0
Do While i < rstPersonTitle.RecordCount
frmMaintain.cboMaintainList.AddItem ![Title]
.MoveNext
i = i + 1
Loop
.MoveFirst
End With

check = True

End Sub

Private Sub cmdMaintainRemove_Click()
Dim current As String

current = cboMaintainList.Text
i = o

If check = True Then
rstPersonTitle.MoveFirst
Do While i < rstPersonTitle.RecordCount
If rstPersonTitle.GetString = current Then
rstPersonTitle.Delete
End If
Loop
End If

If check = False Then
rstWorkTitle.MoveFirst
Do While i < rstWorkTitle.RecordCount
If rstWorkTitle.GetString = current Then
rstWorkTitle.Delete
End If
Loop
End If
End Sub

Private Sub cmdMaintainWork_Click()
cboMaintainList.Clear
With rstWorkTitle
i = 0
Do While i < rstWorkTitle.RecordCount
frmMaintain.cboMaintainList.AddItem ![Title]
.MoveNext
i = i + 1
Loop
.MoveFirst
End With

check = False

End Sub

Private Sub Form_Load()
Dim check As Boolean
Dim i As String

End Sub
 
Code:
Private Sub cmdMaintainRemove_Click()
    Dim current As String
    
    current = cboMaintainList.[b]list(cboMaintainList.listindex)[/b]
    i = o
    
    If check = True Then
        rstPersonTitle.MoveFirst
        Do [b]until rstPersonTitle.EOF[/b]

            If rstPersonTitle.GetString = current Then
                rstPersonTitle.Delete
           [b]else
                debug.print "[COLOR=red]'[/color]"rstPersonTitle.GetString & "[COLOR=red]' '[/color]" & current & "[COLOR=red]'[/color]"[/b]
            End If
        [b]rstPersonTitle.movenext[/b]
        Loop
    End If
    
    If check = False Then
        rstWorkTitle.MoveFirst
        Do While i < rstWorkTitle.RecordCount
            If rstWorkTitle.GetString = current Then
                rstWorkTitle.Delete
            End If
        Loop
    End If
End Sub

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I am still getting the EOF or BOF error. It seems to bring this up on the last statement (before the Ends and Loops) which is currently Debug.Print "'"; rstPersonTitle.GetString & "' '" & current & "'". The error also occured on "rstWorkTitle.Delete" before I amended it.
 
What exactly does this do?:
Debug.Print "'"; rstWorkTitle.GetString & "' '" & current & "'
 
Have you got the "immediate window" open? (view menu or press ctrl-G)

It should print out the current values of rstWorkTtitle.getstring and current for each time through the loop. This will give you an idea of wether the comparison is working ok...

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I understand. Well it is saying the recordset is BOF or EOF when reaching that part of the code. I assume it would do the same on rstWorkTitle.delete if the condition were met.
 
Let's just do one more check...all your code is in VB6, yes?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top