hi, all
I am trying to delete items but i keep getting a subscript out of range. I left my code below to see if i done something wrong.
ThanksCode:Private Items() As String Private RecCount As Long Public Sub AddRecord(ByVal sRec As String) 'Resize array ReDim Preserve Items(0 To RecCount) As String 'Store record Items(RecCount) = sRec 'Inc record counter RecCount = (RecCount + 1) End Sub Private Sub DeleteRecord(ByVal Index As Long) Dim Counter As Long Dim Tmp() As String Dim idx As Long On Error GoTo ErrFlag: For Counter = 0 To RecCount - 1 If (Counter <> Index) Then ReDim Preserve Tmp(0 To idx) As String Tmp(idx) = Items(Counter) idx = (idx + 1) 'Inc idx End If Next Counter Items = Tmp RecCount = UBound(Tmp) Exit Sub ErrFlag: RecCount = 0 End Sub Private Sub cmdDelete_Click() Call DeleteRecord(0) Call Command1_Click End Sub Private Sub Command1_Click() Dim x As Long 'Clear listbox Call List1.Clear 'Show items in list box For x = 0 To UBound(Items) Call List1.AddItem(Items(x)) Next x End Sub Private Sub Form_Load() 'Add some items AddRecord "Software" AddRecord "Games" AddRecord "Hardware" End Sub




Reply With Quote