Access VBA - Msgbox - vba

Looking for a workaround here. One that preferably doesn't involve making a bunch of user forms.
Background: I've got an Access DB used for scanning items in and out of the office. After each item is scanned, it verifies that the item is okay to leave the office. If it is not, a msgbox is called and says "This item is out of calibration, are you sure you want to take it?" (vbyesno)
Issue: When an item is scanned, the system sees the numbers from the bar code and a carriage return. This works great with the scanning form. However, if the user isn't paying attention when a msgbox appears and scans the next item, the carriage return will click the default value from the msgbox, close it and the user is none the wiser.
My temp solution: I added a third button (cancel) after the yes/no that becomes the new default value. Scanning a new item will click that and it just reopens the msgbox. Therefore the user should eventually notice. It's not really a good solution though.
Dim ans As Integer
'2 is vbCancel
ans = 2
Do While ans = 2
ans = MsgBox("Test", vbYesNoCancel + vbDefaultButton3 + vbSystemModal + vbExclamation)
if ans = 2 then
'Restart Loop
elseif ans = vbYes
'Do something
elseif ans = vbNo
'Do something else
end if
Loop
There are many different cases in which a msgbox can appear, so making an individual user form each one is something I'd hope to avoid.
Thanks,
Brock

Force the user to enter a value like 'Yes' or a checkbox (form).

Related

MS Access 2019: How do I check for duplicate records before update, then run a specific procedure if duplicates are found?

I am very new to both StackOverflow and to doing any sort of advanced programming in MS Access. I created a database to catalog my trading card collection (Excel just wasn't cutting it since we're talking about over 2000 unique cards). At first it was just a simple table of records, but now it's turned into a full-fledged database that I have search forms for and queries and everything.
What I'm trying to do right now is streamline my process a bit, and there is something very specific that I want to make Access do. I'm almost certain that whatever I want to do will have to be done in VBA, and I'm just not familiar enough with it to do what I want.
What I want it to do is this: Any time a new record is entered, I want it to check the record before it saves the record into the DB (I'm fairly confident that I need to use the "Before Update" event for this) and make sure that the "Sort ID" field (an auto-calculated field I've created) contains no duplicates (I know I'll most likely have to use queries for this since auto-calculated fields can't be indexed). If the program detects a duplicate, I want it to produce a message box saying that I'm trying to enter a duplicate record and ask me if I want to update the "number owned" field of the existing record instead of creating a new one, and then take me to the record in question on an affirmative response.
What I currently have is a validation rule that uses an index (comprised of the fields that generate the Sort ID), which generates a custom error message by using the following VBA code in the "On Error" event:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conErrRequiredData = 3022
If DataErr = conErrRequiredData Then
MsgBox ("Duplicate Sort ID. Please update the 'number owned' field on the existing record instead.")
Response = acDataErrContinue
Else
Response = acDataErrDisplay
End If
End Sub
This code works exactly as it should, but I want more than a pop-up error that I can't do anything with. I have a query entitled "CheckDuplicateSortID" that I created using the Query Wizard, and it checks the "Sort ID" field for duplicates, but that's as far as I've managed to get. The example on This site is about the closest I've managed to find to what I'm looking for, but the code sample given is very difficult for me to understand because there's very little explanation with it; I'm not familiar enough with Access VBA to know which parts are important code and which parts are his specific field names and other variables; I haven't gotten any error messages because I'm stumped on even trying to figure out what needs to be changed from that sample code and what it needs to be changed to.
Edit: Just for the sake of clarification, the solution doesn't have to involve the Sort ID field. I created that so I'd have one field I can point the program to. But if it would be simpler to just use the index that I use for my current validation rules (with the error message generated by the above code), I'm open to that too.
After some considerable finagling, I actually managed to find a solution to this on my own, though it is probably needlessly complicated (I'm definitely open to simpler solutions, if anyone has any).
Rather than using the actual SortID field, I modified the code in the OP to this:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Dim strMsg As String
Dim iResponse As Integer
'The text to be displayed in the message prompt.
strMsg = "Unable to save record. The values you have entered would generate a duplicate." & Chr(10)
strMsg = strMsg & "Would you like to clear this form and edit the existing record instead?"
'Calls for the yes/no message prompt specifically when the no-duplicate
'validation rule is violated (error 3022).
Const conErrRequiredData = 3022
If DataErr = conErrRequiredData Then
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Invalid Sort ID")
Response = iResponse
If iResponse = vbYes Then
'Calls a custom function that opens the record in question for editing.
UpdateOnError
Else
'Cancels the operation on a negative response and does not clear the form.
Cancel = True
End If
Else
Response = acDataErrDisplay
End If
End Sub
As you can see from the above code, the event calls a custom function, which is coded as follows:
Function UpdateOnError()
On Error GoTo UpdateOnError_Err
Dim UpdateGoToID As Variant
'Selects the Sort ID in question for the purpose of opening the existing record.
UpdateGoToID = Forms![Card List Entry Form]!txtSortID
'Clears the invalid form.
DoCmd.RunCommand acCmdUndo
'Opens the existing record the user attempted to duplicate.
DoCmd.OpenForm "Card List Entry Form", acNormal, "", "[Sort ID]=" & "'" & UpdateGoToID & "'", , acNormal
UpdateOnError_Exit:
Exit Function
UpdateOnError_Err:
MsgBox Error$
Resume UpdateOnError_Exit
End Function
Like I said, this is probably a lot more steps than actually needed, but it does work as I want it to: when the user attempts to enter a duplicate record, an error message pops up asking if they would like to update the existing record. If yes, it takes them to the existing record. If no, it closes the error message without clearing the form or saving the record.

how to check if an item in combobox is already in the listbox?

I am working on a seat booking form that asks the user for their full name(textbox) and seat of choice(combobox) before a button is pressed to confirm the booking. If name was not entered, a msgbox comes up asking them to input name. If the seat of choice is already in a listbox that shows all bookings so far, a msgbox comes up to choose another seat as it has already been reserved. The seat options in the combobox are named A1, A2 etc.
When I run the code the check for name is successful. However, even if the seat of choice is already in the listbox, the prompt to choose another seat does not show up. What am I doing wrong?
Dim buyers(0) As person
buyers(0).firstname = firstnamebox.Text
buyers(0).surname = lastnamebox.Text
buyers(0).seatchoice = seatlist.SelectedItem
If firstnamebox.Text.Trim.Length = 0 Then
MsgBox("please include a name")
ElseIf lastnamebox.Text.Trim.Length = 0 Then
MsgBox("please include full name")
ElseIf bookingdetails.Items.Contains(seatlist.SelectedItem) Then
MsgBox("This seat is already taken please pick another")
Else
bookingdetails.Items.Add(buyers(0).getfullname & " , " & buyers(0).seatchoice)
End If
I expect a msgbox to pop up saying "This seat is already taken please pick another" if the bookingdetails listbox already contains the selected item in the combobox. But it seems to just ignore it...
I suspect that there could be some spelling/case issues with your procedure. The Contains method is case sensitive, and you should at least be converting source and target strings ToUpper and perhaps trim for good measure. Ultimately, though, I think you are giving to much credit to the end user not to make a mistake.
here's an example
Dim LstBxItms() As String = ListBox1.Items.Cast(Of Object).Select(Function(obj) ListBox1.GetItemText(obj).ToUpper.Trim).ToArray
If LstBxItms.Contains(TextBox1.Text.ToUpper.Trim) Then
MsgBox("Found match")
End If

Error catching a form in Access using VBA

So i have a form that's updating a table in Microsoft Access. I'm using a BeforeUpdate event call to stop the form from updating if the 'first name' hasn't been filled in.
I basically only want the form to be saved once everything has been filled in, and then switch to see the updated table (the bit under If not cancel).
Currently the popup box to say 'You must enter a First Name' correctly fires, the next pop up box asking if the user wants to undo any changes fires. But then a pop up box saying 'No current record' appears and then the view changes to the table. I'm not sure why this is all happening.
Option Compare Database
Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = False
' perform data validation
If IsNull(Me.customer_f_name) Then
MsgBox "You must enter a First Name.", vbCritical, "Data entry error..."
Me.customer_f_name.BorderColor = vbRed
DoCmd.GoToControl "customer_f_name"
Cancel = True
End If
If Not Cancel Then
DoCmd.SelectObject acTable, "ticket_tracker_table"
DoCmd.Requery
DoCmd.GoToRecord acDataTable, "ticket_tracker_table", acLast
End If
' if the save has been canceled or did not pass the validation , then ask to Undo changes
If Cancel Then
If MsgBox("Do you want to undo any changes?", vbYesNo, "Confirm") = vbYes Then
Me.Undo
End If
End If
End Sub
Review https://learn.microsoft.com/en-us/office/vba/api/Access.Form.BeforeUpdate(even)
Instead of GoToControl, use SetFocus.
I did a very simple test with the Undo question and it does work:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.customer_f_name) Then
MsgBox "Must enter customer name."
Cancel = True
If MsgBox("Do you want to undo edits?", vbYesNo) = vbYes Then Me.Undo
Me.customer_f_name.SetFocus
End If
End Sub
Suggest you put table code in AfterUpdate event.
so to supplement J7, with some big picture view: "basically only want the form to be saved once everything has been filled in" …. that is swimming against the tide; which is why you are having to write a lot of code. The product enters data as you go fundamentally. that is it's core design.
it is very normal to do field checks as you go - typically in the AfterUpdate event, the check code is applied and if invalid - generate a message box and invoke an Undo plus reset the focus back to the field they were attempting to leave.
A totally different design, in some cases it may justify a temp local duplicate table just for the purposes of holding form input data - - this approach allows a very elaborate check(s) against data in the production database; where upon if valid an Append Query then writes the data into the permanent table.

My VBA code is not reacting when a condition has been met

I am using VBA in Microsoft Access. I want the code to run before the user completes each entry of "downtime". I am trying to make sure that the value of a box is not a negative number. The box actually holds a formula. I don't know if that matters but, I thought I would mention that. I want to check the result of the calculation (the value that is showing in that box) and if it is less than 0, I want a MsgBox to pop up. My code is doing nothing. No error, no pop-up, no warnings.
Here is my code.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Me.RunningTotal.Value < 0) Then
MsgBox (RunningTotal & "Please check your downtime.")
Cancel = True
End If
End Sub
I have tried using the "RunningTotal" in brackets as well with no luck. I have also tried beforeupdat as well as afterupdate.
I got it to work. I had to put the code in the user's data entry box AND as an "after update" since the calculation wouldn't happen until after the entry has been made. Thank you, Darren, for your help. :) Here is the code that works.
*Private Sub MinutesDown_AfterUpdate()
If (Me.RunningTotal < 0) Then
MsgBox (RunningTotal & "Please check your downtime.")
Cancel = True
End If
End Sub*

Enable/Disable Fields with certain criteria MS Access

I have a form in MS Access that I'm trying to create for insurance claims. I have all the fields that I need to be filled in but what I'd like to be able to do is enable or disable those fields depending on certain actions of the users. So the flow of the form is like this: I have a Frame at the top with two radio buttons, one for a single-claim incident and one for a multi-claim incident. If the user clicks the single-claim button everything continues with no problem. If the user clicks the multi-claim incident button, a combo box appears to the side with a dropdown list of MultiClaim_Incident_ID numbers that they need to select from. What I'm trying to do is if the user selects the Multi-Claim Incident button AND does not select an Incident ID number from the dropdown down list (i.e. leaves it at default value) then the rest of the form is disabled until corrected as well as clear all the fields...
It seems like it should be pretty straightforward but I can't seem to get it to work, I'm not sure if my logic is flawed or what. Here's an abridged version of my VBA code:
Private Sub Form_Load()
Me.SM_Frame.Value = 1
Me.MultiClaim_Drpdwn.Value = Null
End Sub
Private Sub SM_Frame_AfterUpdate()
If SM_Frame.Value = 1 Then
Me.MultiClaim_Incident_ID_Label.Visible = False
Me.MultiClaim_Drpdwn.Visible = False
ElseIf SM_Frame.Value = 2 Then
Me.MultiClaim_Incident_ID_Label.Visible = True
Me.MultiClaim_Drpdwn.Visible = True
ElseIf SM_Frame.Value = 2 & MultiClaim_Drpdwn.Value = Null Then
Me.Incident_Date = Null
Me.Incident_Date.Enabled = False
Me.Claimant_Name.Value = ""
Me.Claimant_Name.Enabled = False
//PATTERN CONTINUES FOR REST OF FIELDS//
MsgBox ("CLEAR EVERYTHING!!")
ElseIf SM_Frame.Value = 1 Then
Me.Incident_Date.Value = ""
Me.Incident_Date.Enabled = True
Me.Claimant_Name.Value = ""
Me.Claimant_Name.Enabled = True
//PATTERN CONTINUES FOR REST OF FIELDS//
MsgBox ("Everything can continue as is")
End If
End Sub
Let me just say that getting sequences like these right is NOT straightforward AT ALL! So don't feel bad about not getting it right on the first try. I have to create things like that about once a month and still need a lot of tries until it works in all situations.
Try to seperate to concerns:
You already have SM_Frame_AfterUpdate(). Do in it what you must to handle changing from Value 1 to 2, namely making the Combobox visible, but STOP there. You dont know what will happen to the fields with information from SM_Frame alone, you need to wait for MultiClaim_Drpdwn. Also, do what is needed to go from 2 to 1, namely hide the Combobox.
Next, create an AfterUpdate-handler MultiClaim_Drpdwn_AfterUpdate(). Use THAT to deal with the fields (enable/disable, set empty) according to its value.
Once you have that in place, you only have some edge cases remaining. For example, you want the fields to behave like MultiClaim_Drpdwn_AfterUpdate() states right after you change the SM_Frame. Thats easy once you understand that you can just happily CALL MultiClaim_Drpdwn_AfterUpdate() from within SM_Frame_AfterUpdate(), best done at the very end. These eventhandlers are still just normal functions, already public and available for anyone. This will make things chain nicely when you come from the radiobutton or not when you come from the Combobox.
In an "elseif" series, once a condition is true, the rest is ignored.
So, your
ElseIf SM_Frame.Value = 2 & MultiClaim_Drpdwn.Value = Null Then
is never reached, because you've got
ElseIf SM_Frame.Value = 2 Then
before.
In the same idea, the "ElseIf SM_Frame.Value = 1 Then" after the MsgBox is totally useless, because it's hidden by "If SM_Frame.Value = 1 Then"
Try to use the step-by-step debug mode to see that.