After update setfocus not working Access VBA - vba

In the amount field, which will give a message if the amount becomes too much after giving input, it is still working. But the amount is not right, it has become too much, it is giving the message that the amount is not focusing again in the field. Please help.
Private Sub Amount_AfterUpdate()
If Not IsNull(Me.[InvoiceRef].Column(3)) And Not IsNull(Me.Amount) Then
If CInt(Me.[Amount]) > CInt(Me.[InvoiceRef].Column(3)) Then
MsgBox "You have given input in excess of the amount due.", vbExclamation, "Receipt Amount"
Me.Amount.SetFocus
End If
End If
End Sub

Related

Erratic combobox comparison in Access VBA

I have set the row source to the same table for two different unbound comboboxes, cbo_MaxCost and cbo_MinCost, on a form. These comboboxes are meant to specify the minimum and maximum of a range of values for each record in another table. To ensure they do, I've set up code in each combobox's AfterUpdate event to check that
cbo_MaxCost >= cbo_MinCost
or warn the user with an error.
The problem is, this check performs erratically. For example, sometimes Access VBA will evaluate
21 > 11
as False and other times as True. Usage of the dot and exclamation point notations makes no difference, nor does using "Me." or not, or ".Value" or not. Stepping through it shows that Access registers the proper values from the comboboxes during the comparison. This behavior seems to happen more often when I select a much higher or lower value than was previously in one of the boxes. I can't figure out what causes it.
This is the code:
Private Sub cbo_MaxCost_AfterUpdate()
If cbo_MaxCost >= cbo_MinCost Then
MsgBox ("Access is calculating correctly.")
Else
MsgBox ("The maximum cost should be higher than the minimum.")
End If
End Sub
Private Sub cbo_MinCost_AfterUpdate()
If cbo_MaxCost >= cbo_MinCost Then
MsgBox ("Access is calculating correctly.")
Else
MsgBox ("The maximum cost should be higher than the minimum.")
End If
End Sub
Have in mind, that a combobox/listbox always returns text, so convert to numeric before the comparison:
CCur(Me!cbo_MaxCost.Value) >= CCur(Me!cbo_MinCost.Value)

Can I make some fields on an Access form conditional?

I am creating a database and I have a table where I am collecting if a patient has been or is currently on specific medications.
I have a list of 17 medications that we care about and have a yes/no checkbox for each one. If a patient is or has ever been on one of these medications we want to collect 9 additional fields.
I want to create a form that lists only the medications and checkboxes. When the user checks a checkbox I then want the additional fields to appear for them to fill out.
Most patients will have only been on 2-5 of these medications, so I don’t want to clutter the form with unnecessary blank fields.
Is there a way to do this without VBA? If no, will someone give me an example of what the VBA code should look like?
Thanks so much! We collect this data as part of an ongoing registry monitoring the long term safety of a specific research medication. We usually collect this data differently when the patient is in clinic however, in lite of the pandemic we need to do this via telephone and this database will be crucial in ensuring the continued collection of this vital data!!
Thanks,
Allen
What you can do is to rely on the fact that Yes/No fields are stored as 0 (False) and non-0 (True). You can then have a small piece of VBA code behind the form that adds all of the Yes/No fields to determine whether to display the additional text boxes or not:
Sub sCheckMedication()
On Error GoTo E_Handle
If (Me!Medication1 + Me!Medication2 + Me!Medication3) <> 0 Then
Me!txtMedicationNotes.Visible = True
Else
Me!txtMedicationNotes.Visible = False
End If
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbclf & "sCheckMedication", vbOKOnly + vbCritical
Resume sExit
End Sub
You would then call this procedure in the Form's Current event (to cover when the form is first loaded, and also when navigating between records), and also on the CheckBox's Click event:
Private Sub chkMedication1_Click()
Call sCheckMedication
End Sub
Regards,

BeforeUpdate Access VBA in form field can only enter number

I have a form where I want either Asset or Location filled out and Supervisor or Lead or Crew or Work Group or Crew Work Group. I have the code and it works but it only allows me to enter numbers into these fields. I want to be able to enter numbers and letters into these fields.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Form_frmAddPM.Asset Or Form_frmAddPM.Location) Then
MsgBox "Please enter a value in an asset or location."
Cancel = True
End If
If IsNull(Form_frmAddPM.Supervisor Or Form_frmAddPM.Lead Or
Form_frmAddPM.Crew Or Form_frmAddPM.Work_Group Or
Form_frmAddPM.Crew_Work_Group) Then
MsgBox "Please enter value in Supervisor, Lead, Crew, Work Group, or
Crew
Work Group."
Cancel = True
End If
End Sub
I have tried to change integer to different types. With the code above when I try to enter letters into the form a mismatch type error pops up.
Thank you in advance for your help.
You're using the bitwise OR operator, which requires both values you use it on to be an integer (see the docs, VB.Net but works the same in VBA)
Instead, you should check for each field if it's null separately:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Form_frmAddPM.Asset) Or IsNull(Form_frmAddPM.Location) Then
MsgBox "Please enter a value in an asset or location."
Cancel = True
End If
If IsNull(Form_frmAddPM.Supervisor) Or IsNull(Form_frmAddPM.Lead) Or
IsNull(Form_frmAddPM.Crew) Or IsNull(Form_frmAddPM.Work_Group) Or
IsNull(Form_frmAddPM.Crew_Work_Group) Then
MsgBox "Please enter value in Supervisor, Lead, Crew, Work Group, or
Crew
Work Group."
Cancel = True
End If
End Sub

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*

How to prevent negative integers and blank inputs using DataError event in vb.net

I have a datagridview bound to a dataset. My goal for my datagridview is to prevent the user from entering negative integers and leaving the datagridviewcell blank. I wish to have some sort of error message or message-box to tell the user that their entry is invalid. Here is what I have so far, hopefully it can give you a starting point. I greatly appreciate any help or suggestions you may give.
Private Sub DataGridView1_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError
If CInt(e.Exception.Message, "Input string was not in a correct format.") < 0 Then
MessageBox.Show("Please Enter a positive Value")
'This will change the number back to original
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = " "
End If
Have a look at the documentation for DataGridView's CellValidating event: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx
Attaching a handler for this event allows you to check whether the new value is:
empty
not a number
a negative number
(in that order) and cancel the change if any one of those conditions are true.
EDIT
I'm not sure if this is a coincidence, but you can just copy and paste the example code from the documentation. It does exactly what you're trying to do.