I have a single record form with contract details and a subform where I enter some additional information. The main form has a command button to close it.
I need to check 2 controls on closing(one in main form and one in subform) and if some conditions are met a Yes/No MsgBox should appear. If the user presses "Yes", the form closes, if he presses "No" the form remains open.
So far I have this:
Private Sub Form_Unload(Cancel As Integer)
Dim Response As Integer
Response = MsgBox("It looks like this contract is fixed. Would you like to edit the final price?", vbYesNo, "Database Information")
If Me![fixed price] = 0 And Me![Fixation Orders Subform1].Form!Text38 = "Final Fixed Price" Then
Cancel = True And Response
If Response = vbYes Then
Cancel = True
Else
Cancel = False
End If
Else
Cancel = False
End If
End Sub
Problems: Before I defined "response" the MsgBox appeared correctly but the form closed anyway. After I defined it to use it in the second "If":
The message box appears no matter what
The form closes either I press "Yes" or "No"
Additionally, after I solve this, on which event should I put this code to chack the conditions also when I go to next record? Thanks in advance.
I believe it should be something like this:
Private Sub Form_Unload(Cancel As Integer)
If Me![fixed price] = 0 And Me![Fixation Orders Subform1].Form!Text38 = "Final Fixed Price" Then
If MsgBox("It looks like this contract is fixed. Would you like to edit the final price?", vbYesNo, "Database Information") = vbYes Then
Cancel = True
End If
End If
End Sub
Related
I have a textBox formatted as "Short Date". When I put invalid data in field, for example random "dfsdf", and try to change focus, form throws validation Error 2113.
My goal is to give an opportunity to user to close form by click on "Cancel" button without any problem, because no matter what he entered in Date textbox while form is canceled.
I can handle this error and disable message with Form_Error event, but focus stays set to date textBox anyway, when i try to click Cancel button, and button is never clicked.
I use a setup, probably similar to yours:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const InvalidDate As Long = 2113
Select Case DataErr
Case InvalidDate
MsgBox "Invalid date!"
Response = acDataErrContinue
' Cancel invalid entry.
Me.Undo
' DoCmd.Close
Case Else
Response = acDataErrDisplay
End Select
End Sub
Problem is, that this will fire before your button gets focus. This means, that the form doesn't even know what you clicked, only that it was somewhere outside the date textbox.
Thus, the DoCmd.Close would close the form for whatever was clicked.
You may turn it into an advantage, that the user is able to watch the removal of the faulty input ...
You should use the after update event to check if the user entered a valid date,
if not then clear or undo. Without knowing the controls of your form i can't tell if you allow users to change table values or if you are using transactions.
pseudo code:
Private Sub Text1_AfterUpdate()
If Check_If_Date Then
Execute_Code
Else
End If
End Sub
Private Function Check_If_Date() as Boolean
If IsDate(Text1) then
Check_If_Date = True
Else
Check_If_Date = False
Select Case msgbox("Not a valid date", vbOKCancel)
case vbOK
reset_value
case vbCancel
Close_Form
case else
End Select
End If
End Sub
Private Sub Reset_Value()
'need to clear value or undo
End Sub
Public Sub Execute_Code()
'Code to save values or allow Update to complete
End Sub
Private Sub Close_Form()
'Code For Closing Form and logging errors
End Sub
I split up repeatable actions for reuse in case the same thing can be repeated in another text box for example a second date or to get the same behavior for the other controls.
I had a piece of VBA running, and for some reason it has suddenly stopped working.
On my form unload event I have the code:
if isnull(me.field) then
ans=MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
If ans=vbNo then
Cancel=True
end if
end if
This worked for a couple months, when the user exited the warning message would appear, and if they select no the form would not exit. Now when I click no I get an error:
Run time Error 3270. Property not Found
I changed the code to:
if isnull(me.field) then
ans=MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
If ans=vbNo then
docmd.cancelevent
end if
end if
Now I get error:
Runtime Error '2001' You Canceled The Previous Operation
Which is what I want.
How do I get a messagebox to confirm that a user wants to exit a form?
Edit: I realize that the exit warning works when I exit the form by pressing x in the upper right, but when I exit using a button with the docmd.close I get the errors. Any way around that?
Simply use the button click event.
Where CommandButton14 is the name of your exit button.
EDIT for users comment.
Have your exit button call the UserForm_QueryClose event.
Private Sub CommandButton14_Click()
'UserForm_QueryClose 0, 0
Unload Me
End Sub
Ask your question in that event. If they say yes end the app or unload the form. If they say no, cancel.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim Response As Integer
' Displays a message box with the yes and no options.
Response = MsgBox("Warning you have not entered all the data. Do you want to exit the form?", vbYesNo)
' If statement to check if the yes button was selected.
If Response = vbYes Then
'Cancel = False
End
Else
Cancel = True
End If
End Sub
You can take everything out of your unload event.
I feel like I am missing something because the path you chose seems a little over complicated.
Firstly, with my database I always make sure to set all of my forms Close Button property to "No" this way you always have control of when the user closes the form.
So from that point you just need this code attached to your close button:
Private Sub btnClose_Click()
Dim blnClose As Boolean
Dim strResponse As String
'Default to true so it always closes unless one or more future checks fail
blnClose = True
If IsNull(Me.Field) Then
strResponse = MsgBox("Warning you have not entered all the data. Do you want to exit the form", vbYesNo, "Error")
'User wants to cancel close toggle Boolean to false
If strResponse = vbNo Then
blnClose = False
End If
End If
'If nothing has toggled to false then close the form
If blnClose = True Then
DoCmd.Close , ""
End If
End Sub
Nw to the forum and may as well be new to programming.
I would like to make 'Invoice No.' text box required, ONLY when 'INVOICED' is selected in my combobox.
If your form is bound, you could add the validation in the before update event like this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.cboType = "INVOICED" And Nz(Me.InvoiceNo) = vbNullString Then
MsgBox "Invoice No. is required!", vbExclamation, Me.Caption
Me.InvoiceNo.SetFocus
Cancel = True
End If
End Sub
If unbound form, add the above statements (except Cancel = True) to your button click event.
I am trying to revert back to the original value if, at the MsgBox prompt, the user chooses "No". However, this is not happening. Can you please have a look to see where I am going wrong?
Private Sub txtNov2_AfterUpdate()
Dim x As Integer
Dim NewValue As Integer
Dim OrigValue As Integer
NewValue = Me.txtNov2.Value
OrigValue = [Forms]![DataEntry]![txtNov2].OldValue 'Me.txtNov2.OldValue
If NewValue <> OrigValue Then
x = MsgBox("Value Has Changed Do you Want to Update?", vbQuestion + vbYesNo, "Value Change")
If x = vbYes Then
MsgBox ("Please Press Update Button")
btnUpdateData2.SetFocus
Else
txtNov2.SetFocus
'Me.Undo
txtNov2.Value = OrigValue
'Cancel = True
End If
End If
End Sub
It appears to me that your code is working, as far as it goes. I open the form...
...and change the value from 123 to 124...
...then when I hit the [Tab] key so the control loses the focus I get the MsgBox...
...and if I choose "No" then the value reverts to 123
The only difference between that state and the initial state (when I first opened the form) is that the form is still "dirty", as indicated by the "pencil" icon in the record selector on the left side of the form. If you want to completely undo all changes to the form then try replacing...
Else
txtNov2.SetFocus
'Me.Undo
txtNov2.Value = OrigValue
'Cancel = True
End If
...with...
Else
DoCmd.RunCommand acCmdUndo
End If
I'm sure this is very simple, but I can't find it. In the close event of an Access Form, how can I cancel closing the form? I have a test that counts the records in a table. If that table has records, I want to ask the user if they want to close or go back and work with them. So how do I cancel the close event?
You can use the Unload event:
GlobalVar ButtonClicked
Private Sub Form_Open(Cancel As Integer)
ButtonClicked = False
End Sub
Private ClickMe_Click(Cancel As Integer)
ButtonClicked = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not ButtonClicked Then
Cancel = True
End if
End Sub
Order of events for database objects
Study and try this code, it worked for me. Replace necessary variable names with your chosen names. Paste the code in the form_unload Event of your form.
WARNING!!!: After you perform this operation you will find it difficult to access your form in design and layout view
Private Sub Form_Unload(Cancel As Integer)
userresponse = MsgBox("Are you sure you want close? All your work wouldn't be saved", vbYesNo, "Database Information")
Select Case userresponse
Case 6
Cancel = False
'this line opens another form in my own case
DoCmd.OpenForm "EngMenu"
Case 7
Cancel = True
'this line keeps my own form open in my own case
DoCmd.OpenForm "UpdateForm"
Case Else:
MsgBox "You are not allowed to perform this operation", vbInformation, "Database Information"
End Select
End Subenter code here
Use the "Form_BeforeUpdate(cancel as integer)" event and set cancel to True.
Notice that you simply will not be able to close at all unless you add some logic to actually allow updating the database.