views:

93

answers:

2

I am trying to format a number as currency in Access VBA.

In the immediate window, when I enter:

? Format(123, "Currency")

I get the expected response: "$123.00"

However, in the code window, when I enter:

Debug.Print Format(123, "Currency")

I get an error pointing to that line: "Run-time error '13': Type mismatch"

Why does the same simple code work in the immediate window, but throw an error when run from the code window?

+1  A: 

I don't see why your second example should cause an error. The following subroutine compiles and runs without error on my Access 2003 system:

Public Sub test_Format()
    Debug.Print Format(123, "Currency")
End Sub

Try that subroutine in a new database. Perhaps your current database is corrupted.

See Tony Toews' Corrupt Microsoft Access MDBs FAQ

HansUp
I tried that in a new database and it worked. How can I tell for sure whether my database is corrupted? Other code executes properly.
LCountee
HansUp
Also check your project's references in the VBE editor. Strange things happen with missing/broken references, although your problem isn't really a symptom I've noticed before. It won't hurt to check.
HansUp
Strange things can also happen if a procedure or macro is named with a reserved word.
Remou
I would decompile before importing in to a new database. There are all sorts of things you lose when you import, such as history of objects, as Startup settings, and any custom properties. The first of those can never be recovered, while the latter two have to be recreated from scratch. If decompile doesn't work, I'd still try objects with SaveAsText/LoadFromText before I started a new database.
David-W-Fenton
LCountee
Just checking ... did you check your project's references and make sure you have no name conflicts with procedure and macro names?
HansUp
If you do a decompile and then don't recompile, you don't know if you've solved the problem or not.
David-W-Fenton
A: 

What do you mean by Code Window?

This works:

Private Sub Form_Load()
       Debug.Print Format(123, "Currency")

End Sub
Jeff O