Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

True/False for a string value

Status
Not open for further replies.

LucasC

Automotive
Feb 18, 2019
157
US
I'm trying to make a msgbox display when a named table(there are multiple tables i'm looping through) is not found on a V5 drawing. if the correct table is found a user form launches.

I had everything working when just using an item index but wanted to add the loop just in case another table is added in the future. Can anyone point out where I went off the road and crashed in the ditch?


Code:
'check if correct frame is used
Dim DrawingTables As DrawingTables
Set DrawingTables = drawingDocument1.sheets.ActiveSheet.views.Item("Background View").Tables

Dim I As Integer
For I = 1 To DrawingTables.Count

Dim TableName As DrawingTable
Set TableName = DrawingTables.Item(I)
Next

For Each TableName In DrawingTables

If TableName.Name = "Revision_Text_Table" Then
Revisions.Show vbModeless

End If
Next
[COLOR=#73D216]'code operates as expected to this point[/color]
[COLOR=#4E9A06]
'I tried the below string compare, if <> then, and if is not then. I added the for each loop thinking that I might need it in order to solve the code.[/color]
For Each TableName In DrawingTables
If StrComp(TableName.Name, "Revision_Text_Table") = Null Then
MsgBox ("Current Drawing Frame Not Compatible With Macro.") & vbNewLine & ("Replace Sheet Background Or Check Table Names ."), vbInformation
Exit Sub
End If
Next

End Sub
 
Replies continue below

Recommended for you

According to the documentation on StrComp it returns integer (-1, 0, 1) not Null.

In such scenarios I am making use of the fact that for..each sets iterator variable to Null once it reaches the end of a collection.

Code:
For each tbl in DrawingTables
  If tbl.Name = "Desired_Name" then
    Exit for
  End if
Next
If IsNull(tbl) then
  Msgbox "not found"
End If
 
no luck.

If I change the table name in V5, I get "Object variable or With block variable not set" at the msgbox I added just to verify what string was passing through.

I think this is the problem, strcomp doesn't have 2 valid strings to compare and I don't know how to fix that error. I don't understand why the value passes to the msgbox for 1 case and not the other.

example:
TableItem.Name = "Revision_Text_Table" msgbox displays the name. when it is something different like "Revision_Text_Ta" I receive the error.

Code:
'check if correct frame is used
Dim DrawingTables As DrawingTables
Set DrawingTables = drawingDocument1.sheets.ActiveSheet.views.Item("Background View").Tables

Dim I As Integer
For I = 1 To DrawingTables.Count

Dim TableItem As DrawingTable
Set TableItem = DrawingTables.Item(I)
Next

For Each TableItem In DrawingTables

If TableItem.Name = "Revision_Text_Table" Then
Revisions.Show vbModeless
Exit For

End If
Next

MsgBox (TableItem.Name)

[COLOR=#4E9A06]'works to here when the table is named "Revision_Text_Table", fails if I change the V5 drafting table name to something like "Revision_Text_Ta"[/color]
 
I don't understand why the value passes to the msgbox for 1 case and not the other.

Well, that's exactly what I wrote:

"...the fact that for..each sets iterator variable to Null once it reaches the end of a collection"

When table is not found, TableName becomes Null ones for..each is over and it causes "object is not set" error.

Why don't you use the pattern I provided?
 
I did try your example before my last post, It gave the same object not set error. Sorry if that wasn't clear. While I was trying other ideas, it would sometimes perform Revisions.show AND display my real msgbox like i wanted. That's what lead me to test what was passing through with the simple msgbox below in the 2nd code sample.

Code:
For Each TableItem In DrawingTables

If TableItem.Name = "Revision_Text_Table" Then
Revisions.Show vbModeless
Exit For

End If
Next

If IsNull(TableItem.Name) Then [highlight #73D216]Object not set error occurred here[/highlight]
MsgBox ("blah blah shortened for post")
End If

If it becomes null, why did it pass through and display in the msgbox I created? Based on that logic, shouldn't it also pass the string through after I changed it? I'm self taught so maybe I don't know some(probably most) of the VB rules.

Code:
For Each TableItem In DrawingTables

If TableItem.Name = "Revision_Text_Table" Then
Revisions.Show vbModeless
Exit For

End If
Next

MsgBox (TableItem.Name)[highlight #73D216]It performed the Revisions.show above and populated this msgbox[/highlight]
 
I got it to work.

Please point out if my method is bad practice or if there was a better way. [ponder]

Thanks!

Code:
'check if correct frame is used
Dim DrawingTables As DrawingTables
Set DrawingTables = drawingDocument1.sheets.ActiveSheet.views.Item("Background View").Tables

Dim I As Integer
For I = 1 To DrawingTables.Count

Dim TableItem As DrawingTable
Set TableItem = DrawingTables.Item(I)
Next

For Each TableItem In DrawingTables

If TableItem.Name = "Revision_Text_Table" Then
Revisions.Show vbModeless
Exit Sub [highlight #73D216]Added this after it worked when the correct table was present but still was seeing one of the incorrect tables in the collection below[/highlight]
End If

Next

[highlight #73D216]I figured if the value was null after the operation above, I would call it again[/highlight] 

Set DrawingTables = drawingDocument1.sheets.ActiveSheet.views.Item("Background View").Tables
For I = 1 To DrawingTables.Count
Set TableItem = DrawingTables.Item(I)



If (TableItem.Name) <> ("Revision_Text_Table") Then
MsgBox ("Current Drawing Frame Not Compatible With Macro.") & vbNewLine & ("Replace Sheet Background Or Check Table Names ."), vbInformation
Exit Sub
End If

Next
 
Ok, I get it. The key here is how you named (and how you think of) your variable - "TableName".

You probably has a feeling that "TableName.Name" is some magic string (like postal code) for retrieving name of a table. And if there's no table it should retrieve an empty string.

Well, it's not. CATIA API is object-oriented, which means there's an object of type named "table" and that object has "Name" property.

For..Each iterates through objects, again not "magic strings" and sets iterator variable to Null.
When there's no object (variable is set to null) you get an error.

By the way, you said that you tried my code, but that is NOT what I wrote:

If IsNull(TableItem.Name)

I wrote: If IsNull(TableItem)
 
I have 4 functions that require this code, I'll try your way without the .name and see what happens.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top