Solved

VBSCRIPT - dim and redim?

Posted on 2006-06-29
12
1,603 Views
Last Modified: 2012-05-05
Hi

I try to do this to keep an array with 'erroneous website names'

Dim erroneousWebsites(0)

While ...

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
    erroneousWebsites(UBound(erroneousWebsites)) = Trim(CStr(website_rows.fields("website").value))


Wend

but I get

Syntax error - type mismatch on the line

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
0
Comment
Question by:paulwhelan
  • 3
  • 2
  • 2
  • +1
12 Comments
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
Hi paulwhelan,

You have to initially Dim the array with no elements:

Dim erroneousWebsites()

If you specify bounds in the initial declaration then it is fixed length and cannot be redimensioned.

Tim Cottee
0
 

Author Comment

by:paulwhelan
Comment Utility
Hey!

I still get the same error even with


Dim erroneousWebsites()

Thanks
0
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
paulwhelan,

Ok, that is probably because when it is declared initially it has no length and therefore UBound() is meaningless.
Try

If Len(Join(erroneousWebsites)) = 0 Then
    ReDim Preserve erroneousWebsites(1)
Else
    ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
End If

To avoid this.


Tim
0
 

Author Comment

by:paulwhelan
Comment Utility
Thanks Tim

That gives me an error at

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))

---------------

If Len(Join(erroneousWebsites)) = 0 Then
      ReDim Preserve erroneousWebsites(1)
    Else
      ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
    End If
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:lunchbyte
Comment Utility
Here is what I do


Dim erroneousWebsites()

Then before I use it add more it it, I set it to zero and just ignore index zero.

redim
Dim erroneousWebsites(0)

YOUR LOOP or whatever
   ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))

   erroneousWebsites(UBound(erroneousWebsites)) = "YOUR VALUE"

END LOOP
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 20 total points
Comment Utility
The +1 is in the wrong spot.  Move it outside the paren:

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites) + 1)
0
 
LVL 5

Expert Comment

by:lunchbyte
Comment Utility
LOL. I made the same mistake. That damn copy and paste.
0
 
LVL 5

Expert Comment

by:lunchbyte
Comment Utility
CORRECT ONE

Dim erroneousWebsites()

Then before I use it add more it it, I set it to zero and just ignore index zero.

redim
Dim erroneousWebsites(0)

YOUR LOOP or whatever
   ReDim Preserve erroneousWebsites(UBound(erroneousWebsites) + 1)

   erroneousWebsites(UBound(erroneousWebsites)) = "YOUR VALUE"

END LOOP
0
 
LVL 13

Expert Comment

by:jmundsack
Comment Utility
The reason for the error in the ORIGINAL QUESTION...

"Syntax error - type mismatch on the line
ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))"

...is because the + 1 is inside the parens, and I was the first to point this out.

TimCottee is also correct that if you want to ReDim you need to Dim without bounds initially.

But for 20 points it's hardly worth fighting about LOL.  Flip a coin?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now