I have a list box that populates with different sets of data based on user selections.
How can I cycle through any given values that may be in the list box? Is this a For Each statement, or what?
Here is how you iterate through the ListBox:
Dim i as Integer
For i = 0 to Me.ListBoxName.ListCount -1
'Access each item with
'Me.ListBoxName.ItemData(i)
Next i
You can do a For loop to examine each row in the listbox, and do whatever with the rows which are selected. In this example, I display the second column from selected items in the lstLocations listbox. (Column numbering starts with zero.)
Private Sub cmdShowSelections_Click()
Dim lngRow As Long
Dim strMsg As String
With Me.lstLocations
For lngRow = 0 To .ListCount - 1
If .Selected(lngRow) Then
strMsg = strMsg & ", " & .Column(1, lngRow)
End If
Next lngRow
End With
' strip off leading comma and space
If Len(strMsg) > 2 Then
strMsg = Mid(strMsg, 3)
End If
MsgBox strMsg
End Sub
Note I assumed you want the selected items from the list box. If you want all items, selected or not, you could use .ItemData as #DavidRelihan suggested. However, in that case, you could get them from the listbox .RowSource instead.
If working with a listbox in Access I like to capture the listbox recordset and loop through it. Perhaps because I find DAO recordset objects easy to work with.
I'd do something like:
Dim Rst as DAO.Recordset
Set Rst = lbxYourListboxObj.Recordset
'test to assure that there are records
If Rst.EOF then
'some error handling
end if
'I'm just paranoid so I always do this
Rst.MoveFirst
'iterate through list
Do Until Rst.EOF
'do something for each record
'it is nice and convenient to be able to reference the field names directly too!
debug.print Rst!Field1.name,Rst!Field1.value
Rst.MoveNext
Loop
Related
I have a form, with a listbox. User can choose record and add it to a list box using listbox. I want to create a VBA script to open a report and match all the values in listbox with corresponding records. So far i can do this with single one, but for many i don't know what is going on. Here is the code:
Private Sub btnSearchMany_Click()
Dim i As Long
With Me.List12
If .ListCount = 0 Then
MsgBox "Brak wybranych wpisow.Dodaj wpisy, wybierajac je z listy i wciskajac przycisk dodaj.", vbCritical
Exit Sub
End If
For i = 0 To .ListCount - 1
DoCmd.OpenReport "rptKKsy", acViewReport, , "[tblKKsy].[KKS]='" & .List12(i) & "'"
Next i
End With
End Sub
List12- lisbox that user can add records "later i will rename it"
rptKKsy- my report
tblKKsy- table that stores values
KKs - one of the values stored in my table
I always getting error: method or data member not found. I tried to replace:
With Me.List12
with
With Forms("frmSearch").Form.list12
But it just created another error.
Edit: As suggested, my list box is a value list, and user can put thing in it by using a combobox and clicking a button ( button have addItem command programed). After user add some things, i want to open a report for each value stored in listbox. I dont want user to select things in listbox, because we will be using everything in it so its not nedded. I tried to use Item.Data property as June7 linked to, but ill be honest: I dont get it. Here is the code:
Private Sub btnSearchMany_Click()
Dim i As Long
Dim lValue As String
With Me.List12
For i = 0 To .ListCount - 1
If .ListCount = 0 Then
MsgBox "Brak wybranych wpisow.Dodaj wpisy, wybierajac je z listy i wciskajac przycisk dodaj.", vbCritical
Exit Sub
End If
Next i
End With
If i = Len(lValue) > 2 Then
DoCmd.OpenReport "rptKKsy", acViewReport, , "[tblKKsy].[KKS]='" & .ItemData(lValue) & "'"
End If
End Sub
So I have this spreadsheet with several listboxes. In these listboxes I have some values/items that are actually filters. I want to get each item/filter of each listboxes to amend an SQL query in my code.
So I've been asked to looped through the listboxes and I managed to do it by looping the Shapes of the spreadsheet but eventually ... those listboxes are now viewed as Shapes in VBA and not listboxes anymore. I'm looking for a way to either turn my shape in listbox or maybe find a method from the Shapes type to loop each listbox's items. Here is the part of my code, so far I loop through each shapes/listboxes, if within my shapes'name there is the word "CFRA" then I want to loop within each item selected of my listbox so that my function return them.
Private Function getListFilters() As String
My_Sheet.Activate
Dim Shp
For Each Shp In My_Sheet.Shapes
pos = InStrRev(Shp.Name, "CFRA", , vbTextCompare)
MsgBox (pos)
If pos <> 0 Then
MsgBox (TypeName(Shp))
End If
Next
End Function
Thanks in advance for those who are willing to help me and have a great day :)
Since you do not explain what is to be extracted from the list box, try the next Function, please. It will deliver the list box object having "CFRA" string in its name. Of course, any string can be used:
Private Function getListObjX(strPartName As String, sh As Worksheet) As MSForms.ListBox
Dim oObj As OLEObject
For Each oObj In sh.OLEObjects
If oObj.Name Like "*" & strPartName & "*" Then
'Debug.Print oObj.Name, TypeName(oObj.Object): Stop
If TypeName(oObj.Object) = "ListBox" Then
Set getListObjX = oObj.Object: Exit Function
End If
End If
Next
End Function
It can be called in the next way:
Sub testGetListObj()
Dim sh As Worksheet, lstB As MSForms.ListBox, lstBF As ListBox
Dim i As Long, arrSel As Variant, k As Long
Set sh = ActiveSheet
Set lstB = getListObjX("CFRA", sh)
If lstB Is Nothing Then MsgBox "No such an ActiveX list box...": Exit Sub
ReDim arrSel(lstB.ListCount - 1)
For i = 0 To lstB.ListCount - 1
If lstB.Selected(i) Then
'Debug.Print lstB.List(i)
arrSel(k) = lstB.List(i): k = k + 1
End If
Next i
ReDim Preserve arrSel(k - 1)
MsgBox Join(arrSel, "|")
End Sub
But, being an ActiveX list box type, you can simply use one of its events. Of course, if you do not need to take items from more then a list box...
I also prepared a function to return the object for a Form list box (before you clarify the issue). Maybe, somebody else will use it...
Dim oObj As ListBox
For Each oObj In sh.ListBoxes 'even not being shown by intellisense, this collection exists...
If oObj.Name Like "*" & strPartName & "*" Then
'Debug.Print oObj.Name
Set getListObjF = oObj: Exit Function
End If
Next
End Function
It can be called similarly, but the lstB should be declared As ListBox.
Edited, to make the function working in one step:
Private Function getListFilters(strPartName) As String
Dim sh As Worksheet, lstB As MSForms.ListBox
Dim oObj As OLEObject, i As Long, arrSel As Variant, k As Long
Set sh = ActiveSheet ' use here your sheet
For Each oObj In sh.OLEObjects
If oObj.Name Like "*" & strPartName & "*" Then
If TypeName(oObj.Object) = "ListBox" Then
Set lstB = oObj.Object: Exit For
End If
End If
Next
If lstB Is Nothing Then MsgBox "No such an ActiveX list box...": Exit Function
ReDim arrSel(lstB.ListCount - 1)
For i = 0 To lstB.ListCount - 1
If lstB.Selected(i) Then
arrSel(k) = lstB.List(i): k = k + 1
End If
Next i
ReDim Preserve arrSel(k - 1)
getListFilters = Join(arrSel, "|")
End Function
And the function will be simple called as:
Debug.Print getListFilters("CFRA")
You access ActiveX-Objects via the OLEObjects-Collection of a worksheet. The interesting control information are in the property Object of such an object:
Use VBA function TypeName to figure out what kind of OLE object you have
Number of items can be fetched with the Object.ListCount property.
To access the items of a listbox, loop over the Object.list property (index starts at 0, so loop must run from 0 to ListCount-1)
To check if an item is selected, use the matching .Object.Selected property.
The following code will loop will print all selected items of all listboxes of a worksheet:
Sub listBoxes()
Dim objx As OLEObject
For Each objx In ActiveSheet.OLEObjects
Debug.Print "Name = " & objx.Name & " Typ = " & TypeName(objx.Object)
If TypeName(objx.Object) = "ListBox" Then
Dim i As Long
For i = 0 To objx.Object.ListCount - 1
If objx.Object.Selected(i) Then
Debug.Print objx.Name, objx.Object.list(i)
End If
Next i
End If
Next
End Sub
Update: To show the coherence between Shapes, OleObjects and ActiceX controls on a sheet:
A Shape is a container for everything that is not part of a cell/range. Could be any kind of painted shape forms (rectangels, arrows, stars...), could be an image, a chart, an OLEObject, a form control and so on.
An OLEObject is a something that is not from Excel but is put into an Excel sheet, using a technique called OLE, Object Linking and Embedding.
An ActiveX is a control (editbox, listbox...). These controls where developed by Microsoft and where meant to run in different environments (eg a browser). They are accessible via dll and this dll is added into Excel and other office programs.
Every ActiveX-Control is added as an OLEObject into a sheet, but you can have also different OLEObjects (eg an embedded Word document) that are not an ActiceX objects.
When you want to access those things via VBA, you can use the Shapes-collection that lists all shapes of a sheet (including all OLEObjects), or you can use the OLEObjects-collection that lists all OLEObjects (including all ActiveX controls). However, there is no ActiveX collection, so if you want to fetch all ActiceX-Controls, you have to loop over either the two collections mentioned above.
If you want to access an OLEObject from the shape collection, you first need to check the type of the shape, it must have the type msoOLEControlObject (12) or msoEmbeddedOLEObject (7). A list of all shape types can be found here.
If the shape is either 7 or 12, you can access the OLEObject using Shape.OLEFormat.Object. The following to loops results in exactly the same (ws is just a worksheet variable)
Dim sh As Shape, oleObj As OLEObject
For Each sh In ws.Shapes
If sh.Type = msoOLEControlObject Or sh.Type = msoEmbeddedOLEObject Then
Set oleObj = sh.OLEFormat.Object
Debug.Print oleObj.Name, oleObj.OLEType
End If
Next
For Each oleObj In ws.OLEObjects
Debug.Print oleObj.Name, oleObj.OLEType
Next
Note that sh.Name and sh.OLEFormat.Object.Name are not necessarily the same.
Now the last step is to find the ActiveX-Controls of a specific type, this was already shown in the code of the original answer above - the ActiveX-control can be accessed via oleObj.object. Check the object type if the VBA function TypeName to filter out for example your listboxes.
I have two listboxes, named listbox1 and listbox2.
Listbox1 is populated using a SQL query, and contains two columns. Its first column contains values that have commas.
Listbox2 is set as a value list in the "row source type" attribute of the Access property sheet.
My goal is to copy selected items from listbox1 to listbox2 using a control button.
I also need to be aware that listbox1 records contain commas, which act as delimiters during copying. That particular issue has been resolved, though.
I have created two modules to accomplish the copying of selected records from one listbox to another:
Public Sub CopySelected(ByRef frm As Form)
Dim ctlSource As Control
Dim ctlDest As Control
Dim strItems As String
Dim intCurrentRow As Integer
Set ctlSource = Me!listbox1
Set ctlDest = Me!listbox2
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) = True Then
'must insert double quote around single quote to escape commas
strItems = strItems & "'" & ctlSource.Column(0, intCurrentRow) & "'" & ";"
Me!listbox2.AddItem (strItems)
End If
Next intCurrentRow
End Sub
And
Private Sub cmdAddSelected_Click()
CopySelected Me
End Sub
I do have the multi-select option on listbox1 set to "extended".
The current problem is that when I click my control button, only the first selection from listbox1 is copied over- with the caveat that it is copied multiple times (copied the same number as those selected records).
Clearly, there is a problem with my For-loop.
R is my main language, and I am only just learning VBA.
In your program you are concatenating all selected values with separator and adding the longer and longer string to Listbox2. The ; acts as a column separator for multicolumn listboxes, i.e. you are kind of "transposing" the selected values from Listbox1 to Listbox2 while the excess items (above the number of columns of Listbox2) are simply ignored. If you want to copy individual values into the single column Listbox2, do this:
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) Then
strItems = "'" & ctlSource.Column(0, intCurrentRow) & "'"
Me!listbox2.AddItem (strItems)
End If
Next intCurrentRow
I am developing an application, in excel with VBA forms. in one form I have a combobox to that let user select customer name, the rowsource of this combobox is a named range (name column of customers table). Everything working fine but I need to add 1 or more additional items in the combobox that not exist in the table column. For example I need to add "All" item in the cobmobox so user can select a particular customer name or All. at other place I wan't to add "Other" item in combobox with same rowsource so if the customer is new user can select Other and then type name in textbox.
I tried following code to add an item
Private Sub UserForm_Activate()
With Me.testCombo
.AddItem "All"
End With
End Sub
but i got error
Run-time error '70'
permission denied
if i remove rowsource property from the combobox then the above code work but only one item "All" display.
Note: I don't want to add "All" and "Other" in customer table, this could be easy solution but will cause other problem.
Try like this:
Private Sub UserForm_Activate()
Dim rowValue As Variant
Dim lngCount As Long
Dim myCell As Range
Dim varCombo() As Variant
With Me.ComboBox1
ReDim varCombo(Me.ComboBox1.ListCount)
For Each myCell In Range(.RowSource)
varCombo(lngCount) = myCell.value
lngCount = lngCount + 1
Next myCell
.RowSource = ""
For lngCount = LBound(varCombo) To UBound(varCombo) - 1
.AddItem CStr(varCombo(lngCount))
Next lngCount
.AddItem "All"
.AddItem "Nothing"
End With
End Sub
As mentioned in the comments, by A.S.H., you should unset the .RowSource property. However, you do not lose it, if you run the code twice, it would be the same. In my code I use UBound(varCombo) - 1, because I use lngCount=lngCount+1 on the last looping over the cell.
Something like this could do what you need
Dim a() As Variant
Dim b() As String
Dim s As String
a = Application.Transpose(Range("a1:a5").Value)
s = "Please select;" & Join(a, ";")
Erase a
b = Split(s, ";")
Me.ComboBox1.List = b
Thank you everyone for helping, the main problem was permission as A.S.H said if Rowsource is set then cannot add any item in the ComboBox. So I delete the RowSource from the properties in form. and wrote following code and it seems everything working fine. I hope my codes are good enough and simple.
Private Sub fillComboBox()
Dim comboData As Range
With Me.CWR_CustName
' first option of comobobox will be All
.AddItem "All"
For RW_Cust = 1 To Range("tblCust").Rows.Count
' add each customer name from customer table name column
.AddItem (Range("tblCust[Name]")(RW_Cust))
Next RW_Cust
End With
End Sub
I have a query that returns for example 5 rows, I want to show thes these rows(fields) in textboxes. but it only shows me one record. I have also set Default view Property of my form into Continuous form. her is my code:
Private Sub List2_DblClick(Cancel As Integer)
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT XValue, YValue,Wert FROM tb_DCM_Daten WHERE (FzgID=" & Forms!frm_fahrzeug!ID & " AND [Name]='" & List2.Value & "')")
If rst.RecordCount <> 0 Then
Do While Not rst.EOF
Text8.SetFocus
Text8.Text = rst.Fields("XValue").Value
Text10.SetFocus
Text10.Text = rst.Fields("YValue").Value
Text11.SetFocus
Text11.Text = rst.Fields("Wert").Value
rst.MoveNext
Loop
End If
End Sub
how can I do that?
As it stands now, your code would loop through the Recordset, put the values from the first record into the textboxes, then overwrite those values with the ones from the second record, and overwrite those with the values from the third record, and so on.
It sounds like you want a subform. Have a look at the Office tutorial here for more information.