Copy listbox to another one - vba

I'm trying to copy a listbox to another one
So I clear all items in the destination listbox like this
While Forms!SalesCallInformation!lstMills.ListCount > 0
Forms!SalesCallInformation!lstMills.RemoveItem (0)
Wend
Then I copy all items like this
For i = 0 To lstMillsToAdd.ListCount - 1
Forms!SalesCallInformation!lstMills.AddItem (lstMillsToAdd.Column(0, i) & ";" & lstMillsToAdd.Column(1, i) & ";" & lstMillsToAdd.Column(2, i))
Next
The problem is that in the destination listbox, I still got the old items.
But when I set a watch on Forms!SalesCallInformation!lstMills.ListCount when I delete I can see that's it's decrementing so it must remove something.
Is it something with refreshing the form? Cause I tried after inserting the new items to do this: Forms!SalesCallInformation.Refresh but I have the same result.
Does anyone have an idea?
Thank you
Also, the rowSourceType is set to value list

This example works for me:
Private Sub Button1_Click()
Me.List2.RowSource = Me.List0.RowSource
End Sub
Private Sub Button2_Click()
Me.List2.RowSource = ""
End Sub
Both listboxes are Value Lists. The Row Source is set to "abc";"def";"ghi"

Related

Creating report for each value in listbox

I have a form, with a listbox. User can choose record and add it to a list box using listbox. I want to create a VBA script to open a report and match all the values in listbox with corresponding records. So far i can do this with single one, but for many i don't know what is going on. Here is the code:
Private Sub btnSearchMany_Click()
Dim i As Long
With Me.List12
If .ListCount = 0 Then
MsgBox "Brak wybranych wpisow.Dodaj wpisy, wybierajac je z listy i wciskajac przycisk dodaj.", vbCritical
Exit Sub
End If
For i = 0 To .ListCount - 1
DoCmd.OpenReport "rptKKsy", acViewReport, , "[tblKKsy].[KKS]='" & .List12(i) & "'"
Next i
End With
End Sub
List12- lisbox that user can add records "later i will rename it"
rptKKsy- my report
tblKKsy- table that stores values
KKs - one of the values stored in my table
I always getting error: method or data member not found. I tried to replace:
With Me.List12
with
With Forms("frmSearch").Form.list12
But it just created another error.
Edit: As suggested, my list box is a value list, and user can put thing in it by using a combobox and clicking a button ( button have addItem command programed). After user add some things, i want to open a report for each value stored in listbox. I dont want user to select things in listbox, because we will be using everything in it so its not nedded. I tried to use Item.Data property as June7 linked to, but ill be honest: I dont get it. Here is the code:
Private Sub btnSearchMany_Click()
Dim i As Long
Dim lValue As String
With Me.List12
For i = 0 To .ListCount - 1
If .ListCount = 0 Then
MsgBox "Brak wybranych wpisow.Dodaj wpisy, wybierajac je z listy i wciskajac przycisk dodaj.", vbCritical
Exit Sub
End If
Next i
End With
If i = Len(lValue) > 2 Then
DoCmd.OpenReport "rptKKsy", acViewReport, , "[tblKKsy].[KKS]='" & .ItemData(lValue) & "'"
End If
End Sub

VBA Checkbox to Listbox (uncheck option to remove array)

I am struggling with a simple thing and cannot resolve it. I have a userform that user can populate from a textbox manually. I decided to add a checkbox as well to allow the user to populate the same listbox with a specific list of items. To do it,I made a simple checkbox with array. It works perfectly fine. But obviously keeps adding the items every time you check and uncheck it.
Private Sub Checkbox1_Click()
Dim mylist(7) As String
Dim i As Long
mylist(0) = "time"
mylist(1) = "hour"
mylist(2) = "how"
mylist(3) = "test"
mylist(4) = "number"
mylist(5) = "sent"
mylist(6) = "memo"
mylist(7) = "value"
For i = 0 To 7
If CheckBox1.Value = True Then
Finallist.AddItem mylist(i)
End If
Next i
End Sub
I can populate the list when the checkbox is checked with the code above, but struggling to remove the array items from the list when the user unchecks the listbox. I simply need to remove the same items from listbox when user unchecks the same checkbox.
I tried the following solution after the code, but seem to be making something very wrong with it, I understand. Just totally stuck....Could someone help me please?
If checkobx.value=false then
For i = 0 To 7
For j = 0 To FinalList.ListCount - 1
If InStr(Final.List(j), mylist(i)) > 0 Then
Finallist.RemoveItem mylist(i)
End If
Next j
Next i
end if
Try this (explanations in comments and untested):
If CheckBox1.Value Then ‘ if checkbox checked
For i = 0 To 7
NegKeyList.AddItem interrlist(i)
Next
Else ‘otherwise
Dim i As Long
For i = NegKeyList.ListCount - 1 To 0 Step -1 ‘ loop through listbox items from last one backwards
If Not IsError(Application.Match(NegKeyList.List(i), interrlist,0)) Then NegKeyList.RemoveItem i ‘ if current listbox item matches any interrlist array item then remove it from listbox
Next
End If

Move selected items from one listbox to another

I have two listboxes, named listbox1 and listbox2.
Listbox1 is populated using a SQL query, and contains two columns. Its first column contains values that have commas.
Listbox2 is set as a value list in the "row source type" attribute of the Access property sheet.
My goal is to copy selected items from listbox1 to listbox2 using a control button.
I also need to be aware that listbox1 records contain commas, which act as delimiters during copying. That particular issue has been resolved, though.
I have created two modules to accomplish the copying of selected records from one listbox to another:
Public Sub CopySelected(ByRef frm As Form)
Dim ctlSource As Control
Dim ctlDest As Control
Dim strItems As String
Dim intCurrentRow As Integer
Set ctlSource = Me!listbox1
Set ctlDest = Me!listbox2
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) = True Then
'must insert double quote around single quote to escape commas
strItems = strItems & "'" & ctlSource.Column(0, intCurrentRow) & "'" & ";"
Me!listbox2.AddItem (strItems)
End If
Next intCurrentRow
End Sub
And
Private Sub cmdAddSelected_Click()
CopySelected Me
End Sub
I do have the multi-select option on listbox1 set to "extended".
The current problem is that when I click my control button, only the first selection from listbox1 is copied over- with the caveat that it is copied multiple times (copied the same number as those selected records).
Clearly, there is a problem with my For-loop.
R is my main language, and I am only just learning VBA.
In your program you are concatenating all selected values with separator and adding the longer and longer string to Listbox2. The ; acts as a column separator for multicolumn listboxes, i.e. you are kind of "transposing" the selected values from Listbox1 to Listbox2 while the excess items (above the number of columns of Listbox2) are simply ignored. If you want to copy individual values into the single column Listbox2, do this:
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) Then
strItems = "'" & ctlSource.Column(0, intCurrentRow) & "'"
Me!listbox2.AddItem (strItems)
End If
Next intCurrentRow

Error runtime 91 and 429

This code feels like Schrodinger is executing it. If I open the project and run the code, I won't get any errors at all. If I view the code to edit or add anything, the first time I run the code, I get 'Run-time error 91'. If I try to run it a second time, making no changes, I get 'Run-time error 429' (ActiveX component can't create object).
What I'm trying to achieve is to find the row (BuildSel) in a range on worksheet (Ref) that has the same value as what's selected in a list on a userform (BuildList). Then once the row is found, to take data from that row and columns A and B, and put them in textbox's on my userform. Is my code right and ActiveX making the error? I apologize for terrible coding too.
EDIT: The listbox is on a multipage on my userform. I first noticed the issue today when I tried adding another listbox on a different page.
Private Sub BuildList_Click()
Dim Ref As Worksheet, BuildSel As Long
Set Ref = ThisWorkbook.Sheets("Ref")
BuildSel = Ref.Range("B2", Ref.Range("B" & Rows.Count).End(xlUp)).Find(BuildList.Value, lookat:=xlPart).Row
BuilderText.Value = Ref.Range("A" & BuildSel).Value
CompNameText.Value = Ref.Range("B" & BuildSel).Value
End Sub
Not sure why altering 'BuildSel' to variant makes it work, but the code as it stands has no error checking for when there is no matching list item to be found
The following code should be better suited for usage:
Private Sub BuildList_Click()
Dim Ref As Worksheet: Set Ref = ThisWorkbook.Sheets("Ref")
Dim BuildSel As Range
With Ref
Set BuildSel = .Range("B2", .Range("B" & Rows.Count).End(xlUp)).Find _
(BuildList.Value, lookat:=xlPart)
If Not BuildSel Is Nothing Then
BuilderText.Value = .Range("A" & BuildSel.Row).Value
CompNameText.Value = .Range("B" & BuildSel.Row).Value
Else
BuilderText.Value = ""
CompNameText.Value = ""
End If
End With
End Sub

Removing All Items From A ComboBox?

How can I programmatically remove all items from a combobox in VBA?
Psuedo code ahead (updated with actual code):
Do While ComboBox1.ListCount > 0
ComboBox1.RemoveItem (0)
Loop
Basically, while you have items, remove the first item from the combobox. Once all the items have been removed (count = 0), your box is blank.
Method 2: Even better
ComboBox1.Clear
You need to remove each one individually unfortunately:
For i = 1 To ListBox1.ListCount
'Remove an item from the ListBox using ListBox1.RemoveItem
Next i
Update - I don't know why my answer did not include the full solution:
For i = ListBox1.ListCount - 1 to 0 Step - 1
ListBox1.RemoveItem i
Next i
The simplest way:
Combobox1.RowSource = "" 'Clear the list
Combobox1.Clear 'Clear the selected text
You can use the ControlFormat method:
ComboBox1.ControlFormat.RemoveAllItems
Best Way:
Combobox1.items.clear();
For Access VBA, if a ComboBox has been populated with a Row Source Type of Value List, I find the following works:
ComboBox.RowSource = ""
For Access VBA, which does not provide a .clear method on user form comboboxes, this solution works flawlessly for me:
If cbxCombobox.ListCount > 0 Then
For remloop = (cbxCombobox.ListCount - 1) To 0 Step -1
cbxCombobox.RemoveItem (remloop)
Next remloop
End If
In Access 2013 I've just tested this:
While ComboBox1.ListCount > 0
ComboBox1.RemoveItem 0
Wend
Interestingly, if you set the item list in Properties, this is not lost when you exit Form View and go back to Design View.
me.Combobox1.Clear
This is the common method
I could not get clear to work. (Mac Excel)
but this does.
ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems
If you want to simply remove the value in the combo box:
me.combobox = ""
If you want to remove the recordset of the combobox, the easiest way is:
me.combobox.recordset = ""
me.combobox.requery
Private Sub cmdClear_Click()
ComboBox1.Value = Null
ComboBox2.Value = Null
End Sub