?
Solved

catching an error in call to a collection

Posted on 2006-05-08
7
Medium Priority
?
153 Views
Last Modified: 2010-05-01
I have a collection and I'm running a loop that makes calls to it to get values based on the item values.  However, I don't know how to catch an error where an item doens't exist in the collection.  For example:

dim test as collection

' test containts the following items "a", "b", "c" as the keys

What if I make a call to test with key = "d".

How do I catch the error or determine using the error object that an invalid key was used with the collection?

I tried msgbox test("d") but I got an object not defined error.

Thanks,
vishalarya
0
Comment
Question by:vishalarya
7 Comments
 
LVL 13

Accepted Solution

by:
rettiseert earned 500 total points
ID: 16635508
on error resume next 'Start error handling
v = test("d")
if err then
   'Handle the error here
end if
on error go 0  'End error handling
0
 

Author Comment

by:vishalarya
ID: 16635525
What I want to acheive is if there is an error then do someting, if there is no error then continue..so would the code look like this:

v = test("d")

if err then
' do whatever code here if error occurs
else
' do whatever code here if error doens't occur
end if?
on error go 0

what does "on error go 0 mean" though?
0
 
LVL 13

Expert Comment

by:iHadi
ID: 16635538
Add a textbox and a command button to a form and paste the following code:

Dim test As New Collection

Private Sub Form_Load()
test.Add "Item1", "a"
test.Add "Item2", "b"
test.Add "Item3", "c"
End Sub

Private Sub Command1_Click()
Dim ItemName As String

On Error GoTo Er1
    ItemName = test.Item(Text1.Text)
    MsgBox "Found item: " & ItemName
Exit Sub
Er1:
    MsgBox "No item found with index: " & Text1.Text
End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Expert Comment

by:iHadi
ID: 16635553
On Error goto 0: Disables any On Error statements declared before it means do not handle any errors after this statement
0
 

Author Comment

by:vishalarya
ID: 16635613
This test for error is within a larger for loop.  Here is the pseudo code:

dim test as collection
for k = 1 to last_item

key = cells(k,1)

item = test(key)

IF KEY DOENS'T EXIST THEN
{DO ERROR CODE HERE}
ELSE
{USE ITEM AND KEY HERE}

next k

should the IF statement be the if(err)?

0
 
LVL 13

Expert Comment

by:iHadi
ID: 16635672
dim test as collection

on error resume next

for k = 1 to last_item

key = cells(k,1)

item = test(key)

if err then
   'Handle the error here
   Err.Clear
end if

next k
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16635721
Click on Project --> References and check the "Microsoft Scripting Runtime" entry.

Now use a Dictionary object instead of a Collection.

The Dictionary class has an Exists() method you can use to determine if a Key exists without messing about with trapping errors:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/157fb2fe-7453-480c-bb8b-3aa5c0b21cfd.asp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

621 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