Hi I have this form which works well when it is in list box. but when I change the list box to combo box, the form does not works and I will be stuck at "record does not exist" even though i choose the record from the combo box. this form works mainly by using vb and these are the code for the form:

Private Sub Command23_Click()
'On Error GoTo errhandler
'....................Deleting Agent details.........'

Dim dbs As Database, rst As Recordset
Dim tco, i, ITM, lhm, kk As Variant

kk = 0
For Each ITM In Lsta.ItemsSelected

lag1 = Lsta.Column(0, ITM)
lhm = Lsta.Column(1, ITM)
Next ITM

Tagent1.SetFocus
Tagent1.Text = lag1
thmcode.SetFocus
thmcode.Text = lhm

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("agent")
rst.Index = "PrimaryKey"

With rst
If .EOF Then

Else
.Seek "=", lag1
If .NoMatch Then

Msg = "Record does not exist !!"
Style = vbOKOnly + vbExclamation
Title = "Chemical Deletion"
Response = MsgBox(Msg, Style, Title)
Else

Msg = "Do you want to delete this record ?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "Chemical Deletion"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
Msg = " Record deleted !!"
Style = vbOKOnly + vbCritical
Title = "Chemical Deletion"
Response = MsgBox(Msg, Style, Title)
.DELETE
kk = 1
Else
Msg = " Record not deleted !!"
Style = vbOKOnly + vbInformation
Title = "Chemical Deletion"
Response = MsgBox(Msg, Style, Title)
End If
End If
End If
End With
rst.Close

Recommended Answers

All 10 Replies

Hi I have this form which works well when it is in list box.

Is this nor suppose to be the other way around, listbox in a form?

when I change the list box to combo box

Have you deleted the listbox and then added a new combobox?

Dim tco, i, ITM, lhm, kk As Variant

kk = 0

Make use of String or Integer. Variant calls slows down your system

Dim tco
Dim i As Integer, ITM, lhm
Dim kk As Integer

kk = 0

On Error GoTo errhandler
'....................Deleting Agent details.........'

Remove the error part to see where exactly the error occurs

Then finally, what name do you have for your combobox and what name for the listbox? What code is associated with these two controls? I can't see any code above that refers to any of them either.

This is the coding i have and I am not the creator of this codes. I was told to change the list box to combo box.

I think that this is the main part for choosing the text from the list box but what i wanted is not choosing from listbox but combobox.

kk = 0
For Each ITM In Lsta.ItemsSelected

lag1 = Lsta.Column(0, ITM)
lhm = Lsta.Column(1, ITM)
Next ITM

Tagent1.SetFocus
Tagent1.Text = lag1
thmcode.SetFocus
thmcode.Text = lhm

Both the list box and the combo box have the same name cause i juz simply right click the list box and change it to combo box and the name for the box is "Lsta". I wonder if this is the right way of doing it. Please help!!! I really appreciate all the help that is given.

Both the list box and the combo box have the same name cause i juz simply right click the list box and change it to combo box

This is impossible in VB6. You can either have a combobox on your form OR a listbox, not both. They will not share the same name either, vb6 will warn you that the name already exists and will give the control the default name.

What exactly do you want the code to do? Maybe then I can give a different option with different code.;)

ya i know that there can onli be either the listbox or the combo box to display on the screen using the same name. what i want to do is just changing the list box which has been created by the previous user to a combobox which allow the code to run as well by not juz right clicking on the list box and change it to combobox, but also do some changes to the code that has shown on the previous post.

ya i know that there can onli be either the listbox or the combo box to display on the screen using the same name. what i want to do is just changing the list box which has been created by the previous user to a combobox which allow the code to run as well by not juz right clicking on the list box and change it to combobox, but also do some changes to the code that has shown on the previous post.

Sorry, gone for a while.

If I understand this correctly, the following under the combobox -

Dim xItm As Integer
For xItm = 0 To cmbBox.ListCount - 1

lag1 = cmbBox.ListItem
lhm = Lsta.Column(1, ITM) 'This won't work because the combobox does not have coloumns, nor can it contain coloumns. I suggest you use another combobox to add a value to lhm...
Next xItm

'This will loop through the items in combobox. Add code to get an actual value for lagl

Tagent1.SetFocus
Tagent1.Text = lag1 'in this case it will be the last item in the combobox because of the loop...
'If you want a specific item from the combobox, change the code to...
Taggent1.Text = cmbBox.ListIndex(3) 'if you want item 3
thmcode.SetFocus
thmcode.Text = lhm

As you can see, we need more information of WHAT you want to achieve with your code. We can not guess what "lagl" or "lhm" stands for.
:)

erm 1 more question..

can u tell me wad does this statement means? cause the rowsource data is actually from a query so im wondering if there is any link..

thx in advance

For Each ITM In Lsta.ItemsSelected
lag1 = Lsta.Column(0, ITM)
lhm = Lsta.Column(1, ITM)
Next ITM

It reads from a listview/Listbox control. It then adds a value to lag1 and lhm from column o and 1.

So wad can I do if i juz directly convert the listbox into combobox where there are 2 column in the listbox. And how do I add the value to a variable using a combobox.

It seems that we are stuck on the part "convert listbox to combobox" part. I will reiterate, IT CAN NOT BE DONE. You have to create a combobox and write code on what it is suppose to do.

Now, if you can tel me (as I have asked a FEW times above), what exactly do you want the code to do, I can help you to write the code within the combobox events. I can not help you if I don't know what it is you want, apart from converting a listbox into a combobox.;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.