Consulting

Results 1 to 5 of 5

Thread: conversion of variant

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location

    conversion of variant

    This is the code that i have.
    [vba]
    Private Sub CommandButton1_Click()
    If CheckBox1.Value = False And CheckBox2.Value = False Then
    TextBox3.Text = 0
    End If
    If CheckBox1.Value = True Then
    TextBox3.Text = TextBox1.Text
    Else
    If CheckBox2.Value = True Then
    TextBox3.Text = TextBox2.Text
    End If
    End If
    If CheckBox1.Value = True And CheckBox2.Value = True Then
    TextBox3.Text = CVar(TextBox1.Text) + CVar(TextBox2.Text)
    End If
    End Sub[/vba]

    The problem that I have is the values are not being added. it seems they are kept as strings.
    how can I add the values together, and not just display the values next to each other?

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Are they numbers? Why CVar()? Why not CDbl() and covert as numbers?

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Val(TextBox1.Text)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Try this.
    [vba]
    Private Sub CommandButton1_Click()
    TextBox3.Text = -CheckBox1.Value * Val(TextBox1.Text) - CheckBox2.Value * Val(TextBox2.Text)
    End Sub[/vba]

  5. #5
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Sweet!!!! thank you guys and thanks XLD. the val method worked out very well and that was easy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •