By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

Msgbox problem

100+
P: 184
All

This is most likely a stupidly easy question, but my brain is mud (My first ever Access project I have been given is huge and complicated and learning as I go)

What I want to do is to have a message box prompt a user apon closing a form. With a YesNo option. If the user clicks yes, then the form is closed, if no then the form stays open.

I have tried Macros and VBA code for this. Also spend the last few days searching TheScripts and the Net, but not much luck.

Thanks

Martin
Nov 23 '06 #1
Share this Question
Share on Google+
8 Replies


nico5038
Expert 2.5K+
P: 3,072
Try this code behind a button:

IF msgbox ("Sure ?",vbYesNo) = vbYes then
docmd.quit
endif

This will close the database when the Yes is clicked, else nothing will happen.

Nic;o)
Nov 23 '06 #2

100+
P: 184
Howzit Nico5038

Thank you for the reply. I gave it a go and it works fine except that I only want to close/not close the form the user has open and not the whole database.

I will see if I can alter it slightly to do what I need to.

Thanks again

Martin
Nov 23 '06 #3

NeoPa
Expert Mod 15k+
P: 31,494
I would suggest putting the code in the OnClose event, but then I saw there was no Cancel parameter :(.
You should be able to use the following code though, to quit just the current form. I have it on a command button.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExit_Click()
  2.     Call DoCmd.Close(ObjectType:=acForm, ObjectName:=Name)
  3. End Sub
Nov 23 '06 #4

100+
P: 184
Hi NeoPa

Thanks for your reply. Right I have tested it and it does work, it just does not do what I am after. I need a message box to prompt the user if they are sure they want to close the form or not, so they basically have a Yes/No or OK/Cancel option.

Thanks

Martin
Nov 23 '06 #5

missinglinq
Expert 2.5K+
P: 3,532
Private Sub YourExitFormButton_Click()
If MsgBox("Are You Sure ?", vbYesNo) = vbYes Then
DoCmd.Close acForm, YourFormName
End If
End Sub
Nov 23 '06 #6

100+
P: 184
MissingLinq

That's the baby!!!! Thanks man, you just made my day.

Thanks a lot!!
Nov 23 '06 #7

NeoPa
Expert Mod 15k+
P: 31,494
Sorry,

I was responding after you saw Nico's post.
I meant to put that code (the one line) in place of the .Quit in his code rather than replace it completely.

No harm done, you have all you need :)
Nov 23 '06 #8

100+
P: 184
I have now, thank you very much.
Nov 27 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.