Prevent Form Close - ms-access

I am using Access 2007 and have the following code in the close event:
If [field1] = "" and [field2] = "" then
Msgbox " You must enter a value in either field 1 or field 2 "
Cancel = false
Else
Cancel = true
End If
I am trying to prevent the user from disregarding the message and closing the form via the close X button and saving the record. There have been some previous threads about this but I am unable to figure out the exact syntax.
Any assistance is greatly appreciated!

You probably must check for Null:
Cancel = IsNull([field1] & [field2])
If Cancel = True Then
Msgbox "You must enter a value in either field 1 or field 2."
End If

Related

MS Access - Validation rule for unbound text box

I have a Textbox2 in FormX which updates after I enter the value in Textbox1 using Afterupdate for Textbox1.
Textbox2 is unbounded text box.
Now I am trying to set up validation rule for the text in Textbox2. If Textbox2 contains word "Hold", it should pop up a message box saying "Invalid Entry".
Is it something possible? I could not find any reference online.
Can anyone please help?
Thank you,
Including code would be nice, but based on what you wrote, I would think you could add a check to your after update event in textbox 1 that occurs after the text is written to textbox 2 that will fire off the msgbox if it is invalid and then clear the entry in textbox 1.
Private Sub Textbox1_AfterUpdate()
If Textbox1.Value = "abc" Then
Textbox2.Value = "hold"
End If
If Textbox2.Value = "hold" Then
MsgBox "Invalid Entry", vbOKOnly, "Wrong!"
Textbox1.Value = ""
End If
End Sub

Set a description field to be required if checkbox is checked "Yes" in an Access Form

In my form I have a "Decline/Accept Contract" check box field and if this field is checked then I need the next field (long text) "Decline Reason" to be required. This field is only required is "Yes" is selected. I have tried variations of the following in the Before/AfterUpdate properties in both fields but nothing seems to work.
If Me.Decline_Accept_Contract = "Yes" And IsNull(Me.Decline_Reason) Then
MsgBox "Decline reason is required."
Cancel = True
Me.Decline_Reason.SetFocus
End If
I also tried setting a validation rule in the table properties
[Decline/Accept Reason]="Yes" but this just gave me the validation message regardless of which field was clicked. It was not actually validating on the "yes" in the Decline/Accept Reason.
Any help provided is appreciated. Thank you.
The textbox should be Null when empty, thus:
If Me!Decline_Accept_Contract.Value = True And IsNull(Me!Decline_Reason.Value) Then
MsgBox "Decline reason is required."
Cancel = True
Me!Decline_Reason.SetFocus
End If
Or you can use Nz:
If Me!Decline_Accept_Contract.Value = True And Nz(Me!Decline_Reason.Value) = "" Then
MsgBox "Decline reason is required."
Cancel = True
Me!Decline_Reason.SetFocus
End If
The Checkbox will have a default value of True or False, not string value of "Yes". Also, the textbox will be an empty string if text has been entered and deleted. best to check for both cases like this...
If Me.Decline_Accept_Contract And len(trim(Me.Decline_Reason.Value & vbnullstring))=0 Then
MsgBox "Decline reason is required."
Cancel = True
Me.Decline_Reason.SetFocus
End If

MS Access Msg box VBOKonly if number field data is greater than 1

I want Msgbox for user to remind something if user enters a number greater than 1 in a form Number field.Any help would be appreciated
If you want to prevent the user from entering a number > 1, set "<=1" as validation rule for the field, and your Msgbox text as validation text.
Otherwise use the AfterUpdate event of the form field:
Private Sub myNumberField_AfterUpdate()
If Me.myNumberField > 1 Then
MsgBox "Hello user, I have a message for you!", vbInformation
End If
End Sub

Message box that confirms if a user wants to leave a form

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

VBA isnumber istext function problem

im trying to check if a number is a text or number in a banking system. If the value is incorrect it should erase and send an error message, but my code isnt working at all... can someone help me? here is the code:
Private Sub TextBox1_Change()
name = TextBox1
If Application.IsText(name) = True Then
IsText = True
Else
MsgBox "Insert only words"
Selection.name.Delete
End If
End Sub
I think you should be using name = TextBox1.Text instead. It's probably giving you an error on that line.
Also, you may want to wait until the user is finished typing the response, I believe the changed event will run each time a character is pressed. The AfterUpdate will run after you tab or click away from the textbox.
And really, instead of deleting the response (which I don't think will work), you should simply set the textbox blank back to an empty string.
Private Sub TextBox1_AfterUpdate()
If Not MyIsANumber(TextBox1.Text) Then
MsgBox ("It's Text")
Else
TextBox1.Text = ""
End If
End Sub
Function MyIsANumber(str As String) As Boolean
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then
MyIsANumber = True
Exit Function
End If
Next
MyIsANumber = False
End Function
I'm sure the code could be structured better, but there's something along the lines of what you need. In this case, it just wipes the textbox if there is a number entered. Obviously, things will need to be the opposite for your textbox that you want to contain a number.
Have you considered the vartype() function? https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function