I’m using Access 2010 under Win7. I’ve discovered I can dimension my arrays at run-time by simply invoking ReDim arrayName(x) without first declaring the array as Dim arrayName().
Sub FooBar()
ReDim myArray(2)
myArray(0) = "This is the first string in myArray."
myArray(1) = "This is the second string in myArray."
myArray(2) = "And this is the last string in myArray."
MsgBox myArray(0) & vbCrLf & myArray(1) & vbCrLf & myArray(2)
End Sub
Is there any reason I should NOT use this shortcut?
Cheers!
That’s interesting. This MSDN page confirms what you’re seeing: Here’s a quote:
This page explains that
Redimcreates a new array and that the existing array is copied into it (assuming there is one):http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=vs.80%29.aspx
As to your question, should you do it, I’d say no, because it’s confusing, and does open your code to errors that Option Explicit won’t catch.
Reasonably enough,
Redim Preservedoesn’t exhibit this behavior.