Closing a form from a current event (1 Viewer)

ypma

Registered User.
Local time
Today, 19:45
Joined
Apr 13, 2012
Messages
643
Greetings fellow access users. i am trying to close a form, from the on current event . i have the following code.
#If Me.NoFuthercontactdesired = "Never" Then
MsgBox " This client has requested not to be contacted."
'DoCmd.Close objectType:=acForm, ObjectName:=Me.Name

Exit Sub

Forms("Main Switchboard").SetFocus
End If#

The above does not close the current form as i am in event mode. Is there a work around ? i tried a public function , but that also causes an error .
I hope i have provided enough info for you to see what i am trying to achieve if more details are require i will provide . Any suggestion would be appreciated .

Regards Ypma
 

Ranman256

Well-known member
Local time
Today, 14:45
Joined
Apr 9, 2015
Messages
4,339
Once you close the form, all code vanishes,
So
Open main menu, THEN close the form.
 

isladogs

MVP / VIP
Local time
Today, 19:45
Joined
Jan 14, 2017
Messages
18,209
Once you close the form, all code vanishes,
So
Open main menu, THEN close the form.

Sorry to disagree, but actually that's not true.
You can close the form then open the main menu ... or vice versa

The problem is the placing of the Exit Sub line.
Any code after that line will not be executed
 

ypma

Registered User.
Local time
Today, 19:45
Joined
Apr 13, 2012
Messages
643
Ridderes and Ranman256 thank you very much for you prompt response. however i now know that any thing after the exit sub will not be executed senior moment , but back to my main problem , in that i cannot close the current form using the close form code within the On current event. please advise as to where my code is in error

Regards Ypma
 

isladogs

MVP / VIP
Local time
Today, 19:45
Joined
Jan 14, 2017
Messages
18,209
I can honestly say I've NEVER tried to close a form from the form current event.
As you say trying to do so triggers error 2585

I've tried error handling to ignore that error - failed - form stays open
I tried running cmdClose_Click event from Form_Current - failed - error 2585
I tried opening the other form & adding code to its Form_Load event to close the first form - failed with same error 2585

Offhand, I don't have a solution for you other than don't use that event.
Perhaps I'm missing something obvious, but for now, sorry I can't help
 

Beetle

Duly Registered Boozer
Local time
Today, 12:45
Joined
Apr 30, 2011
Messages
1,808
I'm not sure I follow the logic here. You have a form that is apparently used for managing client contacts, and as the user moves through the records on that form, if they encounter one where the client has elected not to be contacted, you just want to force a close on the form? Seems like this would make things more difficult for the users of your application. Why not just;

1) Exclude those clients front the record source to begin with, or...

2) Disable certain controls on the form based on that field (i.e. so they can't send an email, or whatever they normally do with the form).
 

ypma

Registered User.
Local time
Today, 19:45
Joined
Apr 13, 2012
Messages
643
Ridders thanks for your time. Beetle, restricting clients who don't wish to to be contacted from the data source may be my best bet. The saga begins from the switchboard when users are trawling for old clients who's mortgages iare coming to an end, this is achieved by using a search key on the switchboard hence my code. i will have to create a report for those clients who have asked not to be contacted and change their minds, can be re-instated .

Thanks again Ypma
 

missinglinq

AWF VIP
Local time
Today, 14:45
Joined
Jun 20, 2003
Messages
6,423
As has been suggested, it simply makes little sense to close a Form from the OnCurrent event, and hence the Access Gnomes won't allow it. If you move to a Record where

Me.NoFuthercontactdesired = "Never"

the Form closes, not allowing the user to proceed to a Record where

Me.NoFuthercontactdesired <> "Never"

might be true.

Also, if the first Record that is displayed meets the criteria of Me.NoFuthercontactdesired = "Never" you'd never be able to do anything, as the Form would always close immediately on opening!

The only way around this would be to open the Form to a particular Record.

As to the code beyond the Exit Sub line...it doesn't really matter, as, per Access Help, a Form can only receive the focus if it doesn't have any visible, enabled controls, which seems unlikely in a Form named "Main Switchboard."

Linq ;0)>
 

Users who are viewing this thread

Top Bottom