Adding unique records to a listbox from another listbox - vba

I am hoping someone can help me out because I don't often code in Excel VBA. I have two Listbox and a CommandButton on a UserForm, which adds selected records from the first ListBox to the second. The range in the first ListBox is kind of long and so users of the form will generally select a couple records, hit the Add button, and then scroll down the first ListBox to select more records to add. The problem is, only the ones that have been added last will actually be recognized and used to generate reports even though all of the selected records appear in the second ListBox. Here is my current code for the button that adds the records to the second ListBox:
Private Sub AddButton_Click()
For i = 0 To Listbox1.ListCount - 1
If Listbox1.Selected(i) = True Then Listbox2.AddItem Listbox2.List(i)
Next i
End Sub
How can I populate the second ListBox so that it will include every record selected no matter how many times the add button is used?

Related

MS Access VBA - First Record In List is Overwritten on Form Close?

I just learning how to create forms in Microsoft Access 2013 and using VBA programming to control but I'm having an issue I don't quite understand.
I have created a form with a List Box where the source originates from a query that contains the following fields the Query Builder in order from left to right:
|ParentNumber|ParentName|ChildNumber|ChildName|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
|------------|----------|-----------|---------|
Some fields from the Query are hidden with a Column Width of 0".
I also have 4 Text Box below the List Box corresponding to the appropriate ParentNumber, ParentName, ChildNumber, and ChildName values.
When I select a record in the List Box, it populates the data to the appropriate Text Box.
When the form first loads, the first row in the List Box is selected:
Private Sub Form_Load()
On Error Resume Next
DoCmd.GoToRecord , , acFirst
Me.List = Me.List.ItemData(0)
End Sub
The issue is that if I select a different row, then close the form, and reopen the form, the first row in the List Box is overwritten with the last selected row before the form is closed.
Even if I start out with any other row selected initially when the form opens, the first row is always overwritten by the last selected row when the form is closed.
The following subroutine handles the update of the Text Box data:
Private Sub List_AfterUpdate()
Dim rowIndex As Integer
rowIndex = Me.List.ListIndex
Me.textBox_ParentNumber = Me.List.Column(3, rowIndex)
Me.textBox_ParentName = Me.List.Column(4, rowIndex)
Me.textBox_ChildNumber = Me.List.Column(6, rowIndex)
Me.textBox_ChildName = Me.List.Column(7, rowIndex)
End Sub
I found a somewhat similar problem to mine, but I tried the suggested solution, which didn't seem to work: MS Access - First record in table is overwritten on form close
I'm completely baffled as to what could cause this based on the code above.
What's my issue?
Thanks.
Few things
Private Sub Form_Load()
'remove this so you can see errors.
'On Error Resume Next
' this goes to the first record of the *form*, not the list.
' you might want this, or not.
DoCmd.GoToRecord , , acFirst
' ItemData returns the data in the *bound column* of the list,
' the data from one specific column. the list is set to that
' data every time the form loads. not what you want; remove it.
'Me.List = Me.List.ItemData(0)
End Sub
This should get rid of the problem. What you want to do next is another question.
Your form should be bound to a record set (table or query).
I had same problem, After hours of verification -
In my case - the problem was that the MainForm , And SubForm (table) - was binded to the same recordset. I just Un-Binded data RS from MainForm.

Link cell value to form combobox value

Might be easy, but how do I link a form comboxbox value to a cell or a dropdown list of cells ? In my case a 1 or 2 dimensional with month - year or month txt - month nr value or array needs to be linked to a form combobox.
The result should be that when I change the form combobox value it also updates the excel cell or cells (the 2 cells combination above) and also the other way around, when I update the cell or cells, it also updates the form combobox value.
Atm, I only have it one way, from form combobox to value by using the built in sub.
Sub ComboBox1_Change()
ThisWorkbook.Sheets("List").Range("G2").value = Form1.ComboBox1.Value
End Sub
After some digging around, I have found the answer to be in the Combobox properties window, ControlSource. I put there in my case 'List'!G2 and combined with the Sub ComboBox1_Change() it updates the month both ways.

How to set the items of a ComboBox into another one

I'm populating a VBA ComboBox with some data from a Workshet. As it is populated depending on the choice of the user, sometimes some levels are should not be populated, because they do not have data. When such a thing happens, I want to populate the items of a ComboBox with the same of another. How can a do that?
I think if somebody teaches me how to loop through the items of a ComboBox, I will the able to do what I want.
Here is how you loop through a combobox.
Dim intComboItem As Integer
For intComboItem = 0 To Me.ComboBox1.ListCount - 1
MsgBox Me.ComboBox1.List(intComboItem)
Next

How to delete multiple selections in a listbox?

So I have a userform that has a:
listbox called "lstentries"
radio button called "Delete".
Also, the "Start" is the name of the cell that is the top left corner of the table of rows.
I already set the code so it can select multiple entries in the listbox. However, when I select multiple entries it only deletes the first row that it comes to. So i tried making a while loop so it keeps deleting the selected ones, but that's not working either. I get the "Run-time error '1004' - Method 'Range' of object'_Global' failed"
Hopefully someone can help me out. Here is the snippet of the code where it deletes the rows. Thanks in advance.
If optDelete.Value = True Then
Dim delete As Range
Do While True
Set delete = Range("start").Offset(lstEntries.ListIndex, 0)
delete.EntireRow.delete Shift:=xlUp
Loop
End If
In the list box you can run through the list of items. Since you are allowing for multiple items to be selected and deleted, you will have to adjust the index as items are removed.
The list box is 0-indexed which means it starts at 0 and not 1.
' set the to go through all items in the list
For listIndexCount = 0 To lstEntries.ListCount
' check to make sure the index count is not greater or equal to the total number of items
' this is needed because the ListCount will change if items are deleted
If listIndexCount >= lstEntries.ListCount Then
Exit Sub
End If
' check to see if the item is selected
If lstEntries.Selected(listIndexCount) Then
' remove item
lstEntries.RemoveItem (listIndexCount)
' subtract 1 to account for removed item
listIndexCount = listIndexCount - 1
End If
Next
Another option in cases like this is to iterate through in reverse order. When starting at the bottom of the list you do not have to worry about adjusting the index since the items further up are not affected by removing an item below them.

Rows Not Highlighting in Data Grid View

I have a simple selection form that displays a DataGridView and allows the user to select a record to process. I have a short sub routine that loops through the DGV and highlights rows based on a date comparison with a dictionary when the form loads. This works great the first time I open the form, but on subsequent form opens, the grid doesn't highlight. When I step through the code it looks like the rows should be highlighted, but when the form displays, nothing is highlighted.
Here is the code I'm using. I cannot figure out why it doesn't always work. Is there a better, more reliable way to accomplish this?
For Each row As DataGridViewRow In dgvPending.Rows
For Each pair In dPending
If row.Cells.Item("ID").Value = pair.Key Then
If row.Cells.Item("LAST_UPDATED").Value > pair.Value Then
row.DefaultCellStyle.BackColor = Color.BlanchedAlmond
End If
End If
Next
Next