Code (VB) for : If "value" contains "textA" then

Code (VB) for : If "value" contains "textA" then

Anonymous
Not applicable
7,726 Views
4 Replies
Message 1 of 5

Code (VB) for : If "value" contains "textA" then

Anonymous
Not applicable

Hi,

 

I don't know the good expression for to say "contains"

 I tried this, but this expression this means "equal"

= "textA"

And this expression doesn't work.

= "*textA*"

 

Have you an idea ?

0 Likes
Accepted solutions (1)
7,727 Views
4 Replies
Replies (4)
Message 2 of 5

MegaJerk
Collaborator
Collaborator

The below should clear it up 

Dim testString As String 
testString = "This is a string!"

Dim searchTerm As String 
searchTerm = "string"

' Using the 'Contains' method we can 
' determine if a character / string is inside
' of a different string. 

' The below will return True! 

MessageBox.Show(testString.Contains(searchTerm))

' here is another example. 

MessageBox.Show(testString.Contains("is"))

' here is an example of what happens when you search
' for a string that isn't inside of your searchable
' string. It returns a False if it can't find it! 

MessageBox.Show(testString.Contains("MegaJerk"))

Also see :  msdn - String.Contain()

Another way to do this would be to use InStr()

Dim testString As String 
testString = "This is a string!"

Dim searchTerm As String 
searchTerm = "string"

' The below line creates a dialog box showing '11'
' the word 'string' starts at the 11th character slot
' inside of our testString variable. 

MessageBox.Show(InStr(1,testString, searchTerm))

' here is another example. 

MessageBox.Show(InStr(1,testString, "is"))

' here is an example of what happens when you search
' for a string that isn't inside of your searchable
' string. It returns a 0 if it can't find it! 

MessageBox.Show(InStr(1,testString, "MegaJerk"))

Also see : msdn - InStr()



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 3 of 5

Anonymous
Not applicable

Thanks, I'm trying with my real property (part number).

 

Below original code

 

Dim oPartNumber As Property
Dim PartNumber As String
Set oPartNumber = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
        PartNumber = CStr(oPartNumber.Value)

 If oPartNumber.Value = "contains text" Then

 

My test, but it doesn't work, I've an error at "PartNumber"

 

Dim oPartNumber As Property
Dim PartNumber As String
Set oPartNumber = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
        PartNumber = CStr(oPartNumber.Value)

Dim searchTerm As String 
        searchTerm = "text"

 If oPartNumber.Value = PartNumber.Contains(searchTerm) Then
0 Likes
Message 4 of 5

MegaJerk
Collaborator
Collaborator
Accepted solution

It would seem that you’re using VBA, and not iLogic (which uses VB.net).

VBA does not have the method ‘String.Contains()’ and therefore cannot use it. It does however have the InStr method, so you can use that as follows.

 

 

Sub StringSearchTest()

    Dim oDoc As Document
    Set oDoc = ThisApplication.activeDocument
    
    Dim oPartNumber As Property
    Set oPartNumber = oDoc.PropertySets.Item _
    ("Design Tracking Properties").Item("Part Number")
    
    Dim oPartNumberValue As String
    oPartNumberValue = CStr(oPartNumber.value)
    
    Dim searchTerm As String
    searchTerm = "text"
    
    If InStr(1, oPartNumberValue, searchTerm) > 0 Then
        Debug.Print ("We found """ & searchTerm & """ :)")
    Else
        Debug.Print ("We didn't find """ & searchTerm & """ :(")
    End If
     
End Sub


If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 5 of 5

MechMachineMan
Advisor
Advisor

I believe Visual Basic also contains the LIKE operator. It's what I use in all of my VB.net coding.

 

A quick example would be:

 

oString Like "*" & oSearchString & "*"

 

This would look for the search string surrounded by any amount of any characters as it has 2 wildcars on the end.

This is probably the easiest way to accomplish the "contains" functionality.

 

 

https://msdn.microsoft.com/en-us/library/swf8kaxw.aspx

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes