Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I declare an array and give it values?

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
I know how to declare an array and give it values using MULTIPLE lines of vba code. Here is an example:

Public fileNameArray(0 To 4) As String
fileNameArray(0) = "a"
fileNameArray(1) = "b"
fileNameArray(2) = "c"
fileNameArray(3) = "d"
fileNameArray(4) = "e"

How would I do that all on ONE line?

 
As in Split or Array? Or did you wish to iterate?
 
Are you trying to fill static values or fill it with values from a database?

Basically you are doing it the only way possible for static, if you are wanting to fill from a recordset then you can do it with a counter variable and loop.

I guess what Remou and I are really asking is for a more indepth explanation of what you are trying to do.



Andy Baldwin

"Testing is the most overlooked programming language on the books!"

Ask a great question, get a great answer. Ask a vague question, get a vague answer.
Find out how to get great answers FAQ219-2884.
 
I want to declare and populate the array with just one line of code. I want to write something like this...

Public fileNameArray(0 To 4) As String={"a","b","c"}
 
You need either Array or Split. From Help:

MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")


 
Well, if I put the following code at the top of a module I get a syntax error:

Public Const fileNameArray(0 To 4) as string = Array("a","b","c","d","e")
 
Oh, wait. Is it like this?

Public Const fileNameArray = Array("a", "b", "c", "d", "e")


That does not give an error. But where do I specify that it is a string array? Hmmm...
 
I don't think that
Code:
Public Const fileNameArray = Array("a", "b", "c", "d", "e")
will work because constants are initialized when the program is loaded but before any code runs. "Array" is however, a builtin function (i.e. code) so it can't be part of the definition of a constant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top