Weird VBA occurance

Weird VBA occurance

Jacques.Grobler
Advocate Advocate
512 Views
5 Replies
Message 1 of 6

Weird VBA occurance

Jacques.Grobler
Advocate
Advocate

Hi everyone

 

I am experiencing the weirdest thing

 

I have a userform in VBA that is drawing info from my model (a Double value), I have declared my Double value as a space holder, I get the value from the model and pass it to the space holder and with the space holder I do a calculation.  Then I pass the space holder and the calculated results to the textboxes on my form.

 

Sounds about right?  That is what I also thought. My problem occurs when I pass the spaceholder value to the calculations and the calculations return a 0 when another value is expected. When I pass the model value directly to the textbox, then the textbox to the spaceholder and then the spaceholder to the calculation I get the results I expect.

 

Why is this happening and what can I do to fix it?

0 Likes
513 Views
5 Replies
Replies (5)
Message 2 of 6

Jacques.Grobler
Advocate
Advocate

Has anyone else found some weird behavious with VBA recently?

0 Likes
Message 3 of 6

bshbsh
Collaborator
Collaborator
I'm not sure if I understood the problem right, could you post the code?
0 Likes
Message 4 of 6

Jacques.Grobler
Advocate
Advocate

Ok, this is getting even weirder...

 

I have firgured out that the value "116" is giving me a problem which results in me not getting the value returned I need...

 

 

Function Spacing(Size As Double) As Double

    If Size = 116 Then
        Spacing = 150 'mm
    ElseIf Size = 120 Then
        Spacing = 160 'mm
    End If

End Function

 

If I run the code in the Immediate window I get my answer, but as soon as I pass my value from the form as I do below I get a "0" returned

 

 

 

Option Explicit

Public Diameter As Double
Public DiaSpacing As Double

Private Sub UserForm_Initialize()

    'Retrieve EXISTING parameters from model
    Call param_RetieveForForm

    'Populate form
    Call form_Populate

    'Calculate all DIMENSIONS
    Call calc_Dims

End Sub

Private Sub param_RetieveForForm()
    Diameter = param_Get("Diameter")
End Sub

Private Sub form_Populate()
    Diameter_txt.Value = Diameter
End Sub

Sub calc_Dims()
    DiaSpacing  = Spacing_Centres(bag_Diameter)
End Sub

Function param_Get(paramName As String)

    ' Get the Parameters object.  This uses error handling and will only be successful
    ' if a part or assembly document is active.
    Dim oParam As Parameters
    Set oParam = ThisApplication.ActiveDocument.ComponentDefinition.Parameters
    On Error Resume Next

    'Get the Parameter required
    Dim oParamName As Parameter
    Set oParamName = oParam.Item(paramName)

    'Get the Parameter units
    Dim oParamUOM As UnitsOfMeasure
    Set oParamUOM = ThisApplication.ActiveDocument.ComponentDefinition. _
                    Parameters.Item(paramName).Units

    'Get the value of parameter
    If oParamUOM = "mm" Then
        param_Get = oParamName.Value * 10
    End If

End Function

 

 

0 Likes
Message 5 of 6

Boorda
Advocate
Advocate
Looks like you are calling Spacing_Centers and your function's name is just Spacing. Also i see a Diameter variable defined but not sure where you are pulling the bag_Diameter from. These could be your issues.

Automation is key!
0 Likes
Message 6 of 6

Jacques.Grobler
Advocate
Advocate

Uhm, sorry, I had to isolate my code and because of IP had to rename a lot of stuff...

 

Spacing_Centres was meant to be Spacing and bag_Diameter should read Diameter, but no, these are not my issues...

 

As I said, I have changed code, isolated code, tested code, changed ordering and everything points to the "116 as an issue.

 

I am able to retrieve value for "120" without issues, but not the "116", if I change "116" to "115" or anything else it works. When I then change it back to "116", issues again

0 Likes