PDA

View Full Version : Solved: Need Help with calculation inside FORM



marshallgrad
08-28-2008, 06:34 AM
I need some help from the board.

We use a terminal emulator that has Visual Basic for applications built into it. I am designing a form and am having some trouble with a calculation.

Form name: DILUTION_FACTOR

I have 3 labels and 3 corresponding text boxes, one combobox, and 3 commandbuttons


Label1 ==> textbox1
Label2 ==> textbox2
Label3 ==> textbox3

Combobox1

Commandbutton1 ? CALCULATE
Commandbutton2 ? CLEAR_ENTRY
Commandbutton3 ? CANCEL

The form is supposed to allow the user to enter a numerical value into textbox1 and textbox2 and then perform a calculation and display the result in textbox 3 when the CALCULATE commandbutton is pressed.

My problem that I have is in my calculation instead of performing a numerical calculation it is simply concatenating the two variables SAMPLEVOLUME + DILUENTVOLUME. Since I am relatively new to VB I was hoping someone could point out what I am doing wrong in my coding or variables?

Thanks.
Butch

My code looks like this.


Private Sub CALCULATE_Click()
'MAKE CALCULATIONS HERE

Dim SAMPLEVOLUME, DILUENTVOLUME, DILUTION As Variant

If DILUTION_FACTOR.ComboBox1.Value = "DILUTION" Then
'FIND THE DILUTION RATIO
SAMPLEVOLUME = DILUTION_FACTOR.TextBox1.Value
DILUENTVOLUME = DILUTION_FACTOR.TextBox2.Value
DILUTION = ((SAMPLEVOLUME + DILUENTVOLUME) / SAMPLEVOLUME)
msgbox DILUTION
'using msgbox for testing purposes only...
ElseIf DILUTION_FACTOR.ComboBox1.Value = "SAMPLE VOLUME" Then
'FIND THE SAMPLE VOLUME NEEDED
ElseIf DILUTION_FACTOR.ComboBox1.Value = "DILUENT VOLUME" Then
'FIND THE DILUENT VOLUME NEEDED
End If

End Sub
-----------------------------------------------------------------------------
Private Sub CLEAR_ENTRY_Click()

Unload DILUTION_FACTOR
DILUTION_FACTOR.Show

End Sub
-----------------------------------------------------------------------------
Private Sub CANCEL_Click()

Unload DILUTION_FACTOR
Call Login.STOPMACRO

End Sub
-----------------------------------------------------------------------------
Private Sub ComboBox1_Change()

If ComboBox1.Text = "DILUTION" Then
DILUTION_FACTOR.Label3.Visible = False
DILUTION_FACTOR.TextBox3.Visible = False
DILUTION_FACTOR.Label2.Visible = True
DILUTION_FACTOR.TextBox2.Visible = True
DILUTION_FACTOR.Label1.Visible = True
DILUTION_FACTOR.TextBox1.Visible = True
ElseIf ComboBox1.Text = "SAMPLE VOLUME" Then
DILUTION_FACTOR.Label2.Visible = False
DILUTION_FACTOR.TextBox2.Visible = False
DILUTION_FACTOR.Label1.Visible = True
DILUTION_FACTOR.TextBox1.Visible = True
DILUTION_FACTOR.Label3.Visible = True
DILUTION_FACTOR.TextBox3.Visible = True
ElseIf ComboBox1.Text = "DILUENT VOLUME" Then
DILUTION_FACTOR.Label1.Visible = False
DILUTION_FACTOR.TextBox1.Visible = False
DILUTION_FACTOR.Label2.Visible = True
DILUTION_FACTOR.TextBox2.Visible = True
DILUTION_FACTOR.Label3.Visible = True
DILUTION_FACTOR.TextBox3.Visible = True
End If

End Sub
-----------------------------------------------------------------------------
Private Sub USERFORM_INITIALIZE()

With DILUTION_FACTOR.ComboBox1

'USE THIS TO POPULATE THE ENTRIES IN THE COMBOBOX.
'SINCE YOU MOST LIKELY WILL BE LOOKING TO CALCULATE THE DILUTION WE CAN SET
'A DEFAULT.

ComboBox1.AddItem "DILUTION"
ComboBox1.AddItem "SAMPLE VOLUME"
ComboBox1.AddItem "DILUENT VOLUME"

'=================================
ComboBox1.Text = "DILUTION"
'=================================

End With

End Sub

rocheey
08-28-2008, 10:35 AM
>>My problem that I have is in my calculation instead of performing a numerical calculation it is simply concatenating the two variables SAMPLEVOLUME + DILUENTVOLUME. Since I am relatively new to VB I was hoping someone could point out what I am doing wrong in my coding or variables?

Without looking at all the code, the most common occurance of this, is that VB is getting confused with what type of data your variables are.

You originally have them stored in a text box, and also use variants as variables. variants can be used for just about any type of data, letters, numbers, etc.

VB is usually pretty good at deciphering just what type of data is stored in a variant, but since it's having trouble, you should explicitly describe them as numbers.

Try something like this:
'-------------------------------------------------------------
Dim SAMPLEVOLUME, DILUENTVOLUME, DILUTION As DOUBLE

SAMPLEVOLUME = CDBL(DILUTION_FACTOR.TextBox1.Text)
DILUENTVOLUME = CDBL(DILUTION_FACTOR.TextBox2.Text)
'--------------------------------------------------------------

describing your 3 variables as Doubles eliminates any confusion as to what type of data you are dealing with.

the "CDBL" you see, stands for "ConvertDouble", and converts other variables (like the TEXT in your TEXT boxes) into double-precision numbers.

marshallgrad
08-28-2008, 11:22 AM
Forcing the variables to be explicitly read as DOUBLES did the trick. The form now works AS intended and the two variables no longer concatenate. Thanks for the wonderful suggestion !!!