I have a piece of code to make sure the user enters a "scrap amount" if they have entered a "scrap code". When I apply and use the code for Scrap1, everything works great. When I try to apply that same code with the next set of variable names, I get an error that says my code "has an ambiguous name detected. I have checked my variable names and the code until my eyes bled. Everything looks fine to me. Does anyone see an error that I have missed?
'check to see that there is a scrap amount if a code has been entered #1.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ScrapCodes1.Value Then
If Me.ScrapAmount1 = 0 Then
Cancel = True
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
End If
End If
End Sub
'check to see that there is a scrap amount if a code has been entered #2.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ScrapCodes2.Value Then
If Me.ScrapAmount2 = 0 Then
Cancel = True
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
End If
End If
End Sub
Yes, assemble in one sub:
Private Sub Form_BeforeUpdate(Cancel As Integer)
' check to see that there is a scrap amount if a code has been entered #1.
If Me.ScrapCodes1.Value Then
If Me.ScrapAmount1 = 0 Then
Cancel = True
End If
End If
' check to see that there is a scrap amount if a code has been entered #2.
If Me.ScrapCodes2.Value Then
If Me.ScrapAmount2 = 0 Then
Cancel = True
End If
End If
If Cancel = True Then
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
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 have a form that gets updated from a web page scrapping.
One of the entries is missing and so I use a value from a textbox to do a vlookup to get the missing info and update the empty textbox.
None of the event methods fire automatically!
Private Sub envelope_afterupdate()
On Error Resume Next
MsgBox ("test")
name_env.text = Application.WorksheetFunction.VLookup(envelope.text, Worksheets("DATA_name").Range("a1:b334"), 2, False)
If Err.Number <> 0 Then
' MsgBox "currName not found" ''optional, no need to do anything
End If
End Sub
Any suggestions?
I simply put the vlookup in my data extraction code!
Form1.name_env = Application.WorksheetFunction.VLookup(form1.envelope, Worksheets("DATA_name").Range("a1:b334"), 2, False)
Pete
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
I have this simple Access Form that when you fill out the form and forget to fill out the Business Unit field, a msgBox will pop up telling you so and setFocus to that very Combo box. If it is Not null, I want to call the next feature. The first part of this code works, but the when it is notNull it wil not carry on.
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then
MsgBox "Please Select a Business Unit!", vbOKOnly
Exit Sub
End If
Me.Combo_BU_Selector.SetFocus
Exit Sub
If Not IsNull(Me.BU_Selected_Add) Then
Call Add_RPS_LINE
End If
End Sub
Does anybody see where I am totally out in left field?
You've got an extra Exit Sub (the one after the first MsgBox) that stops your code from doing what you want. Also, your first End If is in the wrong location.
Try something like this instead:
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then ' No business unit
MsgBox "Please Select a Business Unit!", vbOKOnly ' Tell user
Me.Combo_BU_Selector.SetFocus ' Focus the control
Exit Sub ' Exit the method
End If ' End the IsNull test
Call Add_RPS_LINE ' You only get here if the above doesn't execute
End Sub
It helps if you learn to properly indent your code to match If and End If visually, so you can see where they line up (match) and where they don't. :-)
If you correct the indentation of your code to:
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then
MsgBox "Please Select a Business Unit!", vbOKOnly
Exit Sub
End If
Me.Combo_BU_Selector.SetFocus
Exit Sub
If Not IsNull(Me.BU_Selected_Add) Then
Call Add_RPS_LINE
End If
End Sub
You can clearly see that the Exit Sub in the middle will terminate the function before it gets to Call Add_RPS_LINE.
If you look at the two If statements you have, you can see that they are almost the same and so an Else is in order, resulting in this simpler and more readable code.
Private Sub Image_AddNon_RPS_Button_Click()
If IsNull(Me.BU_Selected_Add) Then
MsgBox "Please Select a Business Unit!", vbOKOnly
Me.Combo_BU_Selector.SetFocus
Else
Call Add_RPS_LINE
End If
End Sub
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.