Trigger Error MessageBox if calling a parameter which doesn't exist

Trigger Error MessageBox if calling a parameter which doesn't exist

emanuel.c
Collaborator Collaborator
324 Views
3 Replies
Message 1 of 4

Trigger Error MessageBox if calling a parameter which doesn't exist

emanuel.c
Collaborator
Collaborator

I have some code which is used for various parts. The code uses a few part parameters, but since some parts have certain parameters and others don't, I'm defining the parameters under Select Cases. It gets tedious to make sure all the parameters and errors are called for and only those.

 

Could I have it that if ever within the code  a certain parameter is called for, but it doesn't exist, then trigger the error message (there is Sub Main and multiple functions)?

 

Thanks!

 

Here is a piece of the code if it helps to describe the situation...

 

Select Case Product(Part)
		
Case "02" 'Pipe and DOM
	ParamParT = FindParameter(invdoc, "ParT")
	ParamDiam = FindParameter(invdoc, "Diam")
	
	If ParamParT Is Nothing Then
		MessageBox.Show(error1() + vbCrLf + error2, error3 & "ParT")
	Else
		pipe_wall = Parameter("ParT")
	End If
	
	If ParamDiam Is Nothing Then
		MessageBox.Show(error1() + vbCrLf + error2, error3 & "Diam")
	Else
		Diam = Parameter("Diam")
	End If
	
Case "03", "05" 'Angles and Tubes
	ParamGT = FindParameter(invdoc, "G_T")
	ParamGH = FindParameter(invdoc, "G_H")
	ParamGW = FindParameter(invdoc, "G_W")
	
	If ParamGT Is Nothing Then
		MessageBox.Show(error1() + vbCrLf + error2, error3 & "G_T")
	Else
		GT = Parameter("G_T")
	End If	
	
	If ParamGH Is Nothing Then
		MessageBox.Show(error1() + vbCrLf + error2, error3 & "G_H")
	Else
		GH = Parameter("G_H")
	End If	
	
	If ParamGW Is Nothing Then		
		MessageBox.Show(error1() + vbCrLf + error2, error3 & "G_W")
	Else
		GW = Parameter("G_W")
	End If

Case "04" 'Flat bars
	ParamGT = FindParameter(invdoc, "G_T")
	ParamGW = FindParameter(invdoc, "G_W")
	
	If ParamGT Is Nothing Then
		MessageBox.Show(error1() + vbCrLf + error2, error3 & "G_T")
	Else
		GT = Parameter("G_T")
	End If	
	
	If ParamGW Is Nothing Then
		MessageBox.Show(error1() + vbCrLf + error2, error3 & "G_W")
	Else
		GW = Parameter("G_W")
	End If

 

0 Likes
325 Views
3 Replies
Replies (3)
Message 2 of 4

A.Acheson
Mentor
Mentor

A simple solution is to wrap it all in a Try Catch End Try statement. If anything fails then you get an error message but it won't be too specific. Or if you want to be more specific put each parameter function search in a try catch.

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 4

emanuel.c
Collaborator
Collaborator

Well, maybe I should have given more info. Through out the code there are many Select cases (based on an iProperty "Product"). Basically, all the parts coming from the Content Center already have the parameters used in this code. But in case a part (tube for example) is drawn outside the Content Center, those Parameters ("G_T", "G_H", "G_W") must exist in the part, for the code to work.

 

At the beginning I have defined a class so as to use global variables. There's a function that searches for parameters and returns nothing or the value in case it's found. This Select Case here looks through the different product types and defines the global variables to be used later in the code.

 

For example tube shapes don't need Parameter("Diam") so it doesn't need to be defined. Flat bars use "G_T" and "G_W" but not "G_H".  If I would use Try-Catch for every parameter it would trigger errors for parts that don't need to have that certain parameter.

 

I'll attach the code here in .txt file because it's very long. Maybe you can have an idea of what's going on. It's working well the way it is, but it is fairly bushy and I wish I could slim it down if I could... Anyway my question is with regards to line 23 to about 207.

 

Thank you much!

0 Likes
Message 4 of 4

TechInventor20
Advocate
Advocate

What @A.Acheson is saying I would also use try catch statement. Here is a way to use it properly.

 

	Try
	Parameter("part:1", "Parameter_name") = Value
	Catch
Msgbox("Parameter does not exists") End Try
0 Likes