I have the following code that I thought would open a MsgBox warning if there is more than one selection made in ListBoxProjects.
However it opens the MsgBox if there is only one selection as well. It doesn't open the MsgBox if there are no selections. Bit stuck on this one, any help gratefully received.
If Me.ListBoxProjects.ListIndex > 1 Then
MsgBox "You can only edit Projects 1 at a time", vbExclamation, "Project Editing"
End If
ListIndex property returns the position of the selected item.
To achieve what you want you need you need to access the ItemSelected collection's Count property.
If ListBoxProjects.ItemsSelected.Count > 1 Then
MsgBox "You can only edit Projects 1 at a time", vbExclamation, "Project Editing"
End If
Use the Selected property array:
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
' do something
End If
Next i
Related
I have the following two codes on a button: The (first code) aims to submit value of option button into worksheet cell:
For Each FormControl In Me.Controls
'Check only OptionButtons
If TypeName(FormControl) = "OptionButton" Then
'Check the status of the OptionButton
If FormControl.Value = True Then
'Set a variable equal to the Caption of the selected OptionButton
OptionButtonValue = FormControl.Caption
'We found the selected OptionButton so exit the loop.
Exit For
End If
End If
Next
'Store input in the worksheet
Sheets("Answer Sheet").Range("E80").Value = OptionButtonValue
To ensure an option button is selected before proceeding to next form, i have
the 'following code (second code):
Dim cnt As Integer
For Each ctl In Me.Controls
If TypeName(ctl) = "OptionButton" Then
If ctl.Value = True Then cnt = cnt + 1
End If
Next ctl
If cnt = 0 Then MsgBox "Hello " & CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you
have not selected an answer! Please select an answer to proceed to next question. Thank you.",
vbInformation, "Please select an answer!" Else ScoreBoards.Show
Unload Me
MY CHALLENGES
Both codes above exists in my forms i.e. questions 1, 2, 3,...respectively, but can't seem to get the second code (that, which ensures an option button is selected before next form can be opened) to work by adding 'unload me' to the end of it, yet i want the form closed before proceeding to next. Adding 'unload me', pop-up the msgbox (which tells me to select an answer) but when i clicked okay on the msgbox, it closes the form (Question1) instead of returning me to same form to ensure an answer is clicked, then proceed to next form (Question2). However, when i remove the 'unload me', things work fine i.e. the msgbox popup when selection not made, returns to same form when okay on msgbox is clicked, and opens next form when selection made.
What i really want is: i want the second code above (which ensures an option button is selected before next form can be opened) to work as programmed and each form closed before proceeding to the next form.
Thank you in advance
PS:
The concept summary is:
On a userform (Question1), select an option button and submit the value to worksheet
Ensure an option button is selected:
if selected and button clicked, the next form(being Question2) should open
if not selected and button clicked, the msgbox (which tells me to select an answer), should popup
clicking okay on the msgbox, should return me to same form (Question1) so that i can select an option and proceed.
Try adapting your second code in the next way, please:
If cnt = 0 Then MsgBox "Hello " & _
CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you have not selected an answer! Please select an answer to proceed to next question. Thank you.", vbInformation, "Please select an answer!"
Exit For
Else
ScoreBoards.Show
Unload Me
End if
I have a userform with a listbox displaying all items within a table.
the listbox has a double click event where;
if the row double clicked contains text it opens an edit form.
if the row double clicked is empty then a message box displays stating "the item is not valid for editing."
I'm looking to add a new feature to this where if a certain cell along the row contains "closed" a message box displays stating "this item is closed and not valid for editing.
I'm not very good at VBA and I'd appreciated any help that can be offered. below is the current code I have.
Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Checks if the selected row is empty and outputs a message box if it is
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) = 0 Then _
MsgBox "The selected item is empty and not a valid entry for editing"
'Checks if the selected row is closed and outputs a message box if it is
If RiskLogReviewListBox.Column(11, 0) = "closed" Then _
MsgBox "The selected item is closed and not a valid entry for editing"
'Checks if the selected row is populated
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) > 0 Then
Thanks in advance :)
found a solution
Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long
'Checks if the selected row is empty and outputs a message box if it is
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) = 0 Then _
MsgBox "The selected item is empty and not a valid entry for editing"
'Checks if the selected row is closed and outputs a message box if it is
If (RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 10)) = "closed" Then
MsgBox "The selected item is closed and not a valid entry for editing"
Exit Sub
Else
'Checks if the selected row is populated
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) > 0 Then
Replace
If RiskLogReviewListBox.Column(11, 0) = "closed" Then _
with
If Instr(RiskLogReviewListBox.Column(11, 0), "closed") > 0 Then _
But, I suppose that your code must exist in case of the two above situation. So, I would also suggest using of Exit Sub after the messages...
I open a form as shown here: How to use combobox to open specific form in Ms Access
But I still have a problem. When I click the button, it opens two forms immediately. What I want to do, when the value in the combobox is 1, is to open only form 1, and when the value in the combobox is 2, open form 2.
Should I correct something in my macro? How do I do it?
I personally don't like this macro builder.
Here's a proper way to do it with VBA.
Select your combobox and go in the events. In the After Update event, remove your macro code and add VBA code instead, using the option "Code Builder"
Your code should be this :
Private Sub ComboBoxColor_AfterUpdate()
On Error GoTo Err_Handler
Dim strForm As String
Select Case ComboBoxColor.Value
Case "Color1"
strForm = "Form1"
Case "Color2"
strForm = "Form2"
Case "Color3"
strForm = "Form3"
Case Else
MsgBox "I don't know what to do with this combobox value"
GoTo Exit_Sub
End Select
DoCmd.OpenForm strForm, acNormal
Exit_Sub:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Sub
End Sub
Adapt ComboBoxColor with the proper name, and Form1/Form2/Form3 with the names of your forms of course (and the Color1/Color2/color3 if needed)
I have an MS-Word 2013 document with several (legacy) formfields; some text boxes, some comboboxes. The first list item on all of the comboboxes is "(select one)" to let the end user know they need to make a selection (I did not draft or design this document, I've just been asked to write the VBA code for it). So I coded each to give a simple VBA message box, ran on exit, if that first selection was not changed, for example:
Public factor1 As Integer
Sub MyFormFieldFactor1()
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
Exit Sub
End If
If ActiveDocument.FormFields("cbofactor1").Result = "Yes" Then
factor1 = 1
Else
factor1 = 0
End If
End Sub
The word document automatically goes to the next formfield when you click ok on the message box. Through VBA, I want it to stay on the current formfield when "(select one)" is chosen. Bonus points if it stays on the current field and pulls up the list selection automatically.
Will this work:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
Exit Sub
End If
You can auto drop the list with something like:
SendKeys "%{down}", True
DoEvents
Full code:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
SendKeys "%{down}", True
DoEvents
Exit Sub
End If
Is there a way to limit the number of selections a user can choose on a ListBox with MultiSelect enabled in Access 2003? Right now I have a procedure that fires on the On Click event that checks the number of choices selected and if it is over my threshold it will display a warning label.
Give this a try. It basically deselects the last item selected if its over the predefined limit:
Private Sub ListBox1_Change()
Dim counter As Integer
Dim selectedCount As Integer
selectedCount = 0
For counter = 1 To ListBox1.ListCount Step 1
If ListBox1.Selected(counter - 1) = True Then 'selected method has 0 base
selectedCount = selectedCount + 1
End If
Next counter
If selectedCount >= 4 Then 'modify # here
ListBox1.Selected(ListBox1.ListIndex) = False 'listindex returns the active row you just selected
MsgBox "Limited to 4 Choices", vbInformation + vbOKOnly, "Retry:"
End If
End Sub
You can use the listbox BeforeUpdate event to see the listbox.ItemsSelected.Count value.
If this is over your limit then you unselect (with listbox.Selectec(item)=False) the current one and cancel the event.
Here's an example :
Private Sub lstItems_BeforeUpdate(Cancel As Integer)
' Warn the user that only x items can be selected.
' ------------------------------------------------
If lstItems.ItemsSelected.Count > MAX_SELECTED_ITEM_PERMITTED Then
MsgBox "You can only select " & MAX_SELECTED_ITEM_PERMITTED & " items in this list.", vbOKOnly + vbInformation, "Error"
' Unselect previously selected item.
' ----------------------------------
lstItems.Selected(lstItems.ListIndex) = False
Cancel = True
End If
End Sub
Using the listbox ItemsSelected collection? That's the only way that I know of to limit the number of selected items. OTOH there are some truly twisted individuals out there who might have figured out an alternative so I never say never.
What's the problemw with this method?
I would suggest that a much more manageable, user-friendly and understandable UI for this would be the paired listbox approach, with ADD> and button after the user has reached the limit. You don't have to do anything difficult, just check the ListCount of the right-hand listbox and disable the ADD> button when it reaches the limit.
And you avoid many problems as it's quite clear to the user what they are doing in comparison to selecting multiple items at once in a single listbox. You could make the lefthand listbox multiselect and simply disable the ADD> button if the ItemsSelected count exceeds the limit, and notify the user appropriately.