Solved

# remove array element in access vba

Posted on 2014-10-02
1,491 Views
I wish to remove an element from the array that contains " " or "" I know it will involve a redim maybe a preserve too but not sure how to remove at the right place

so far:

For index = LBound(AliasArray) To UBound(AliasArray)
If AliasArray(index) = " " Or AliasArray(index) = "" Then remove that element

Next index
0
Question by:PeterBaileyUk
• 2
• 2

LVL 12

Expert Comment

You can use a temporary array.

``````Option Explicit

Sub EE()

Dim arr As Variant
Dim arrTmp() As Variant
Dim x As Long, counter As Long

arr = Array(1, 2, 3, " ", 4)

For x = LBound(arr) To UBound(arr)

If Not (arr(x) = "" Or arr(x) = " ") Then

ReDim Preserve arrTmp(counter)

arrTmp(counter) = arr(x)

counter = counter + 1

End If
Next x

arr = arrTmp

End Sub
``````
0

Author Comment

arr = Array(1, 2, 3, " ", 4)

i can see its making arr equal to array but the bits within the brackets i am not sure about
0

LVL 12

Accepted Solution

James Elliott earned 500 total points
Sorry, that is just me creating the initial array with some elements to test. If your AliasArray already has elements then you can remove the line. Something like this.

``````Option Explicit

Dim arrTmp() As Variant
Dim x As Long, counter As Long

For x = LBound(AliasArray) To UBound(AliasArray)

If Not (AliasArray(x) = "" Or AliasArray(x) = " ") Then

ReDim Preserve arrTmp(counter)

arrTmp(counter) = AliasArray(x)

counter = counter + 1

End If
Next x

AliasArray= arrTmp
``````
0

LVL 33

Expert Comment

How are you creating the array in the first place?

If it's through code using Redim etc then perhaps you could add something to the code that creates the array to exclude the values/strings/whatever you don't want in the array.
0

Author Closing Comment

0

## Featured Post

### Suggested Solutions

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…