Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert combo box selection to numeric data type

Status
Not open for further replies.

instructorTek

Instructor
Jun 5, 2006
61
0
0
Hi all I have a combo box that is updating text boxes on a form and after the user makes their selections a button is clicked and the sum of the numbers in the text boxes are found. My problem is that although the information selected from the combo box is a number (this comes from a table), when it updates the text boxs it seems to do so as text, because when I click the button to find the sum of the numbers, I am just getting a concatenation of values rather than an addition. E.g. 2 + 2 = 22
What can I do to get the sum being performed?

Option Compare Database
Dim Cost, PriceCam, PriceCase as Single

‘The user makes a selection
Private Sub cboSelect_AfterUpdate()
txtPriceCase.SetFocus
Me![txtPriceCase].Value = Me![cboSelect].Column(3)
PriceCase = txtPriceCase.Value
Me![txtPriceCam].Value = Me![cboSelect].Column(4)
PriceCam = txtPriceCam.Value
Me.Refresh
End Sub

‘The user clicks here to find the sum
Private Sub cmdCost_Click()
txtCost.SetFocus
Cost = PriceCam + PriceCase
txtCost.Value = Cost
End Sub
 
Is there some way? I really, really need to be able to perform the addition.
 
Use one of the conversion functions:
Code:
Private Sub cmdCost_Click()
  If IsNumeric(PriceCam) And IsNumeric(PriceCase) Then
    txtCost.SetFocus
    Cost = CLng(PriceCam) + CLng(PriceCase)
    txtCost.Value = Cost
  End If
End Sub

If there are decimal places you might use CCur() or CDbl() instead of CLng.


 
Me again.

Just noticed your Cost variable is a Single, so you might want to use CSng().


 
I'd use this:
Cost = Val(PriceCam) + Val(PriceCase)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top