PDA

View Full Version : Error handling Macro



gsanghvi20
07-30-2008, 09:42 AM
Hey guys,
I have a text box and a button. Based on the value the text box has, the onClick function of the button performs a task. Now all this is coded usong a macro and not VBA. I want the onClick function to give an error if the value in the Text box is Null. For that I have written a code in the Macro.

Condition > Action > Arguments
-----------------------------------------------------------------------
IsNull([Forms]![frmViewManager]![ViewGoTo]) > MsgBox > Please select
a Manager Name.

My question is how do I write an else statement, coz this constitutes just the If statement.

Thanks,
gsanghvi20

CreganTur
07-30-2008, 10:13 AM
It's really simple:
If...Condition...Then
ElseIF...Condition...Then
Else
End If

So for you it would look something like:
If IsNull(Me.txtBoxName) Then
MsgBox "Please select a manager name."
Exit Sub '<<<This stops any more code from executing
Else
'other steps to complete
End If

Now, you don't have to use the Else statement if you're evaluating a single thing. You could get away with:
If IsNull(Me.txtBoxName) Then
MsgBox "Please select a manager name."
Exit Sub '<<<This stops any more code from executing
End If

The reason for this is that if the textbox value is Null, then the Exit Sub statement will exit the subroutine and no other code will be run. If the textbox is not null, then the If statement will evaluate to False and it will jump down to the code after the End If.


NinjaEdithttp://img293.imageshack.us/img293/9060/ninja3od8.gif: Where did all those double posts come from :bug:

CreganTur
07-30-2008, 10:13 AM
*Deleted Double Post*

CreganTur
07-30-2008, 10:14 AM
*Deleted Double Post*

CreganTur
07-30-2008, 10:14 AM
*Deleted Double Post*

gsanghvi20
07-30-2008, 01:45 PM
Well, I wanted to program it in using a Macro Builder rather than the code builder because there are many tasks which are done if the condition is false (ie the condition IsNull([Forms]![frmViewManager]![ViewGoTo]) = False) which are programmed using Macro Builder. I dont know how to write these conditions using VBA code hence I wanted to figure out the how to write the If..Else loop using the Macro Builder. I hope I am making sense.

Thanks,
gsanghvi20

CreganTur
07-30-2008, 01:59 PM
Macros are only... okay for doing basic things. If you want to go beyond the basics then VBA is your best bet.

You can see the VBA code for your macros by opening a macro, and then File -> Save As -> Module

This will create a new module that does exactly the same thing as your macro. It's an easy way to learn the rudiments of VBA, because you know what the Macro does- now you just get to see the code that accomplishes the same thing!

Although you'll be seeing a bloated version of that code- code that Access generates itself is always bloated.

gsanghvi20
07-31-2008, 12:57 PM
When I say File -> Save As, the only option I get is to save as a Macro. I dont get any option as module :(

CreganTur
07-31-2008, 01:22 PM
On the Macros tab of your database screen you need to click on the Macro you want to convert to Code (Or you can open the macro in design view)

After you click Save As, you should see a box like the one pictured below. Select Module to have it convert to Code.

I'm using Access 2003. If you're using an older version, then that could be the problem.

gsanghvi20
07-31-2008, 01:35 PM
I am actually using 2007, but it gives me only Macro. Is there any other way?

CreganTur
08-01-2008, 05:37 AM
I am actually using 2007, but it gives me only Macro. Is there any other way?

I googled "convert macro to module" and everything I read referenced the Save As -> Module method... try googling this and see if you can find something different.

Carl A
08-02-2008, 02:10 PM
In Access 2007 you have to click on the office logo and Click Save As and then select Save Object As.

Right clicking in design mode only gives you the option to save as a macro.

HTH