With the following code, the function only msgbox's "variable declaration" and "before for/each". i put them in there for debugging when i saw that my function wasn't getting all the way through.

Any suggestions?

Option Explicit

Function ArrToCSV(ByVal array)
	msgbox "variable declaration"
	Dim strTemp
	Dim strQuote
	Dim x
	strQuote = Chr(34)
	msgbox "before for/each"
	For each x in array
		msgbox x
		strTemp = strTemp & strQuote & x & strQuote & vbcrlf
	Next
	msgbox "after for/each"
	Set ArrToCSV = strTemp
End Function

Dani AI

Generated

Good diagnosis by and well spotted by — the behaviour you saw (the last debug MsgBox before the loop only) usually means the routine hit a problem trying to enumerate or index the thing you passed in. The concrete root cause here was that one of the arrays had a lower bound of 1, not 0, so code that assumed a 0 base blew up.

Practical checks and guards to add before looping:

  • Confirm the argument is actually an array with IsArray and inspect it with TypeName or a quick debug MsgBox for LBound/UBound.
  • If LBound/UBound themselves raise an error, the array is uninitialized or Empty — handle that case explicitly.
  • Don't assume a 0 base. Use the array's LBound and UBound when you iterate with numeric indexes so you work with 0-based or 1-based arrays the same way.

CSV-specific tips (since elements contain quotes):

  • Escape any internal double quotes by doubling them, then wrap the whole field in quotes. That is the correct CSV escaping rule.
  • For performance, build a sanitized list of fields and then combine them in one go instead of repeatedly concatenating a string inside a loop — using a join-style approach is much faster in VBScript/HTA.

Quick troubleshooting checklist to reproduce and validate the fix:

  1. MsgBox TypeName(yourVar) to ensure it is an array.
  2. MsgBox LBound(yourVar) / UBound(yourVar) to see bounds.
  3. Add an IsArray guard and handle Empty/Null.
  4. Sanitize fields (double quotes) then combine.

Applying those checks will make the function robust against arrays from different sources and prevent the "bombing out" you saw.

Recommended Answers

All 5 Replies

I've also tried un-abstracting (lol..) the function back into the code block, and even tried a standard for/next loop using Ubound(array), but still no luck. it gaffs right as it hits the 'for' :(

However,

msgbox array(0) & vbcrlf & array(1) & vbcrlf & array(2)

shows the first the elements of the array, as expected.

I'm stuck.

------
FYI: My array isn't actually named "array"; i just did it like that because it was the first thing that came to mind when i was posting.
------

Not quite sure what you are trying to accomplish but...

MsgBox Join(MyArray, vbNewLine)

Good Luck

Not quite sure what you are trying to accomplish but...

MsgBox Join(MyArray, vbNewLine)

Good Luck

The second post was just stating taht i know for a fact that the data is in that array, because i can pick apart elements of it and look at them.

I'm trying to put an array into a readable csv format, but because some of the elements have double quotes in them, i cant just simply do a 'join'; i have to wrap the entire element in quotes, hense the Chr(34)'s

i still dont see anything wrong with the syntax of my loop or function.

Well then go with the LBound/UBound to make sure you don't run out of bounds...

For ForLoopCounter = LBound(MyArray) To UBound(MyArray)
  '...

Good Luck

Well then go with the LBound/UBound to make sure you don't run out of bounds...

For ForLoopCounter = LBound(MyArray) To UBound(MyArray)
  '...

Good Luck

I think you found my problem. One of the arrays i was passing to this function had an LBound of 1, not 0, so it was crapping out.

thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.