I have a drop down select list in a VBA form I would like to validate as soon the user clicks on it. It needs to check that a pre-requisite drop down has been filled already.
This is to avoid the user jumping ahead on the form because there are certain fields that need to be filled out first. My attempt so far is not working:
Private Sub cbo_moduleName_Click()
If Len(cbo_moduleCode.Value) = 0 Then
MsgBox ("Please select a module code")
Exit Sub
End If
End Sub
It seems the Click event is activated only when the box's value is changed with the mouse, not every time it is physically clicked on. Try this:
Private Sub cbo_moduleName_Enter()
If Len(cbo_moduleCode.Value) = 0 Then
MsgBox ("Please select a module code")
cbo_moduleCode.SetFocus
Exit Sub
End If
End Sub
Related
I added two facilities to a form. When I try to run it, it says no selection is made.
If I select "all", it runs. I can't run SMC or SCC, the two additions to the form, individually.
I created the two facility clicks by copying SGV and WHT and renaming.
I edited the coding to run SMC and SCC, that's why it works when I select all.
Private Sub OkayCommandButton_Click()
' Determine which facility is selected
Dim oCtl As MSForms.Control
On Error Resume Next
For Each oCtl In Me.SelectFacFrame.Controls
If TypeName(oCtl) = "OptionButton" And oCtl.Value = True Then
facOption = oCtl.Caption
Exit For
End If
Next oCtl
Debug.Print Err.Number
If Err.Number = 438 Then
MsgBox "No facility selection made. Try again."
Unload Me
End
End If
On Error GoTo 0
Unload Me
End Sub
An Option Button control has both a Name property and a Caption property. When you copied and pasted an old Option Button, you probably updated the Caption property but not the Name property. Your selection logic is based on the Caption property, but your action logic might use the Name property. Check the Name property for the two buttons you copied.
I have created a userform in Excel which will populate a database. However, there is a combobox in my userform where the user selects a specific rate from a list of three. When one specific rate is selected, I need to show an error message immediately. I have other error messages that will show when the user selects to 'Submit' the form, but I need this one to populate as soon as the value is selected. I have corrected my other coding issue so this should not be related to that.
The code that I have so far is here:
Private Sub cmbMileageRates_Change()
If cmbMileageRates.Value = ".58" Then
MsgBox "When requesting mileage reimbursement of the high rate($.58/mile), Division Administrator Approval is required", 0, "Critical"
Exit Sub
End If
End Sub
Why don't you use event "cmbMileageReimbRates_change"?
Private Sub cmbMileageReimbRates_Change()
If cmbMileageReimbRates.Value = ".58" Then
MsgBox "When requesting mileage reimbursment of the high rate ($.58/mile), Division
Administrator Approval is required", vbCritical
Exit Sub
End If
End Sub
You need to fire your code when the form is 'dirty'.
The Dirty event occurs when the contents of the specified control changes.
There is a good example of this on the Microsoft Website:
https://learn.microsoft.com/en-us/office/vba/api/access.combobox.dirty
Private Sub Form_Dirty()
If Me.Dirty Then
If cmbMileageReimbRates.value = ".58" Then
MsgBox "When requesting mileage reimbursment of the high rate ($.58/mile), Division Administrator Approval is required", vbCritical
'if you want to clear the value too, use:
cmbMileageReimbRates.Value = Null
Exit Sub
End If
Else
'Carry on
End If
End Sub
Hopefully this is a simple one for somebody out there. I have an Excel VBA UserForm with a number of text boxes for the entry of parameters. In addition, there is an image on the userform. As the user clicks into a textbox, the image on the UserForm is changed by triggering the _Enter() event to show an image of the parameter in question. This works fine and is not a problem. However, as the user exits the textbox, I want the image to revert back to the original image. I've tried the Events for _Exit() and _AfterUpdate() to make this work and although it works fine if I update the parameter, it doesn't work if I don't update the parameter. In essence, the image changes as I enter the text box but doesn't change as I leave unless I update the value in the text box.
Does anybody have any ideas why this is so and what I might be able to do about it?
Update (with code as requested). I've now figured out that the textbox Exit event is not being triggered because I have more than one frame on my UserForm and I am clicking on a control in a different frame. I've made the following example.
and the following code;
Option Explicit
Dim TextBox1Data As Variant, TextBox2Data As Variant
Private Sub TextBox1_AfterUpdate()
TextBox1Data = TextBox1.Value
Debug.Print "AfterUpdate Textbox 1"
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit Textbox 1"
End Sub
Private Sub TextBox2_AfterUpdate()
TextBox2Data = TextBox2.Value
Debug.Print "AfterUpdate Textbox 2"
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit Textbox 2"
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Array("Item 1", "Item 2", "Item 3", "Item 4")
ComboBox1.Text = ComboBox1.List(1)
End Sub
If I click into TextBox1 and then click into either ComboBox1 or CheckBox1, the Exit event for TextBox1 is triggered. However, if I click into TextBox1 and then click into either CheckBox2 or CheckBox3, the event is not triggered. I assume that this is because the CheckBoxes are in a different frame. Is this the case? Is there a way of triggering an Exit event for a TextBox when moving from one frame to another?
It seems like this is a known problem - see Microsoft Q210734. Rather than use the Set Focus method suggested by MS, I've now added an Event for exiting the Frame where the textboxes are located, e.g. using my example code, I added the following;
Private Sub Frame1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exiting Frame"
End Sub
What happens now is that when moving from one control to another within Frame1, the normal Exit event for the control works as expected and when moving from Frame1 to Frame2, the Exit event for the frame works.
Hope this helps someone in the same predicament.
I am creating a data entry userform that calculates loads (e.g.pipeloads, wind loads etc..). There are important text boxes that cannot be left unfilled. If the user clicks the command "Add Input" and there are some text boxes left unfilled, I want an error to be generated that forces the user to enter a value.
I've got as far as generating an error for one text box. This code will trigger an error alert when you try to leave the text box without filling it first.
Private Sub txtdist_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(txtdist.Value) = "" And Me.Visible Then
MsgBox "Required Entry!", vbCritical, "Error"
Cancel = True
txtdist.BackColor = vbRed
Else
txtdist.BackColor = &HC0FFFF
End If
End Sub
What I'm trying to achieve is that when I click "Continue" command button, then the program will see if any textbox or combobox is left empty to force the user to enter a value. (Similar to when you try to buy online with credit card, the page will not submit if there is not name or credit card number etc...). Thank you.
Maybe you can try this
Private Sub Continue_click()
Dim c as Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
If Trim(c.Value) = "" Then
MsgBox "Please fill out all required information", vbInformation
Exit Sub
End If
End If
Next c
End Sub
Since you want the code to check for empty textboxes when you click "Continue" Command Button, you should add your check code in that Sub.
Here's an example on how you can do it:
Sub Continue_click()
'...
If Trim(txtdist.Value) = "" Then
MsgBox "Please fill out all required information", vbInformation
Exit Sub
End If
'...
End Sub
Hope this helps.
I have a spreadsheet that I want a message box to pop up if a particular cell is "OH". Once the message pops up I want to be able to click "OK" and move forward.
Currently I have:
Private Sub OH_MSG_Click()
If ThisWorkbook.Worksheets("Zip Code Entry").Range("State").Value = "OH" Then
MsgBox "Please review OH Underwriting Guidelines."
End If
End Sub
This works if I run in manually, but how can I get it to run automatically as soon as State = OH?
Open Microsoft Visual Basic for Apllications in you excel file.
Right click of you Worksheet in Pojekt view (on left).
Choose Code View. Now will the code window appear.
From the top left dropdow nmenu of the Code window choose "Worksheet".
From the right drop down menu choose "Change".
Then place you code (something like this) Example:
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Address = "$A$2") Then
If (Range("A2").Value = "OH") Then
MsgBox "Some message"
End If
End If
End Sub
That means: If A2 is Selected AND any cell in this worksheet hat changed, the macro will check If("A2").Value = "OH") If true will the Msg box Appear if false nothing happend.