Remove a Record from an Array

By | 2019-08-24

Remove any record from an array, without leaving a gap in the records.

Option Explicit

Private Type my_type
   field1 As String
   field2 As Long
   field3 As Integer
End Type

Const MAX_ARRAY = 10
Dim my_array(0 To MAX_ARRAY - 1) As my_type

'Delete the record RecPos (from 0 to MaxRecs)...MaxRecs is the maximum array dimension
Public Sub DeleteRecordFromMyArray(RecPos As Integer, MaxRecs As Integer)
   Dim i As Integer

   'Move all the record forward by one position
   For i = RecPos To MaxRecs - 1
      my_array(i) = my_array(i + 1)
   Next
   'Reset the last array record
   my_array(MaxRecs).field1 = ""
   my_array(MaxRecs).field2 = 0
   my_array(MaxRecs).field3 = 0
End Sub

Usage

Private Sub Command1_Click()
   DeleteRecordFromMyArray 3, MAX_ARRAY - 1
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.