The Erase Function

When you want to remove the data from an array, you should use the Erase function. This function deletes all the data held by static or dynamic arrays. In addition, the Erase function reallocates all of the memory assigned to a dynamic array. If a procedure has to use the dynamic array again, you must use the ReDim statement to specify the size of the array. The next hands-on demonstrates how to erase the data from the array cities.

© Hands-On 7-9: Removing Data from an Array

1. In a new module, enter the code of the FunCities procedure shown below.

2. Run the FunCities procedure.

' start indexing array elements at 1 Option Base 1

Sub FunCities()

' declare the array

Dim cities(1 To 5) As String

' assign the values to array elements cities(1) = "Las Vegas" cities(2) = "Orlando" cities(3) = "Atlantic City" cities(4) = "New York" cities(5) = "San Francisco"

' display the list of cities

MsgBox cities(1) & Chr(13) & cities(2) & Chr(13) _ & cities(3) & Chr(13) & cities(4) & Chr(13) _ & cities(5)

Erase cities

' show all that was erased

MsgBox cities(1) & Chr(13) & cities(2) & Chr(13) _ & cities(3) & Chr(13) & cities(4) & Chr(13) _

Introduction to Access 2003 VBA Programming

End Sub

After the Erase function deletes the values from the array, the MsgBox function displays an empty message box.

0 0

Post a comment