Hi there,
I have a userform that contains a listbox that retrieves it's data from a table in a worksheet called Breakdown. The userform has a search function linked to the listbox and has a button to edit and a button to delete. These two buttons open their own userforms - for example the delete button shows a userform asking the user if they are sure they want to delete "________".
The edit button works exactly as intended. However my delete button technically does work, but when clicking yes to confirm deletion a 'Runtime error 380 - Could not set the list property. Invalid property value' pops up.
Even though it does delete the row from the table. Although I can't confirm if it does delete from the listbox as well as the pop up shows as soon as I hit either yes or no to confirm deletion. It does this error even when getting rid of the 'update listbox' part.
Here is my below code related to the delete button.
On the userform called SearchForm (that contains the listbox):
ConfirmDelete Userform:
I'm still fairly new to VBA so any assistance is greatly appreciated!
I have a userform that contains a listbox that retrieves it's data from a table in a worksheet called Breakdown. The userform has a search function linked to the listbox and has a button to edit and a button to delete. These two buttons open their own userforms - for example the delete button shows a userform asking the user if they are sure they want to delete "________".
The edit button works exactly as intended. However my delete button technically does work, but when clicking yes to confirm deletion a 'Runtime error 380 - Could not set the list property. Invalid property value' pops up.
Even though it does delete the row from the table. Although I can't confirm if it does delete from the listbox as well as the pop up shows as soon as I hit either yes or no to confirm deletion. It does this error even when getting rid of the 'update listbox' part.
Here is my below code related to the delete button.
On the userform called SearchForm (that contains the listbox):
Code:
Option Explicit
Dim tblbreakdownTable As ListObject
Public RecordRow As Long
Dim a As Variant
Private Sub UserForm_Initialize()
Dim i As Long
Set tblbreakdownTable = ThisWorkbook.Worksheets("Breakdown").ListObjects("breakdownTable")
With playerList
a = tblbreakdownTable.DataBodyRange.Value2
.List = a
For i = 1 To UBound(a)
playerList.List(i - 1, 9) = i
Next
.ColumnCount = 9
.ColumnHeads = False
End With
End Sub
Code:
Private Sub DeleteButton_Click()
Dim i As Long
If playerList.ListIndex = -1 Then
MsgBox "Select item"
Exit Sub
End If
ConfirmDelete.DeleteRecord Me, tblbreakdownTable
a = tblbreakdownTable.DataBodyRange.Value2
For i = 1 To UBound(a)
playerList.List(i - 1, 9) = i
Next
End Sub
ConfirmDelete Userform:
Code:
Option Explicit
Private Sub cmdYes_Click()
Me.Hide
End Sub
Private Sub cmdNo_Click()
cmdNo.Tag = vbCancel
Me.Hide
End Sub
Sub DeleteRecord(ByVal Form As Object, ByVal objTable As Object)
Dim rngData As Range
Dim i As Long
With Form.playerList
For i = 1 To 3
Me.Controls("Label" & i).Caption = .Column(Choose(i, 1, 0, 2))
Next i
Form.RecordRow = .List(.ListIndex, 9) 'Retrieves the row number of the sheet
End With
Me.Show
If Not Val(Me.cmdNo.Tag) = vbCancel Then
'Delete record from table
objTable.DataBodyRange.Rows(Form.RecordRow).Delete
Set rngData = objTable.DataBodyRange
'update listbox
With Form.playerList
.Clear
.List = rngData.Value2
.ListIndex = Form.RecordRow - 1
End With
MsgBox "Record Updated", 64, "Success"
End If
Unload Me
End Sub
I'm still fairly new to VBA so any assistance is greatly appreciated!