Consulting

Results 1 to 6 of 6

Thread: Solved: Combo Boxes

  1. #1
    VBAX Contributor
    Joined
    Nov 2006
    Posts
    107
    Location

    Solved: Combo Boxes

    I equate a combo box value to a integer variable in my userform. If i leave the combo box field empty and equate i get run-time error 13. How can i get rid of this problem?

    examaple code :

    Dim Temp as integer

    Temp=CmbBox1.value

  2. #2
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    Temp=int(CmbBox1.value) or Temp=val(CmbBox1.value). Can't remember which it is in VBA.

    CmbBox1.value is a string.

    Sorry, misread your post. Please ignore
    Glen

  3. #3
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    [vba]
    Dim Temp as integer

    Temp=iif(CmbBox1.value="",0,cmbBox1.value)
    [/vba]

    or
    [vba]
    if len(cmbbox1.value)=0 then
    temp = 0
    else
    temp = cmbbox1.value
    End if
    [/vba]

    HTH
    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  4. #4
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hi pico,

    moa's code would work, particularly:

    [VBA]
    Temp=val(CmbBox1.value)
    [/VBA]

    If it is empty it will default to 0.

    Or you could do a preliminary check to see if there's any text in the combo box to begin with:

    [VBA]If CmbBox1.Value <> "" Then Temp = CmbBox1.Value[/VBA]




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

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

    If CmbBox1.ListIndex = -1 Then
    Temp = 0
    Else
    Temp = CmbBox1.Value
    End If
    [/vba]

  6. #6
    VBAX Contributor
    Joined
    Nov 2006
    Posts
    107
    Location
    Thank you All

Posting Permissions

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