Results 1 to 3 of 3

Thread: [RESOLVED] Delete Items From Array

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Resolved [RESOLVED] Delete Items From Array

    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.

    Code:
    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
    Thanks

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Delete Items From Array

    I had made some changes to it. Try it:
    Code:
    Option Explicit
    
    Private Items() As String
    Private RecCount As Long
    
    Public Sub AddRecord(ByVal sRec As String)
        'Inc record counter
        RecCount = RecCount + 1
        'Resize array
        ReDim Preserve Items(RecCount) As String
        'Store record
        Items(RecCount) = sRec
    End Sub
    
    Private Sub DeleteRecord(ByVal Index As Long)
      If RecCount = -1 Then Exit Sub '~~~> If it is empty, then prevent the rest of the code from executing
      Dim Counter As Long
    
      For Counter = Index To RecCount - 1 ' - 1
        Items(Counter) = Items(Counter + 1)
      Next Counter
      RecCount = RecCount - 1
      
      If RecCount <= -1 Then
        Erase Items
      Else
        ReDim Preserve Items(RecCount)
      End If
    
    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
        
        If RecCount = -1 Then Exit Sub  '~~~> If it is empty, then prevent the rest of the code from executing
        'Show items in list box
        For x = 0 To UBound(Items)
            Call List1.AddItem(Items(x))
        Next x
    End Sub
    
    Private Sub Form_Load()
        RecCount = -1 '~~~> Initialize
        'Add some items
        AddRecord "Software"
        AddRecord "Games"
        AddRecord "Hardware"
    End Sub
    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: Delete Items From Array

    Thanks akhileshbc that seems to work now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width