Write value to a textbox in Userform - VBA - vba

I am trying to write a value dynamically in Textbox which is placed on a Userform. This is my code, and I am getting an error in the last line. It says object required.
Sub Userform1_Display()
TotalSelected = 0
With Sheets("Main").Ent_ListBox
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
Count = 1
TotalSelected = TotalSelected + Count
End If
Next i
End With
'Useform'
Questionaire.Show
'TextBox placed in Userform'
N_Ent_TextBox.Value = TotalSelected
End Sub
Kindly share your thoughts

It looks like your code isn't actually inside the userform's class module - so you need to fully qualify the object:
Questionaire.N_Ent_TextBox.Value = TotalSelected

N_Ent_TextBox.Text = TotalSelected
try to use this

Related

vba excel If condition error on final iteration

I'm having the code takes the input from my checkboxes and grab data from the related worksheet. I ran it line by line and found out that it always gets a runtime error at the If statement on the final loop. Is there something wrong in my code?
Option Explicit
Private Sub UserForm_Initialize()
Dim counter As Long
Dim chkBox As MSForms.CheckBox
''''Add checkboxes based on total sheet count
For counter = 1 To Sheets.count - 2
Set chkBox = Me.Frame1.Controls.Add("Forms.CheckBox.1", "CheckBox" & counter)
chkBox.Caption = Sheets(counter + 2).Name
chkBox.Left = 10
chkBox.Top = 5 + ((counter - 1) * 20)
Next
End Sub
Private Sub cmdContinue_Click()
Dim Series As Object
Dim counter As Long
'''Clear old series
For Each Series In Sheets(2).SeriesCollection
Sheets(2).SeriesCollection(1).Delete
Next
''Cycle through checkboxes
For counter = 1 To Sheets.count - 2
''If the box is checked then
If Me.Frame1.Controls(counter).Value = True Then ''Error here on 4th iteration
''Add new series
With Sheets(2).SeriesCollection.NewSeries
.Name = Sheets(counter + 2).Range("$A$1")
.XValues = Sheets(counter + 2).Range("$A$12:$A$25")
.Values = Sheets(counter + 2).Range("$B$12:$B$25")
End With
End If
Next counter
Me.Hide
End Sub
Also, a second problem is it always run on the wrong loop. If i check box 2 it'll run data for the box 1 sheet, 3 run for 2, 4 run for 3, and 1 run for 4. Can anyone explain the reason behind this?
EDIT: So as VincentG point out below, adding an explicit name "checkbox" in there did the trick (i didn't know you could do that). Index 1 was probably taken by one of the buttons or the frame in the user form, causing it to get off set.
I guess your main problem comes from the fact that the controls have to be accessed starting from index 0. So to loop over all controls, you would do something like
For counter = 0 To Me.Frame1.Controls.Count - 1
Debug.Print counter; Me.Frame1.Controls(counter).Name
Next counter
So, when you stick to you code, I assume you have to change the if-statement to
If Me.Frame1.Controls(counter-1).Value = True Then

Setting CheckBoxes from another userform in VBA

I have a userform which contains a number of checkboxes from 1 to 100. I have written some very simple code so that when you submit the form it creates a binary string that represents the state of those 100 checkboxes, where 0 is false and 1 is true. The code to do this is here:
Private Sub BusRulesSubmit_Click()
Dim myBinaryString As String
Dim nm As String
Dim c As Control
For busRuleIdx = 1 To 100
nm = "CheckBox" & busRuleIdx
Set c = Controls(nm)
If c.Value = True Then
myBinaryString = myBinaryString & "1"
Else
myBinaryString = myBinaryString & "0"
End If
Next
msgBox myBinaryString
End Sub
I now want to open this Userform from another form, where I have a similar binary string, and use this string to set the values of the checkboxes to true or false as appropariate. However I am having issues when setting my control. The code is here:
Private Sub populateBusRules()
Dim c As Control
Dim myBRBinary As String
myBRBinary = "000000000011100000000000000000000000000000000000000000000000000000000010000000000000000000000000000"
For busRuleIdx = 1 To 100
nm = "BusinessRules.CheckBox" & busRuleIdx
Set c = Controls(nm)
If Mid(myBRBinary, buRuleIdx - 1, 1) = 1 Then
c.Value = True
Else
c.Value = False
End If
Next
End Sub
When I run the above, I get a runtime error "Could not find the specified object" and when going to debug it highlights this problem where the code states "Set c = Controls(nm)" - and I can see that it is failing in the first round of the loop i.e. where nm = "BusinessRules.CheckBox1"
Interestingly if I run the code "Set c = Controls(BusinessRules.CheckBox1)" I get no such issue.
Any help would be much appreciated.
Thanks,
Paul.
I think the BusinessRules is giving you the issue. In the Controls collection, there is no Control named "BusinessRules.CheckBox1", only one named "CheckBox1" within the BusinessRules.Controls collection. Assuming there aren't other issues mentioned in the comments above (like the form being closed before this is called), then this should fix your issue

Correct use of ListIndex navigate form by query

I have a subform that has a query in a combo box, and I want to navigate through the different records matching the query results.
Online, I found the "ListIndex" function for a combo box, and my code is as follows for a "next record" scenario:
Private Sub Button_Click()
comboCountry.SetFocus
If comboCountry.ListIndex <> comboCountry.ListCount - 1 Then
comboCountry.ListIndex = comboCountry.ListIndex + 1
Else
comboCountry.ListIndex = 0
End If
End Sub
Even though my initial query works to get in a finite number of values into the combo box, when I press the button I get the error:
Run-time error 7777; You've used the ListIndex property incorrectly
VBA Debugger says that the erroneous line of code is line 4,
comboCountry.ListIndex = comboCountry.ListIndex + 1
What is going wrong with my macro?
Thanks
Well, the on-line help - which you can access by one key press - is very clear: This property is read-only.
You can do something like this:
If comboCountry.ListIndex < comboCountry.ListCount - 1 Then
comboCountry.Value = comboCountry.ItemData(comboCountry.ListIndex + 1)
End If
I believe the source of your faulty code comes from another web site (maybe bytes?). I am not sure but I guess it is different in excel vba than in access vba. At least in ms access vba, listindex is read only and combo.value = combo.itemdata (i) is what is used to set the selections.
below is the code used to cycle through combobox list items using arrow keys.
Public Sub ArrowKeys(ByRef KeyCode As Integer, combo As ComboBox, ByRef Flag As Boolean)
' flag is used to be able to toggle normal action in combo_change()
Select Case KeyCode
Case vbKeyDown
KeyCode = 0 ' make sure the action wont be duplicated
If combo.ListIndex <> combo.ListCount - 1 Then
combo.Value = combo.ItemData(combo.ListIndex + 1)
Flag = True
combo.Dropdown
Else
combo.Value = combo.ItemData(0)
Flag = True
combo.Dropdown
End If
Case vbKeyUp
KeyCode = 0 ' make sure the action wont be duplicated
If combo.ListIndex = -1 Then ' In case nothingg is selected yet (-1 index)
combo.Value = combo.ItemData(combo.ListCount - 1)
combo.Dropdown
ElseIf combo.ListIndex <> 0 Then
combo.Value = combo.ItemData(combo.ListIndex - 1)
Flag = True
combo.Dropdown
Else
combo.Value = combo.ItemData(combo.ListCount - 1)
Flag = True
combo.Dropdown
End If
End Select
End Sub

Excel multi-select, multi-column listboxes

I am having trouble coding a userform that takes the selected data from one multi-column Listbox and adds it to another Listbox on the same userfrom. after adding, the data is removed from the source Listbox
"ListBox" is where the data is located, and "listbox1" is where it is being added to.
Private Sub add_Click()
For i = 0 To ListBox.ListCount - 1
If ListBox.Selected(i) = True Then
NextEmpty = ListBox1.ListCount
ListBox1.List(NextEmpty, 0) = ListBox.List(i, 0)
ListBox1.List(NextEmpty, 1) = ListBox.List(i, 1)
ListBox1.List(NextEmpty, 2) = ListBox.List(i, 2)
ListBox.RemoveItem (i)
End If
Next
End Sub
This code gives me a Run-time error '381'
"Could not set the list property. Invalid property array index."
I have done some looking around but can't seem to pinpoint how to use these properties correctly. Any help is greatly appreciated.
In order to do this, like Daniel said, we need to use an add function.
In the code below you can see how I used the .additem function in my with-block.
To remove the selection after moving it to a new Listbox, I run a backwards loop.
For i = MainListBox.ListCount - 1 To 0 Step -1
Private Sub add_Click()
Dim i As Integer
For i = 0 To MainListBox.ListCount - 1
If MainListBox.Selected(i) Then
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = MainListBox.List(i, 0)
.List(.ListCount - 1, 1) = MainListBox.List(i, 1)
.List(.ListCount - 1, 2) = MainListBox.List(i, 2)
End With
End If
Next i
For i = MainListBox.ListCount - 1 To 0 Step -1
If MainListBox.Selected(i) Then
MainListBox.RemoveItem (i)
End If
Next i
End Sub
You cannot set a value to an index greater than the maximum in the list (and the maximum is exactly ListCount (Or ListCount-1 if zero based).
So, you must add the values with ListBox1.Add

Show only checked boxes in DataGridView

I am returning SQL results into a DataGridView and have run into a problem. I have an option on my form to only show checked values but can't get it working. Here's my code:
For x As Integer = dgvAutogrow.Rows.Count - 1 To dgvAutogrow.Rows.Count
If dgvAutogrow.Rows(x).Cells("checked").Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next
Here's what part of my DataGridView looks like. I want an event to occur that shows only values that have the check box checked.
When I debug I get the following error:
Any suggestions on what to change?
You have to cast the cell as DataGridViewCheckBoxCell and then test the value like this:
For x As Integer = dgvAutogrow.Rows.Count - 1 To 0 Step -1
Dim cel as DataGridViewCheckBoxCell
cel = CType(dgvAutogrow.Rows(x).Cells("YourColumnName"), DataGridViewCheckBoxCell)
If cel.Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next
Also, you had an ArgumentException because the column you specified doesn´t exist, you need to change that.
Code is tried and tested.
You can try this. We need to cast the cell as a DataGridViewCheckBoxCelland change your loop...
For x As Integer = dgvAutogrow.Rows.Count - 1 To 0 Step -1
If CType(dgvAutogrow.Rows(x).Cells("checked"), DataGridViewCheckBoxCell).Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next