Connecting Tech Pros Worldwide Forums | Help | Site Map

Action on Error Condition with MACROS

John Baker
Guest
 
Posts: n/a
#1: Nov 12 '05
Hi:

I have a MACRO and as one step in it I am opening a form, which is based on a query. The
query calls for entry of a portion of an associates name as the selection criteria for
records. Using the form : Like [ Enter first letters of Associates Name: ] & "*"

The snag is that the box that comes up offers the opportunity to click on "Cancel". When
the user clicks on "Cancel" (intending to stop the process) the system appears to close
out totally. In other words, there is no program response to the cancel command. With a
macro, I would like to be able to intercept the Cancel call, and close the macro without
any further action. Is there some other way to do this, or have I got myself in a hopeless
snarl? I tried putting a close on no data macro in the form, but that does not appear to
do the job either.

Best

John Baker

Allen Browne
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Action on Error Condition with MACROS


Macros have no error handling.

As you found, they are only useful for a *very* limited range of things.

Looks like you have outgrown them, and it's time to dip your feet into VBA
where error handling is part of every procedure you write.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John Baker" <Baker.JH@Verizon.net> wrote in message
news:an5n90lb79210lgmbu959boup3to2sj5k8@4ax.com...[color=blue]
>
> I have a MACRO and as one step in it I am opening a form, which is based[/color]
on a query. The[color=blue]
> query calls for entry of a portion of an associates name as the selection[/color]
criteria for[color=blue]
> records. Using the form : Like [ Enter first letters of Associates[/color]
Name: ] & "*"[color=blue]
>
> The snag is that the box that comes up offers the opportunity to click on[/color]
"Cancel". When[color=blue]
> the user clicks on "Cancel" (intending to stop the process) the system[/color]
appears to close[color=blue]
> out totally. In other words, there is no program response to the cancel[/color]
command. With a[color=blue]
> macro, I would like to be able to intercept the Cancel call, and close the[/color]
macro without[color=blue]
> any further action. Is there some other way to do this, or have I got[/color]
myself in a hopeless[color=blue]
> snarl? I tried putting a close on no data macro in the form, but that does[/color]
not appear to[color=blue]
> do the job either.
>
> Best
>
> John Baker[/color]


John Baker
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Action on Error Condition with MACROS


Thanks.

Unfortunately my VBA sills are very very limited.

How would I open a form (based on a query), check for an error and then close the form if
there was an error,or return to the main macro?

If you could give me a simple outline of the commands required to do this it would be a
great help.

Thanks

John baker

"Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote:
[color=blue]
>Macros have no error handling.
>
>As you found, they are only useful for a *very* limited range of things.
>
>Looks like you have outgrown them, and it's time to dip your feet into VBA
>where error handling is part of every procedure you write.[/color]

Allen Browne
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Action on Error Condition with MACROS


In the Database window, choose the Modules tab, and click New.

In the new module, create a function by typing:
Function WhateverYouWantToCallIt()
Access adds
End Function

Between the 2 lines, enter:
DoCmd.OpenForm "Form1"
Include the quotes. Replace Form1 with the name of your form.

To call the function from the macro, use the RunCode action. That's the
basics.


Here's a more comprehensive example.
Save the code below into your module.

In your macro, choose the StopMacro action, and put this beside it in the
Condition column (View menu if you don't see a Condition column):
Not FormOpened("Form1")
This will cause the macro to stop if the form did not open; otherwise it
continues to the next line.

For more information about error handling, see:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html

----------code starts-----------
Function FormOpened(strForm As String) As Boolean
On Error GoTo Err_Handler
'Argument: Name of the form to open
'Return: True if the form opened.

DoCmd.OpenForm strForm
FormOpened = True

Exit_Handler:
Exit Function

Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & " - " & Err.Description
End If
Resume Exit_Handler
End Function
----------code ends-----------

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"John Baker" <Baker.JH@Verizon.net> wrote in message
news:ntnn90pmuvdq6c1hb5sktlsk7bndbjka6e@4ax.com...[color=blue]
> Thanks.
>
> Unfortunately my VBA sills are very very limited.
>
> How would I open a form (based on a query), check for an error and then[/color]
close the form if[color=blue]
> there was an error,or return to the main macro?
>
> If you could give me a simple outline of the commands required to do this[/color]
it would be a[color=blue]
> great help.
>
> Thanks
>
> John baker
>
> "Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote:
>[color=green]
> >Macros have no error handling.
> >
> >As you found, they are only useful for a *very* limited range of things.
> >
> >Looks like you have outgrown them, and it's time to dip your feet into[/color][/color]
VBA[color=blue][color=green]
> >where error handling is part of every procedure you write.[/color]
>[/color]


John Baker
Guest
 
Posts: n/a
#5: Nov 12 '05

re: Action on Error Condition with MACROS


Allen:

Thank you very much indeed. This was most helpful.

Regards

John Baker

"Allen Browne" <AllenBrowne@SeeSig.Invalid> wrote:
[color=blue]
>In the Database window, choose the Modules tab, and click New.
>
>In the new module, create a function by typing:
> Function WhateverYouWantToCallIt()
>Access adds
> End Function
>
>Between the 2 lines, enter:
> DoCmd.OpenForm "Form1"
>Include the quotes. Replace Form1 with the name of your form.
>
>To call the function from the macro, use the RunCode action. That's the
>basics.
>
>
>Here's a more comprehensive example.
>Save the code below into your module.
>
>In your macro, choose the StopMacro action, and put this beside it in the
>Condition column (View menu if you don't see a Condition column):
> Not FormOpened("Form1")
>This will cause the macro to stop if the form did not open; otherwise it
>continues to the next line.
>
>For more information about error handling, see:
> Error Handling in VBA
>at:
> http://allenbrowne.com/ser-23a.html
>
>----------code starts-----------
>Function FormOpened(strForm As String) As Boolean
>On Error GoTo Err_Handler
> 'Argument: Name of the form to open
> 'Return: True if the form opened.
>
> DoCmd.OpenForm strForm
> FormOpened = True
>
>Exit_Handler:
> Exit Function
>
>Err_Handler:
> If Err.Number <> 2501 Then
> MsgBox "Error " & Err.Number & " - " & Err.Description
> End If
> Resume Exit_Handler
>End Function
>----------code ends-----------[/color]

Closed Thread