Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to enforce field val requirement with VbYesNo msgbox not working 1

Status
Not open for further replies.

RobertIngles

Technical User
Jan 20, 2011
113
CA
The first part of the code works fine. If the user clicks Yes, they are returned to the form with focus on the proper field.

If the user clicks No, they are also returned to the form with focus on the field. I want the form to close without saving if they select No:

Option Compare Database
Option Explicit

Private Sub SaveandCloseButton_Click()

If IsNull([EncryptionStatus]) Then

MsgBox "Encryption Status Cannot be Null. Press OK To Return To Continue With Profile Creation or Cancel to Exit Without Saving", vbYesNo

If vbYes Then

Me.EncryptionStatus.SetFocus

Else
If vbNo Then

DoCmd.Close , "", acSaveNo

End If
End If
End If

End Sub

Thanks for your help!
 

Your message box may be confusing your users.
You tell them to press OK or CANCEL and provide YES and NO buttons.

I'd try this:
Code:
If IsNull(EncryptionStatus) Then
    If MsgBox("Encryption Status cannot be null. Select YES to continue with profile creation. Select NO to cancel",vbYesNo) = vbYes Then
        Me.EncryptionStatus.SetFocus
    Else
        DoCmd.Close,,acSaveNo
    End If
End If


Randy
 

Isn’t that a nice way to confuse the user.

You have a message box with Yes and No buttons to click on, but your message box asks the user to click either on OK button (“Press OK To Return...”) or Cancel button (“Cancel to Exit...”). But there are no OK or Cancel buttons.....


Have fun.

---- Andy
 
Thanks for the help (and the sarcasm...lol) The reason that the two Yes/No ok/cancel don't match was that I was playing with both methods to decide if one was more appropiate to use than the other - didn't change the msg box description/contents each time I tried them - having said that, point taken and thank you for your help!

Robert
 
This line has nothing to do with your data:
Code:
 DoCmd.Close,,acSaveNo
The acSaveNo saves or discards changes to the form design properties, not the data. You may want to try undo the data changes with
Code:
   DoCmd.RunCommand acCmdUndo

Duane
Hook'D on Access
MS Access MVP
 
Still a no go, click yes in the msgbox and the form just closes. Click No, and the form closes.

what do you think I have done wrong??

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(EncryptionStatus) Then
If MsgBox("Encryption Status cannot be null. Select YES to continue with profile creation. Select NO to cancel", vbYesNo) = vbYes Then
Me.EncryptionStatus.SetFocus

Else

DoCmd.RunCommand acCmdUndo

Would it have anything to do with a conflict between this code and my close button? I thought that putting the code in the beforeupdate form property it would supercede the close button but maybe I'm wrong.



End If

End If


End Sub
 
How are ya RobertIngles . . .

Perhaps this:
Code:
[blue]   Dim DL As String
   
   DL = vbNewLine & vbNewLine
   
   If Trim(Me.EncryptionStatus & "") = "" Then
      If MsgBox("Encryption Status must have an entry!" & DL & _
                 "Press OK To Return To Continue With Profile Creation" & _
                 "Press Cancel to Exit Without Saving", _
                 vbOKCancel) = vbOK Then
         Me.EncryptionStatus.SetFocus
         Exit Sub
      Else
         Me.Undo
      End If
   Else
   DoCmd.RunCommand acCmdSaveRecord
   End If
   
   DoCmd.Close[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
RobertIngles . . .

There is a formatting error (on my part). When displayed the MsgBox should have three lines. See whats missing in [red]red[/red]:
Code:
[blue]   Dim DL As String
   
   DL = vbNewLine & vbNewLine
   
   If Trim(Me.EncryptionStatus & "") = "" Then
      If MsgBox("Encryption Status must have an entry!" & DL & _
                 "Press OK To Return To Continue With Profile Creation" [red][b]& DL[/b][/red] & _
                 "Press Cancel to Exit Without Saving", _
                 vbOKCancel) = vbOK Then
         Me.EncryptionStatus.SetFocus
         Exit Sub
      Else
         Me.Undo
      End If
   Else
      DoCmd.RunCommand acCmdSaveRecord
   End If
   
   DoCmd.Close
End Sub[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top