Find Item.index after using Items.find - vba

How do you look up the index of a contact item that was set using the items.find method? After finding the item, I want to be able to move to the next item, but my code sends me to the first item in the collection. A condensed version of my plan is below...
dim ColItms as items
dim CI as contactItem
Dim CIindex as integer
set CI= ColItms.find("[CompanyName] = ""IBM""")
CIindex = CI.???? ''''' This shows what I'm wanting to do, but don't know how
' now advance to next item in collection
set ci = ColItms.item(CIindex +1) ' i think this would work if I could find CIindex
set ci = ColItms.GetNext ' this fails as it returns the 1st item in the collection
Right now all that seems to work is to loop through each item in the collection to see if it matches the found contact,

Items have no intrinsic index, only an entry id.
To find the next match, use Items.FindNext.

Related

Specify column index of selected data in ItemBox to retrieve when iterating through selected items

Basically have an ItemBox with three columns in an Access form. I want to get specific columns when I iterate though the selected items, but I am having difficulty doing so.
Function GetSelectedValues(famList As Variant) As Collection
Dim famListColl As New Collection
' Loop through all of the selected surveys
For Each fam In famList.ItemsSelected
Debug.Print (fam) ' prints the index number of the selected item
Debug.Print (famList.ItemData(fam)) ' this prints out the last columns value for the selected item
famListColl.Add famList.ItemData(fam)
Next
Set GetSelectedValues = famListColl
End Function
If I try to do the following I get an Run-time error '424': Object required error:
Debug.Print(fam.Column(1))
Debug.Print(famList.ItemData(fam).Column(1))
This somewhat works, but only displays the values of the first selected item indefinitely (it doesn't iterate):
Debug.Print(famList.Column(1))
So I need to combine the two somehow, but struggling to figure that much.
Thanks for the help!
Stumbled across this post that helped me sort it out. I doubt I would have figured it out otherwise. Looks like this:
Debug.Print (famList.Column(2, fam))
So the entire solution for me is the following:
Function GetSelectedValues(famList As Variant) As Collection
Dim famListColl As New Collection
' Loop through all of the selected surveys
For Each fam In famList.ItemsSelected
famListColl.Add famList.Column(1, fam)
famListColl.Add famList.Column(2, fam)
Next
Set GetSelectedValues = famListColl
End Function

How to remove selected items from a listbox

This is for a VB.NET 4.5 project in VS2015 Community.
I am trying to remove certain selected items from a listbox, but only if the selected item meets a condition. I've found plenty of examples on how to remove selected items. But nothing that works with a condition nested in the loop going through the selected items (at least, I can't get the examples to work with what I'm trying to do...)
Here's my code:
Dim somecondition As Boolean = True
Dim folder As String
For i As Integer = 0 To lstBoxFoldersBackingUp.SelectedItems.Count - 1
If somecondition = True Then
folder = lstBoxFoldersBackingUp.SelectedItems.Item(i)
Console.WriteLine("folder: " & folder)
lstBoxFoldersBackingUp.SelectedItems.Remove(lstBoxFoldersBackingUp.SelectedItems.Item(i))
End If
Next
The console output correctly shows the text for the current iteration's item, but I can't get the Remove() to work. As the code is now, I get the console output, but the listbox doesn't change.
Removing items changes the index position of the items. Lots of ways around this, but from your code, try iterating backwards to avoid that problem. You should also remove the item from the Items collection, not the SelectedItems collection:
For i As Integer = lstBoxFoldersBackingUp.SelectedItems.Count - 1 To 0 Step -1
If somecondition = True Then
folder = lstBoxFoldersBackingUp.SelectedItems.Item(i)
Console.WriteLine("folder: " & folder)
lstBoxFoldersBackingUp.Items.Remove(lstBoxFoldersBackingUp.SelectedItems(i))
End If
Next
You can simply use this in order to remove a selected item from the listbox
ListBox1.Items.Remove(ListBox1.SelectedItem)
I hope this was helpful.
This will not work:
ListBox1.Items.Remove(ListBox1.SelectedItem)
You need to define exactly which items will be deleted, so the next works fine for me:
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
However it removes one line every time it is called.

ideal way of clearing out collections

In my procedure, I have multiple collections that are templates for data. These collections will be stocked with data, called upon, and then emptied for repopulation during my repetitive process. I've read that I can do: set myCollection = new Collection to essentially clear it out for reuse.
I've also seen set myCollection = Nothing. Is there a preferred method for handling the clearing out of existing collections?
I have extracted related portion from an article which clarifies this point.
Use Set means the collection will behave differently than when you set the collection to nothing. The next section explains this.
Removing All items from a Collection
To remove all items from a collection you can simply set it to nothing.
Set Coll = Nothing
An important point to understand here is that what this does depends on how you created the collection. As we saw you can create a Collection by declaring using New or by using Set and New. Let’s look at both types
Declaring Using New
If you set this collection to nothing then it will be set to the state where the “object is not set”. When you add a new item VBA automatically sets the Collection variable to a valid collection.
In other words, if you set the collection to nothing it will empty all the items. If you then add an item to the collection you will now have a collection with one item. This makes it simple to empty a collection.
The following code demonstrates this.
Sub EmptyColl()
' Create collection and add items
Dim coll As New Collection
' add items here
' Empty collection
Set coll = Nothing
' Add item
coll.Add "Pear"
End Sub
A subtle point to emphasize here is that when you set the collection to Nothing it is not actually set to nothing. Therefore if you try to compare it with being it will not work.
Using Set and New
When you use Set to create a collection you must create the collection again if you set it to Nothing. In the following code after setting to nothing you must then set using new again. If you don’t do this you will get the error: “Object Variable or With block variable not set”.
Sub EmptyCollSet()
' Create collection
Dim coll As Collection
Set coll = New Collection
' Add items here
' Empty collection
Set coll = Nothing
' SET TO NEW BEFORE USING
Set coll = New Collection
' Add item
coll.Add "Pear"
End Sub
Remove All – An Alternative Method
The following method will also remove all the elements of a collection but is a slower way to do it. The advantage is that is will work no matter which way you create the collection.
Sub RemoveAll(ByRef coll As Collection)
Dim i As Long
For i = coll.Count To 1 Step -1
coll.Remove i
Next i
End Sub
For details please refer The Ultimate Guide To Collections in Excel VBA by Paul Kelly

Find Groups in when selected on screen

While i was running my code, i found that the number of groups present in the selection rectangle in mechanical drawings, are shown below in autocad drafting. But when i try to find using VBA, i couldn't because of unavailability of API.
Option Explicit
Sub Group()
'Declaration
Dim acApp As AcadDocument
Dim acSeSet As AcadSelectionSet
Set acApp = ThisDrawing.Application.ActiveDocument
'Selection Set Creation
Set acSeSet = acApp.SelectionSets.Add("ShelSde4htd1")
acSeSet.SelectOnScreen
When i am running this part of code it goes to screen, and ask me to select the part on the screen. I am able to get entities from this selection but not the groups. I know group is also a collection, but can i then get the group selection from the selection on the screen?
Dim acEnt As AcadEntity
Dim entity_handle() As String
Dim i As Integer
i = 0
Dim entity_count As Integer
entity_count = acSeSet.Count() 'Selection Set Count
ReDim entity_handle(entity_count) 'Resizing the entity handle array
For Each acEnt In acSeSet 'Iterating through selected entities and storing in one array, the handles
entity_handle(i) = acEnt.Handle
i = i + 1
Next
Here i can get the entities, but apart from it i also want to get the groups selected in that region.
VBA's SelectionSet object can only select graphic elements
and groups are not graphic elements, they're (named) selectionset themselves!
to get possibly selected groups you must iterate through each element of the selectionset and check whether it belongs to a Group. then, if you need to make sure the whole group is selected you must check whether all elements of that group belongs to selectionset.
it's quite a bunch of multiple iterations: if your drawing has many groups and/or many elements in groups then you'd better investigate the best suitable searching technique (arrays to store groups elements?)

Is it possible to move a part with repect to constraints in Product using Catia vba?

I have to move a probe like sphere between two parts such that the probe is in contact with both the parts. And I have to find the point of contact of the parts, measure their distance and make a fillet on the parts based on this distance. I have achieved in moving the sphere between the parts but the sphere is moving through the parts. So trying to move with respect to constraints
I am trying to automate the manipulate tool in Catia Product.
Is there any command or method exist to move a part with respect to contraints in Catia using vba ?
Or
Is there any way to find the clash between two parts using vba ?
Looking Forward for a solution.
Thank you!!!
Here is a link where you can find a solution for clash.
OK, I got the idea, you want to see the code here :-)
To compute clash in a CATScript:
Sub CATMain()
' get root product of document
Dim RootProd As Product
Set RootProd = CATIA.ActiveDocument.Product
' retrieve selection object of active document
Dim objSelection As Selection
Set objSelection = CATIA.ActiveDocument.Selection
' get two selected objects
If (objSelection.Count2 <> 2) Then
MsgBox "Before running the script you must select two products to compute clash for", vbOKOnly, "No products selected"
Exit Sub
End If
Dim FirstProd As Product
Dim SecondProd As Product
Set FirstProd = objSelection.Item2(1).Value
Set SecondProd = objSelection.Item2(2).Value
' create groups for clash computation
Dim objGroups As Groups
Set objGroups = RootProd.GetTechnologicalObject("Groups")
Dim grpFirst As Group
Dim grpSecond As Group
Set grpFirst = objGroups.Add()
Set grpSecond = objGroups.Add()
' add selected products to groups
grpFirst.AddExplicit FirstProd
grpSecond.AddExplicit SecondProd
' get access to Clashes collection
Dim objClashes As Clashes
Set objClashes = RootProd.GetTechnologicalObject("Clashes")
' create new clash
Dim newClash As Clash
Set newClash = objClashes.Add()
' set new clash to be computed between two groups (two selected products)
newClash.FirstGroup = grpFirst
newClash.SecondGroup = grpSecond
newClash.ComputationType = catClashComputationTypeBetweenTwo
' compute clash
newClash.Compute
End Sub