VBA - Argument not optional error ! Trying to set the enabled property of a Listbox based on the Selected Value of another Listbox - vba

I have a form named as NoForm which has two List-boxes named as No_ListBox1 and No_ListBox4.
By Default, the Property of No_ListBox4 is set to be enabled = false
when No_ListBox1 value is "yes", , the Property of No_ListBox4 is set to be enabled = true
When it is "No", it stays as enabled = false. But here comes the Problem, I am not able to deselect the selected values in No_ListBox4 when the user changes it back to "No" in No_ListBox1.
Kindly Share your thoughts.
But,
This is the Code,
Private Sub No_ListBox1_Click()
SelectNext = NoForm.No_ListBox1.Value
If SelectNext = "Yes" Then
NoForm.No_ListBox4.Enabled = True
End If
If SelectNext = "No" Then
NoForm.No_ListBox4.Enabled = False
If NoForm.No_ListBox4.Selected = True Then
NoForm.No_ListBox4.Selected = False
End If
End If

Try this:
Private Sub No_ListBox1_Click()
With Me
If .No_ListBox1.Value = "Yes" Then
.No_ListBox4.Enabled = True
Else
With .No_ListBox4
.Selected(.ListIndex) = False
.Enabled = False
End With
End If
End With
End Sub

The solution is to flip your logic around. Deselect the items first, then disable the control.

Related

VB PowerPoint, showing a picture when several others are clicked

I've been learning today some VBA basics to apply in powerpoint, but I have some experience in some other languages. As title says, I want a picture to be shown after I click on 3 other pictures before that. When any of this 3 pictures are clicked, they trigger a tick to be shown above that image, and I'm using those as a refer to code my macro. I have the following:
Sub Condicion()
Set Diapo14 = ActivePresentation.Slides(14)
If Diapo14.Shapes("tick1").Visible = True And _
Diapo14.Shapes("tick2").Visible = True And _
Diapo14.Shapes("tick3").Visible = True Then
Diapo14.Shapes("FlechaDer").Visible = True
End If
End Sub
I have the picture I want to show (FlechaDer) with a disappear effect as soon as the slide starts, but no matter what I do, when I test the slide, the picture is always there. Maybe I'm not applying the correct approach, hope someone can help me. I'm not even sure if this can be done in PowerPoint.
I got it working with the code below. I had to replace the "ticks" in the If cycle, and instead of them I used the pictures that once clicked popped up the ticks:
Sub Can12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Can").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
Sub Wool12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Wool").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
Sub Pencil12()
Set Diapo12 = ActivePresentation.Slides(12)
Diapo12.Shapes("Pencil").Visible = False
If Diapo12.Shapes("Can").Visible = False And Diapo12.Shapes("Wool").Visible = False And Diapo12.Shapes("Pencil").Visible = False Then
Diapo12.Shapes("FlechaDer").Visible = True
End If
End Sub
It worked fine on activating an animation (Right Arrow popping up) when all 3 items were clicked. I had to add the corresponding macro to the picture. (Can, wool and pencil)

vba Checkbox if else condition in Subform, access

I have a main form with several tab controls with subforms in them. On the first tab or subform I have a "checkbox 1", which shall be:
checked if "textbox 1" is not empty
unchecked if "textbox 1" is empty
The code is directly put into the Class Object of "subform 1", that's why I thought I could use Me.
Here is my code, but I always get error messages :(
Private Sub Ctltextbox_1_AfterUpdate()
If Len(Ctltextbox_1.Value) = 0 Then
Me.checkbox_1.Value = 0
Else
Me.checkbox_1.Value = -1
End If
End Sub
That way I am getting the
Run-time error '2448': You can't assign a value to this object.
On the line that attempts to assign -1 to Me.checkbox_1.Value.
Try this. It is working for me. I moved it to the Ctltextbox_1_Change action. This will call the function whenever it is changed and will catch when you have nothing in the box as well. I also changed your values to True and False.
Please try this and let me know.
Private Sub Ctltextbox_1_Change()
If Len(frmSub.Ctltextbox_1.Value) = 0 Then
frmSub.CheckBox_1.Value = False
Else
frmSub.CheckBox_1.Value = True
End If
End Sub
you can also solve this in one line as mentioned below.
Private Sub Ctltextbox_1_Change()
frmSub.CheckBox_1.Value = IIf((Len(frmSub.Ctltextbox_1.Value) = 0), False, True)
End Sub
Ok, the simple answer is: you can not use the expression Len()= 0 for a not numeric field! So my working code for the subform is now:
Private Sub Textbox1_AfterUpdate()
If IsNull(Textbox1.Value) = False Then
Me.checkbox1.Value = True
Else
Me.checkbox1.Value = False
End If
End Sub
Thank you all for your help! :)

How to create expandable/retractable form MS Access 2013 VBA?

I am creating a data entry form for one of my database tables. For one of the sections, I have the text field with ONLY the caption: "Description 1" showing. If the Description 1 textbox is filled out by the user, I want it to show the Description 2 textbox. If the user fills out the Description 2 textbox, the Description 3 textbox will show up and so on up to 10 Description textboxes. Is there a way to hide the extra text boxes kind of like when you fill out the information while creating a macro? For example when you click Create --> Macro, there is only a dropdown box for you to select an action. If you choose Open Form and hit enter, 6 more text boxes with captions appear.
Is there a way to get that kind of functionality in a form? Also, in the Macro builder, it dynamically rearranges the page for you, can this also be done with the form?
Follow these steps:
Mark the visible property as false
Add OnChange event for each textbox.
Write the VBA code to determine if the next control will be showed or hide. Note, the Me!FormControlItem.Text is accessible only if the control is focused.
There is the 3 functions for each control.
Private Sub text1_Change()
If Not Trim(Me!text1.Text) = "" Then
Me!Text2.Visible = True
Me!Label2.Visible = True
ElseIf Not Trim(Me!Text2) = "" Then
Me!Text2.Visible = True
Me!Label2.Visible = True
Else
Me!Text2.Visible = False
Me!Label2.Visible = False
End If
End Sub
Private Sub Text2_Change()
If Not Trim(Me!Text2.Text) = "" Then
Me!Text3.Visible = True
Me!Label3.Visible = True
ElseIf Not Trim(Me!Text3) = "" Then
Me!Text3.Visible = True
Me!Label3.Visible = True
Else
Me!Text3.Visible = False
Me!Label3.Visible = False
End If
End Sub
Private Sub Text3_Change()
If Not Trim(Me!Text3.Text) = "" Then
Me!Text4.Visible = True
Me!Label4.Visible = True
ElseIf Not Trim(Me!Text4) = "" Then
Me!Text4.Visible = True
Me!Label4.Visible = True
Else
Me!Text4.Visible = False
Me!Label4.Visible = False
End If
End Sub
Enjoy!

ActiveX List Boxes will not "Size and Move" with their parent cells

I'm new at VBA so sorry in advance if this is a silly question. I have a Worksheet with ActiveX List boxes. The worksheet also has Toggle Switches. The toggle switches are set up to Hide Rows and ActiveX boxes when not depressed and Show Rows and ActiveX boxes when depressed. I'd like to save the file with all of the Toggle switches not depressed so that the user can un-hide only the rows and boxes that they need. Everything works properly until I save the file with all rows hidden. After the save all of the boxes change locations. I've tried setting the boxes to "Move and Size with cell", "Move but don't size with cell", and "Don't more or Size with cell" in the preferences. The same thing happens with all options. Below is my toggle switch code. Is there something in there causing this to happen?
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
'This area contains the things you want to happen
'when the toggle button is not depressed
Range("101:183").EntireRow.Hidden = False
Sheet1.Range("94:144").EntireRow.Hidden = False
'This hides the listboxes since they can not move and
'size with cells
Sheet11.OLEObjects("ListBox1").Visible = True
Sheet11.OLEObjects("ListBox2").Visible = True
Sheet11.OLEObjects("ListBox3").Visible = True
Sheet11.OLEObjects("ListBox4").Visible = True
Sheet11.OLEObjects("ListBox5").Visible = True
Sheet11.OLEObjects("ListBox6").Visible = True
Sheet11.OLEObjects("ListBox7").Visible = True
Sheet11.OLEObjects("ListBox8").Visible = True
Sheet11.OLEObjects("ListBox9").Visible = True
Sheet11.OLEObjects("ListBox10").Visible = True
Sheet11.OLEObjects("ListBox11").Visible = True
Sheet11.OLEObjects("ListBox12").Visible = True
Sheet11.OLEObjects("ListBox13").Visible = True
Sheet11.OLEObjects("ListBox14").Visible = True
Sheet11.OLEObjects("ListBox15").Visible = True
Sheet11.OLEObjects("ListBox16").Visible = True
Sheet11.OLEObjects("ListBox17").Visible = True
Sheet11.OLEObjects("ListBox18").Visible = True
Else
'This area contains the things you want to happen
'when the toggle button is depressed
Range("101:183").EntireRow.Hidden = True
Sheet1.Range("94:144").EntireRow.Hidden = True
Sheet11.OLEObjects("ListBox1").Visible = False
Sheet11.OLEObjects("ListBox2").Visible = False
Sheet11.OLEObjects("ListBox3").Visible = False
Sheet11.OLEObjects("ListBox4").Visible = False
Sheet11.OLEObjects("ListBox5").Visible = False
Sheet11.OLEObjects("ListBox6").Visible = False
Sheet11.OLEObjects("ListBox7").Visible = False
Sheet11.OLEObjects("ListBox8").Visible = False
Sheet11.OLEObjects("ListBox9").Visible = False
Sheet11.OLEObjects("ListBox10").Visible = False
Sheet11.OLEObjects("ListBox11").Visible = False
Sheet11.OLEObjects("ListBox12").Visible = False
Sheet11.OLEObjects("ListBox13").Visible = False
Sheet11.OLEObjects("ListBox14").Visible = False
Sheet11.OLEObjects("ListBox15").Visible = False
Sheet11.OLEObjects("ListBox16").Visible = False
Sheet11.OLEObjects("ListBox17").Visible = False
Sheet11.OLEObjects("ListBox18").Visible = False
End If
End Sub
I know this isn't the answer to your question (I haven't even looked at it yet), but i just felt like giving you this code, this is the exact code you provided and will function the same way, just looks a tiny bit clearer (actually as it also removes the if statement it prolly even performs at like 1/1000000 of a millisecond faster also =D)
Private Sub ToggleButton1_Click()
Dim boolToggleValue As Boolean
Dim i As Integer
boolToggleValue = ToggleButton1.Value
'This area contains the things you want to happen
'when the toggle button is not depressed
Range("101:183").EntireRow.Hidden = Not boolToggleValue
Sheet1.Range("94:144").EntireRow.Hidden = Not boolToggleValue
'This hides the listboxes since they can not move and
'size with cells
With Sheet11
For i = 1 To 18
.OLEObjects("ListBox" & i).Visible = boolToggleValue
Next i
End With
End Sub

Cycling through some checkboxes on my excel page

I am VERY new to VBA (and know of it only within excel).
I am trying to cycle through some (but not all) checkboxes. They are currently named CheckBox1 through CheckBox15. Howe do I cycle through say for instance CheckBox5 through CheckBox10?
I guess I am hoping there is a 'method' similar to 'CheckType' for controls that will allow me to check name?
Here is what I have tried. Causes a Compile Error - Sub or Function not defined, and highlights Worksheet.
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 2 To 4
If Worksheets("ActiveX").Controls("Checkbox" & i).Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
While the above doesnt work, what is below does:
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 1 To 2
If Sheet2.CheckBox2.Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
Of course, the loop has no affect on the outcome, but it compiles and atLeastOneChecked turns from False to True when Checkbox2 is True. Note that Sheet2 has been named ActiveX. I clearly don't understand how Worksheet and Controls work. Could someone help?
After fixing the error described below, this still won't work. I simplified to the following:
Private Sub BoxCheck()
Dim ole As OLEObject
atLeastOneChecked = False
Set ole = Sheets("ActiveX").OLEObjects("Checkbox2")
If ole.Value = True Then
atLeastOneChecked = True
End If
End Sub
This doesn't work. It fails at:
If ole.Value = True Then
The error states: Object Doesn't support this property or method
This is true for OLEObjects, but not for Checkboxes. When I look at the properties of ole, I see that its Object property is set to Object/Checkbox and that this Object has a value. I guess that is what I should be referencing in my if statement, but I don't know how.
I think I solved the problem.
The value of the Checkbox is accessed by Referencing the Object property within the OLEObject I set...Like this:
If ole.Object.Value = True Then
Thanks for all your help. If someone has a more elegant solution, I would still like to see it.
Use CheckBox.Name
Example:
For Each cb In ActiveSheet.CheckBoxes
If cb.Name = "CheckBox5"
' Do stuff
End If
Next cb
To expand on #Parker's answer:
Private Sub BoxCheck()
atleastonechecked = False
Dim oles As OLEObject
For i = 2 To 4
'If you're using Shapes Controls:
If ThisWorkbook.Worksheets("ActiveX").Shapes("Check Box " & i).Value = True Then
atleastonechecked = True
End If
' If you're using ActiveX Controls
Set oles = ThisWorkbook.Worksheets("ActiveX").OLEObjects("CheckBox" & i)
If oles.Value = True Then
atleastonechecked = True
End If
Next i
End Sub
Sorry, just put together the previous answer without testing - that always fails. Just use the If loop based on what type of control you're using.