Results 1 to 7 of 7
  1. #1
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182

    Dialogue Box Yes/No before clearing a field


    I have a button on my form to clear several fields. When people click it, I want it to ask: "Are you sure you want to clear data - Yes/No". If the user says yes, it runs the code. If the user says no, then nothing happens

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Code:
    Private Sub MyButton_Click
    
    If MsgBox("Are you sure you want to clear data - Yes/No", vbQuestion+vbYesNo+vbDefaultButton2, "Clear Data?") = vbNo Then Exit Sub
    
    'YOUR CODE GOES HERE
    
    End Sub
    NOTE: vbDefaultButton2 sets the default button to 'No'.
    If you prefer it to be Yes, change to vbDefaultButton1 or omit that part

  3. #3
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Thank you! Also, thanks for mentioned the default button thing.

  4. #4
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You should learn how to assign the message box function result to a variable in case you ever need more than 2 buttons (like Yes/No/Cancel). While you could write code that repeats the entire message box construct 3 times, this would be better IMHO:
    Code:
    Dim result As Integer
    
    result = Msgbox ("Some message requiring 3 choices",vbYesNoCancel)
    If result = 6 Then
      do yes stuff
    ElseIf result = 7 Then
      do no stuff
    Else
      do cancel stuff
    End If
    You can use vb constants or integers - the effect is the same even though result is dim'd as an integer. Note that parentheses for the function must not be used unless the function is used to return a result (you can tell by the fact that it is set to = something)
    Here's more on the options https://www.techonthenet.com/access/...msgbox_ret.php
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Hi micron
    In this case there was no need for alternative outcomes hence just the one line of code.

  6. #6
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I know that:
    in case you ever need more than 2 buttons
    Is there something wrong with trying to teach somebody something?

  7. #7
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Quote Originally Posted by Micron View Post
    I know that: Is there something wrong with trying to teach somebody something?
    Sorry ....
    By chance, I'd just written an answer at StackOverflow where I did much the same thing.
    The moderators at SO deleted my post as according to them it
    wasn't answering the question in hand

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Do not display print dialogue box
    By Philw in forum Macros
    Replies: 1
    Last Post: 06-30-2014, 05:00 PM
  2. Clearing a field with conditions
    By Ruegen in forum Forms
    Replies: 8
    Last Post: 11-05-2013, 04:28 PM
  3. Replies: 7
    Last Post: 07-07-2013, 11:11 AM
  4. Clearing a Date field
    By chris.williams in forum Programming
    Replies: 8
    Last Post: 08-09-2012, 06:04 PM
  5. SaveAs Dialogue box
    By saqqer in forum Programming
    Replies: 8
    Last Post: 09-10-2009, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums