"Subscript out of range error"

H

HeislerKurt

I'm trying to do some simple experimenting with the Immediate Window
in Access' VB environment.

For instance, I wrote this function:

###

Public Function Testing()
Dim X As Integer

X = 4

Testing = X * 2

End Function

###

In the immediate window, when I type "?Testing", it should return 8.
But instead I get this error:

Run-time error '9' : Subscript out of range

Any idea?

Kurt
 
A

Allen Browne

Something is wrong elsewhere.

In the Immediate Window, choose Compile on the Debug menu.
Fix any error, and repeat until your code compiles.
 
H

HeislerKurt

I compiled but it didn't detect any errors.

I also just created a new database, created an unbound form, and
attempted this same function in VB.

When I pressed Enter after typing "?Testing" in the immediate window,
the cursor just went to a new line in the Immediate Window. It didn't
return anything. (In fact, it did this before when I was testing this
initially, and then for unexplained reasons started to produce the
error I mentioned.)

However, if I type something like, "?4+2" it correctly returns "6".

Compacting the database has no effect.

Very strange.
 
A

Allen Browne

Where is this code? Is it in a standard module (one you can see listed on
the Modules form of the Database window)? Or is it in the module of a form?

Do you have anything else named Testing?
Did you give this name to the MDB file as well?
 
H

HeislerKurt

Discovered the problem.

I've been testing the code in a module of a form. When I tested it in
a standard module, it worked fine.

But this puzzles me - shouldn't you be able to use the immediate
window to test your code when working in the module of a form?

As for your other questions, nothing else is named Testing. (In fact,
there's nothing else in the database except for this module.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top