Reading values from userform to many different VBA codes

Reading values from userform to many different VBA codes

almutaz_86
Advocate Advocate
1,756 Views
11 Replies
Message 1 of 12

Reading values from userform to many different VBA codes

almutaz_86
Advocate
Advocate

Good morning
Previously I asked about run VBA inside another VBA code HERE
and thanks for alfred.NESWADBA he gave me a great solution ,
but I faced a little problem I couldn't fix it
here is the code of userform1 :

Private Sub CommandButton1_Click()
Dim length As Double, width As Double, height As Double, center(0 To 2) As Double
Dim box1 As Acad3DSolid
center(0) = txt1 * 0.5: center(1) = txt2 * 0.5: center(2) = txt3 * 0.5
length = txt1: width = txt2: height = txt3
Set box1 = ThisDrawing.ModelSpace.AddBox(center, length, width, height)
ZoomAll
Call LeftBoring.ThisDrawing.LeftBoring
Call rightboring.ThisDrawing.rightboring
Unload Me
End Sub

and 2 codes for boring I will write one of them
here is the code of Leftboring :

Public Sub LeftBoring()
'NFR=  number of Rows
'NFC=  number of columns
'NFL=  number of levels
'DBR=  distance between Rows
'DBC=  distance between columns
'DBL=  distance between levels
Dim center(0 To 2) As Double, RADIUS As Double
Dim NFR As Long, NFC As Long, NFL As Long, DBR As Double, DBC As Double, DBL As Double
Dim RECTARRY As Variant
If txt2 < 24 Then
MsgBox "Length Should be equal or more than 24 mm."
Else
If txt2 < 60 Then
center(1) = txt2 / 2: center(0) = 9: center(2) = txt3: RADIUS = 2.5
Set circ = ThisDrawing.ModelSpace.AddCircle(center, RADIUS)
Else
If txt2 < 100 Then
center(1) = (txt2 - 32) / 2: center(0) = 9: center(2) = txt3: RADIUS = 2.5
Set circ = ThisDrawing.ModelSpace.AddCircle(center, RADIUS)
center(1) = ((txt2 - 32) / 2) + 32: center(0) = 9: center(2) = txt3: RADIUS = 4
Set circ = ThisDrawing.ModelSpace.AddCircle(center, RADIUS)
Else
If txt2 < 125 Then
center(1) = txt2 / 2: center(0) = 9: center(2) = txt3: RADIUS = 2.5
Set circ = ThisDrawing.ModelSpace.AddCircle(center, RADIUS)
center(1) = (txt2 - 64) / 2: center(0) = 9: center(2) = txt3: RADIUS = 4
Set circ = ThisDrawing.ModelSpace.AddCircle(center, RADIUS)
NFR = 1: NFC = 2: NFL = 1: DBR = 1: DBC = 64: DBL = 1
RECTARRY = circ.ArrayRectangular(NFC, NFR, NFL, DBC, DBR, DBL)
Else
If txt2 < 171 Then
center(1) = txt2 / 2: center(0) = 9: center(2) = txt3: RADIUS = 4
Set circ = ThisDrawing.ModelSpace.AddCircle(center, RADIUS)
center(1) = (txt2 - 64) / 2: center(0) = 9: center(2) = txt3: RADIUS = 2.5
Set circ = ThisDrawing.ModelSpace.AddCircle(center, RADIUS)
NFR = 1: NFC = 2: NFL = 1: DBR = 1: DBC = 64: DBL = 1
RECTARRY = circ.ArrayRectangular(NFC, NFR, NFL, DBC, DBR, DBL)
Else
MsgBox "Length Should be less than 171 mm."
End If
End If
End If
End If
End If
End Sub

End Sub
And I added the boring code as a reference to first code .
txt1 , txt2 and txt3 are textbox in userform1 .
when I try to run the code , its work good and give me message box from second code

MsgBox "Length Should be equal or more than 24 mm."

its appear if value is less than 24 mm, but in user form value is greater than 24 .


How to make the second code take values from  userform1 (txt1,txt2 and txt3) ?
I'll attach the .dvb files

0 Likes
Accepted solutions (1)
1,757 Views
11 Replies
Replies (11)
Message 2 of 12

almutaz_86
Advocate
Advocate
0 Likes
Message 3 of 12

Ed__Jobe
Mentor
Mentor
Accepted solution

You have several problems that can be addressed here. But first, the one causing your main problem. Your called sub does not have scope to the form so it thinks that "txt2" is a new variable, and it gets initialized to an empty text or "". I would change the signature of your sub so that you can pass an instance of the form. Then  you can use Me.txt2 rather than just txt2. For example:

Public Sub LeftBoring (frm As MyFormName)    'substitute the name of your form.

End Sub

Then call with:

Call LeftBoring(Me)

When you need to refer to a control on the form, use the instance of the form:

frm.txt2.Value

 

Now for problem 2. You didn't realize that you created a new variable within the context of LeftBoring. This is because you are not using the Option Explicit statement. It should be declared as the first line of all your modules as shown below. This option checks each new variable you type and makes sure that it was declared using the Dim statement. If not it throws a compile error. At this point you would have asked why does VBA think that txt2 is a new variable? Hopefully, you would have figured out that you have a scope problem. To have the VBA IDE add the Option Explicit statement each time you create a new module, you can turn this on in Tools>Options as shown in the second image.

Option Explicit.pngVBA Options.png

 

For problem 3, your nested If..Then..Else statements may allow some unwanted conditions. I would change to a Select..Case structure and change your logic to using values that require a variable to be between certain values. You do this using the And condition. For example:

Select Case frm.txt2.Value

   Case Is < 24

      Msgbox

   Case Is >= 24 And <= 72

      'process this case

End Select

 

If you are allowing real numbers rather than integers, you will need to change the values. The other advantage of using Select..Case is that as soon as a match is found, processing is done and the other cases do not get evaluated. Whereas in your previous code, all the If statements get evaluated, taking longer to process.\

 

The last area is something that would make your code easier to read. If your code is easy to read and well commented, then you or others will have an easier time editing it later. While you can Dim multiple vars on the same line, its harder to read and comment. I would make it like the following.

Dim NFR As Long   'Number of Rows

Dim NFC As Long   'Number of Columns

Dim NFL As Long   'Number of Levels

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 12

almutaz_86
Advocate
Advocate

Good evening @Ed__Jobe

There is alot things to learn from your reply 🙂

 

I tried to do these changes, but give me no result, I think that I made it wrong.

 

If you may to rewrite the code, please 

0 Likes
Message 5 of 12

Ed__Jobe
Mentor
Mentor

@almutaz_86 wrote:

If you may to rewrite the code, please 


Rewrite what? You haven't posted anything. A dvb would be nice.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 12

almutaz_86
Advocate
Advocate
0 Likes
Message 7 of 12

Ed__Jobe
Mentor
Mentor

Sorry, I thought that was just some sample code. Is there a reason you have to have 3 dvb's? Having only one would be easier to maintain.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 8 of 12

almutaz_86
Advocate
Advocate

Yes @Ed__Jobe , the original boring code is about 1500 line its very hard to put it in same dvb file, 

And maybe I use it more than 10 times!! 

So I wrote some dvb codes, and call them is the easiest way 

 

The DVBs in the attachment code it just a sample

 

0 Likes
Message 9 of 12

Ed__Jobe
Mentor
Mentor

1500 lines is nothing, especially if you keep your code organized and readable. I've attached an updated dvb. Since these were not the actual code, I combined the 3 dvb's to show you how to organize it. I commented everything I did. Notice the added modules and renaming. Creating meaningful names that follow a naming convention helps you to read the code easier.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 10 of 12

almutaz_86
Advocate
Advocate

Thank you @Ed__Jobe

But its complicated more than that 

In original code bores are cylinders not circles with 3d rotat. 

 

Some piece needs bores from 5 faces(box faces) .. Its mean 7500 line 😅 and some design maybe contain 20 pieces. 

 

Absolutely its hard to read

 

0 Likes
Message 11 of 12

Ed__Jobe
Mentor
Mentor

I understand that its more complicated. The examples I gave you were meant for you to learn from so that you could clean up your code and make it easier to manage.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 12 of 12

almutaz_86
Advocate
Advocate

Thank you @Ed__Jobe 
Its working perfectly, Good Job 

 

 

0 Likes