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

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*

Related

How to access Form.Textbox.Text property when the source query returns no results?

I am making a continuous form for searching that self-updates as the user types in multiple search boxes.
The code works up until the user types in parameters that don't correspond to any records.
The query then can't find anything and I get the error
"Can't use the property or method when the control doesn't have the focus"
I could not figure out what element of the form is doing it. Adding the .SetFocus to the textbox control didn't help.
Any ideas on how to either
Set focus to the textbox again and prevent it from being lost, or
Figure out what is stealing the focus and disable it?
The following sub is called in the textbox_Change sub. I added a workaround.:
Private Sub RefreshTB(textbox As Control)
'This is to prevent Acces from removing trailing spaces
'If the textbox isn't empty and there is a space at the end, don't requery. This preserves trailing spaces as Access trims them on Me.requery
If Len(textbox.Text) <> 0 And InStr(Len(textbox.Text), textbox.Text, " ", vbTextCompare) Then
Exit Sub
End If
'If the last character isn't a space, requery on change to show new results of the query
Me.Requery
'The workaround: If the query returns no results, detect that, warn the user and clear search box. Requery to show some results again.
If DCount("*", "DatasetsFilterQ") = 0 Then
If MsgBox("No results found. The last TextBox you searched in will be cleared.", vbOKOnly, "No Records") = vbOK Then
textbox = ""
Me.Requery
End If
Exit Sub
End If
textbox.SetFocus
textbox.SelStart = Len(Nz(textbox.Text))
End Sub
I tried a filter but ran into the same error when passing .text value to the function.
You can't set SelStart to Null, so avoid that with Nz:
textbox.SelStart = Len(Nz(textbox.Value))

Check cell = 0 if it does display message box and cancel save

I am having an issue with some VBA code which seems so basic but yet is just not working. I am new to VBA so possible that I am missing something.
The code is supposed to check a cell (XFD3002) to see if it equals 0, if it does then display a message and stop the file from saving. For background, there is a check to see that certain cells are filled and if not it will return a 0.
The code is:
Private Sub Check_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim check As Integer
check = Sheets("Data").Range("XFD3002").Value
If check = 0 Then
MsgBox ("You have missed one or more required field")
Cancel = True
End If
End Sub
Can anyone see something wrong with the above?
The sub name should be Workbook_BeforeSave.
The hardest to find errors are always the simplest ones. :D
You're seeing if what is in the check cell is a string, since you've put quotation marks around it - "0", but check has been declared as an integer. Try changing it to just 0 without the quotation marks.
EDIT: Please also ensure that this code appears in "TheWorkbook" module and change the name to Workbook_BeforeSave (as pointed out by #Pierre)

Continue Through An If Statement If False

I'm trying to create a simple ignore list option for my program. When a sub runs, I want it to not execute some code if a user is on the list and a checkbox is checked.
I have tried this:
If Not My.Settings.IgnoredNames.Contains(PartnerDisplayName) AndAlso chkIgnore.Checked = False) Then
'Do something...
End If
This works, except even when the check box is checked by itself the if statement doesn't continue. I don't have a clue how to rewrite it.
This will not work either and I don't really know why. The sub triggers on an certain API event, and exiting it like so still lets the real code go through.
If My.Settings.IgnoredNames.Contains(ParterDisplayName) And chkIgnore.Checked = True Then Exit Sub
'Do something...
Instead of using Nots and creating messy logic, just invert your If statement and short circuit out if you hit the condition where you don't want to run the code:
If My.Settings.IgnoredNames.Contains(PartnerDisplayName) AndAlso chkIgnore.Checked = True Then
Return
End If
' Do your other logic here.

Programming VBA in an Outlook form

I created my own Outlook form to use it as standard surface to enter certain orders instead of the normal message form. The creation, editing and sending works perfectly fine and in the next step I want to insert some code via VBA.
My problem is that I can´t access the objects of my form in the VBA editor. E.g. I want to show a message box when a certain checkbox is checked. According code would be:
Sub example()
If CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
When I run the code I get the error that the object could not be found. The same goes for every other object, like textboxes or labels etc.
I guess the solution is pretty simple, like putting Item. or sth. like that in front of each object. But so far I wasn't able to find the solution.
I´m using Outlook 2010.
I know this is a year too late but you'll want to do something like this example below. It's kinda a work around but you can get whatever value was selected.
Sub ComboBox1_Click()
Set objPage = Item.GetInspector.ModifiedFormPages("Message")
Set Control = objPage.Controls("ComboBox1")
MsgBox "The value in the " & Control.Name & _
"control has changed to " & Control.Value & "."
End Sub
You should be able to get the value, just get a handle on the object you want using the Inspector
The following is an excerpt from here
When you use a custom form, Outlook only supports the Click event for
controls. This is a natural choice for buttons but not optimal for
controls like the combo box. You write the code by inserting it into a
form’s VBScript editor. You need to have the Outlook form open in the
Form Designer and click the View Code button found in the Form group
of the Developer tab.
Sub CheckBox1_Click()
msgbox "Hello World"
End Sub
The code page is fairly minimal with no syntax highlighting. I just tried this now and it does work. Dont forget to Publish your form to pick up the new changes.
I know this is almost 6 years late but, in VB and VBA, simply start with the form name. (And if that doesn't work, just keep going up a parent object and you'll get there.) So, your code becomes:
Sub example()
If MYFORMNAME.CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
Of course, after typing "MYFORMNAME." you'll know if it will work because typomatic will kick in when the system recognizes "MYFORMNAME" after you hit the period.

Microsoft Access VBA pop up alert date approaching

I am trying to write some VBA in Microsof Access (if VBA is the way to go?). What I need is a pop up message alerting someone that a deployment is happening within the next week.
My table is called Tasks_List and there is a field called Deployment_Date.
What I think I need is to put together an OnLoad for the initial form. It would check today's date and check through Deployoment_Date and show a pop up if any deployments are happening within the next week. The pop up should show what deployments are happening e.g. Initiating_System, Deployment_Date and Description.
Thank you in advance, I've hit a brick wall on this. I'll post what I've tried but I have no VBA knowledge and it is pretty bad.
What I tried:
Private Sub Report_Open(Cancel As Integer)
Dim varX As Variant
varX = DLookup(Tasks_List.[Deployment_Date]< Now - 20)
If varX > 0 Then GoTo line2
line1: msgbox "Deployment approacing for: "
line2:
End Sub
EDIT: After help below I have created a query and form for this. Using Dcount:
Private Sub Detail_OnLoad()
Deploy = DCount("*", "Tasks_List_Popup_Query")
If Deploy <> 0 Then
DoCmd.OpenForm "Tasks_List_Popup_Query_Form"
DoCmd.GoToRecord , , acNewRec
End If
End Sub
You should not need any VBA. Create a query that selects the relevant records and create a form based on the query. You can use DCount to ensure that there are records before you launch the form, which would take a little VBA.
SELECT * FROM Tasks_List WHERE [Deployment_Date]< (Date - 20)
For the DCount:
Deploy = DCount("*","TheQuery")