ListBox - MultiSelect VBA issue

T

Trepalium

I'm having difficulty with this code... I'm trying to go through and
reference multiple columns for each item that is selected in a list
box. When I run this code it only executes one time and removes one
item when I have two items selected. Code is below. Let me know if you
have any ideas.. - Thnx

Dim PstClsNme As String, PstClsID As String, PstClsAssignDte As Date,
tpe As String, rw As Variant

Call DBConnect

If List0.ListCount = 0 Then
MsgBox "You must select at least one item to assign!", vbCritical,
"Error!"
Else

For Each rw In List0.ItemsSelected


tpe = List0.Column(10, rw)


If tpe = "D2RE" Then
D2RE = True
ElseIf tpe = "Partnership" Then
Partner = True
End If

PstClsNme = Combo2.Column(0)
sql = "SELECT USERID, NAME FROM " & tblUserData & " WHERE
NAME = '" & PstClsNme & "'"
rsdata.Open (sql)
If rsdata.EOF Then
MsgBox "Post-Closer could not be located!",
vbCritical, "Post-Closer Not Found!"
rsdata.Close
Else
PstClsNme = rsdata("Name")
PstClsID = rsdata("USERID")
PstClsAssignDte = Format(Date, "mm/dd/yyyy")



rsdata.Close
sql = "INSERT INTO " & tblFileAssign & " VALUES ('" &
PstClsID & "', '" & PstClsNme & "','" & List0.Column(1, rw) & "','" &
PstClsAssignDte & "')"
rsdata.Open (sql)
If D2RE = True Then
sql = "UPDATE " & tblCustInfoD2RE & " SET ASSIGNED
= 1 WHERE CLAPPLNUMBER = '" & List0.Column(1, rw) & "'"
rsdata.Open (sql)
List0.RemoveItem rw
ElseIf Partner = True Then
End If
End If

Next rw
End If
 
G

Guest

Personally, I don't like to remove items from a list while I am processing
the SelectedItems, unless my Row Source Type is a table or a query. Try
remarking that line out and see what happens. Are you using a value list as
the source for your listbox? I prefer to use tables, so that I can delete
the rows from the table, then requery the list rather than trying to remove
the list items directly from the list.

I assume that D2RE and Partner are both declared as boolean, somewhere else
in your code. But based on the code you provided, cannot figure out why you
are even bothering with the Partner variable.

Also, your first If statement should probably be:

If list0.itemsselected.Count = 0 then ....
Don't know how others feel about this topic, but I would exit your sub or
function right after the MsgBox, then end the If statement. I find that the
fewer indentions in my code the easier it is to read.

HTH
Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top