Retrieving numeric value from dropdown selection for calculation

I am trying to get the number selected by someone from a Dropdown, to use it in a risk score. Here is the code:

Function dropdownBCA50plus_onclick()
  dropdownBCA50plus.value = dropdownBCA50plus.selection
  Score = Score + (CInt(dropdownBCA50plus.selection) * 3)
  MsgBox CStr(Score)
End Function

I’ve tried lots of variations, but it is always “NaN” (Not a Number).

Any help would be much appreciated!

Mark

Tip : If you’re pasting code, html or config files, surround the code with triple back ticks (```), before the first line and after the last one. It will be formatted properly. (We fixed it for you this time)

What is in dropdownBCA50plus.value?

The man! George, hope you’re well. I just retired from my university and have some pet programming projects I’m finally getting back to. First is an app for people to quickly figure out which cancer screenings they are due for. Having a heck of a time with the new way to place controls. End up using lots of negative numbers for top to get it to line up. And grabbing the index isn’t working as it does using MS version of VB. Here is the value for dropdownBCA50plus.value:

<img width="423" height="332" style="width:4.4062in;height:3.4583in" id="Picture_x0020_3" src="/uploads/db0741/original/2X/9/9a81d145bb6dcc86ee0abaf2f1f916b78225aaa7.png" alt="A screenshot of a computer

Description automatically generated">

And code:

Dim Score = 0
Dim Num

Function dropdownBCA50plus_onclick()
  dropdownBCA50plus.value = dropdownBCA50plus.selection
  MsgBox dropdownBCA50plus.value
  Score = Score + (CInt(dropdownBCA50plus.selection) * 3)
  MsgBox CStr(Score)
End Function

Function dropdownBCAunder50_onclick()
  dropdownBCAunder50.value = dropdownBCAunder50.selection
  Score = Score + (dropdownBCAunder50.value * 4)
End Function

Function dropdownOvarian_onclick()
  dropdownOvarian.value = dropdownOvarian.selection
  Score = Score + (dropdownOvarian.value * 5)
End Function

Function dropdownMaleBCA_onclick()
  dropdownMaleBCA.value = dropdownMaleBCA.selection
  Score = Score + (dropdownMaleBCA.value * 8)
End Function

Function dropdownAshkenazi_onclick()
  dropdownAshkenazi.value = dropdownAshkenazi.selection
  Score = Score + (dropdownAshkenazi.value * 4)
  MsgBox("Score is " & CStr(Score))
End Function

Function brnBack_onclick()
  ChangeForm(StartForm)
End Function

Thanks, Mark

dropdownBCA50plus.selection is also 2. But can’t tell if string or numeric

(I cleaned up your code again)

I’d put some more tracing in. What is Score? What is (CInt(dropdownBCA50plus.selection) * 3)?

Code

Dim Score = 0

Function dropdownBCA50plus_onclick()

MsgBox “Score at baseline is: “ & Score ‘ THIS YIELDS 0

dropdownBCA50plus.value = dropdownBCA50plus.selection

MsgBox "Product of selection x 3 is: " & CInt(dropdownBCA50plus.selection) * 3 ‘ THIS CORRECTLY YIELDS 6 BELOW

Score = Score + (CInt(dropdownBCA50plus.selection) * 3)

MsgBox "Score is: " & Score ‘ THIS YIELDS NaN

End Function

The product of (CInt(dropdownBCA50plus.selection) * 3) is 6 and it did the math correctly:

<img width=“453” height=“162” style=“width:4.7187in;height:1.6875in” id=“Picture_x0020_3” src=“/uploads/db0741/original/2X/c/cc075c80f6220cb189f936beee0d5b46473f80db.png” alt="A screenshot of a computer

Description automatically generated">

But when I try to add it to the score Score I get NaN:

<img width=“197” height=“123” style=“width:2.052in;height:1.2812in” id=“Picture_x0020_4” src=“/uploads/db0741/original/2X/1/116c1250f0cf95424f4be42e959b0b2a0d69a469.png” alt="A screenshot of a computer screen

Description automatically generated">

Isn’t there a property that tells you the number of the selection in the array of dropdown values that is parallel to “.index” in MS VB? So the first item (default) is 0, next 1, then 2, etc as integer values?

And how do I declare a variable to be a number. I’ve read about Dim and in standard VB you say “Dim Score as integer” for example but that is not allowed. Seems like it may be a problem with mixing strings and integers?

Best,

Mark

Let me know if you have questions about formatting your code properly. It makes it much easier to help you!

I am retrieving a numeric value from a dropdown. I would like to use that numeric value in a risk score, adding several of these values together. But as described above I repeatedly get “NaN” despite using CInt() to convert any string to a number before trying to add them.

Have a look at the sample code in the Wiki:

Function Dropdown1_onclick(s)
  If typeof(s)="object" Then return
  MsgBox s & " " & Dropdown1.selection 
End Function

Notice the second statement? Sometimes this function gets called without the selection having been made yet. In that case, the value is NaN. (Not a Number)

@markebell , It might be worth seeing if Dropdown also sends an onchange event. That way you can get the final value after all the up and down of the clicking is done.

Great idea, thanks!