# type mismatch

Page 1 of 1

## 9 Replies - 1216 Views - Last Post: 30 August 2012 - 01:59 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=290422&amp;s=af92ce27f540882ff734705f151582c8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cerealkiller17

Reputation: 0
• Posts: 6
• Joined: 29-August 12

# type mismatch

Posted 29 August 2012 - 11:44 PM

```Private Sub cmdcalculate2_Click()
Dim total2 As Currency
Dim cufee1 As Currency
Dim med2 As Currency
Dim labexam1 As Currency

cufee1 = Me.txtcufee1.Text
[b]med2 = Me.txtmed2.Text[/b]
labexam1 = Me.txtlabexam1.Text

If cufee1 >= 0 And med2 >= 0 And labexam1 >= 0 Then
total2 = Val((Me.txtcufee1.Text) + (Me.txtmed2.Text) + (Me.txtlabexam1.Text))
labeltotal2.Caption = total2
Exit Sub
ElseIf cufee1 >= 0 And med2 >= 0 Then
total2 = Val((Me.txtcufee1.Text) + (Me.txtmed2.Text))
labeltotal2.Caption = total2
Exit Sub
ElseIf cufee1 >= 0 And labexam1 >= 0 Then
total2 = Val((Me.txtcufee1.Text) + (Me.txtlabexam1.Text))
labeltotal2.Caption = total2
Exit Sub
ElseIf cufee1 >= 0 Then
labeltotal2.Caption = total2
Exit Sub
End If

End Sub
```

i have this code and the error is type mismatch and it points to the one that is in bold text .. can anyone help me in this ? please.. TNX :*

This post has been edited by Atli: 29 August 2012 - 11:57 PM
Reason for edit:: Use [code] tags when posting code.

Is This A Good Question/Topic? 0

## Replies To: type mismatch

### #2 maj3091

• D.I.C Lover

Reputation: 316
• Posts: 1,883
• Joined: 26-March 09

## Re: type mismatch

Posted 29 August 2012 - 11:57 PM

Give an example of what you have in txtmed2.

Seeings how you doing exactly the same thing in the line above, just using a different variable and textbox (assuming they are textboxes), then it should work.

### #3 cerealkiller17

Reputation: 0
• Posts: 6
• Joined: 29-August 12

## Re: type mismatch

Posted 30 August 2012 - 12:03 AM

maj3091, on 29 August 2012 - 11:57 PM, said:

Give an example of what you have in txtmed2.

Seeings how you doing exactly the same thing in the line above, just using a different variable and textbox (assuming they are textboxes), then it should work.

what example do you mean ? im sorry im just a beginner

### #4 maj3091

• D.I.C Lover

Reputation: 316
• Posts: 1,883
• Joined: 26-March 09

## Re: type mismatch

Posted 30 August 2012 - 12:09 AM

What do you have entered in the textbox.

i.e.: what is the value of txtmed2.text

### #5 cerealkiller17

Reputation: 0
• Posts: 6
• Joined: 29-August 12

## Re: type mismatch

Posted 30 August 2012 - 12:11 AM

maj3091, on 30 August 2012 - 12:09 AM, said:

What do you have entered in the textbox.

i.e.: what is the value of txtmed2.text

it has no value .. i just type a random number in the first textbox .. (txtcufee1.text) then the rest has no value ..

### #6 maj3091

• D.I.C Lover

Reputation: 316
• Posts: 1,883
• Joined: 26-March 09

## Re: type mismatch

Posted 30 August 2012 - 12:20 AM

cerealkiller17, on 30 August 2012 - 08:11 AM, said:

maj3091, on 30 August 2012 - 12:09 AM, said:

What do you have entered in the textbox.

i.e.: what is the value of txtmed2.text

it has no value .. i just type a random number in the first textbox .. (txtcufee1.text) then the rest has no value ..

Then it needs to have a value...you can't convert an empty string to a currency, hence the Type Mismatch error.

You really should check the contents of the textbox before trying to change types.

For example:

```' Check if something is entered.
if len(txtmed2.text)>0 then
'carry on
else
'textbox is empty
endif

' check if it's a number
if isnumeric(txtmed2.text) then
'carry on
else
'textbox doesn't contain a number
endif

```

This post has been edited by maj3091: 30 August 2012 - 12:21 AM

### #7 cerealkiller17

Reputation: 0
• Posts: 6
• Joined: 29-August 12

## Re: type mismatch

Posted 30 August 2012 - 12:26 AM

maj3091, on 30 August 2012 - 12:20 AM, said:

cerealkiller17, on 30 August 2012 - 08:11 AM, said:

maj3091, on 30 August 2012 - 12:09 AM, said:

What do you have entered in the textbox.

i.e.: what is the value of txtmed2.text

it has no value .. i just type a random number in the first textbox .. (txtcufee1.text) then the rest has no value ..

Then it needs to have a value...you can't convert an empty string to a currency, hence the Type Mismatch error.

You really should check the contents of the textbox before trying to change types.

For example:

```' Check if something is entered.
if len(txtmed2.text)>0 then
'carry on
else
'textbox is empty
endif

' check if it's a number
if isnumeric(txtmed2.text) then
'carry on
else
'textbox doesn't contain a number
endif

```

ill try this .. thanks !! :*

### #8 cerealkiller17

Reputation: 0
• Posts: 6
• Joined: 29-August 12

## Re: type mismatch

Posted 30 August 2012 - 12:34 AM

thank you so much !! yay !!

### #9 maj3091

• D.I.C Lover

Reputation: 316
• Posts: 1,883
• Joined: 26-March 09

## Re: type mismatch

Posted 30 August 2012 - 01:35 AM

Can you post your modified code using the [ CODE] tags from the toolbar.

This allows others to see how you solved your problem in the future.

### #10 cerealkiller17

Reputation: 0
• Posts: 6
• Joined: 29-August 12

## Re: type mismatch

Posted 30 August 2012 - 01:59 AM

```Private Sub cmdcalculate2_Click()
Dim total2 As Integer
Dim cufee1 As Integer

cufee1 = Me.txtcufee1.Text

If Len(Me.txtcufee1.Text) >= 0 And Len(Me.txtmed2.Text) >= 0 And Len(Me.txtlabexam1.Text) >= 0 Then
total2 = CSng(Me.txtcufee1.Text) + CSng(Me.txtmed2.Text) + CSng(Me.txtlabexam1.Text)
txttotal2.Text = total2
Else
txttotal2.Text = ""
End If

If Len(Me.txtcufee1.Text) >= 0 And Len(Me.txtmed2.Text) >= 0 And Len(Me.txtlabexam1.Text) = "" Then
total2 = CSng(Me.txtcufee1.Text) + CSng(Me.txtmed2.Text)
txttotal2.Text = total2
Else
txttotal2.Text = ""
End If

If Len(Me.txtcufee1.Text) >= 0 And Len(Me.txtmed2.Text) = "" And Len(Me.txtlabexam1.Text) >= 0 Then
total2 = CSng(Me.txtcufee1.Text) + CSng(Me.txtlabexam1.Text)
txttotal2.Text = total2
Else
txttotal2.Text = ""
End If

If Len(Me.txtcufee1.Text) >= 0 And Len(Me.txtmed2.Text) = "" And Len(Me.txtlabexam1.Text) = "" Then
txttotal2.Text = cufee1
Else
txttotal2.Text = ""
End If
End Sub

```

ihave this problem again .. huhuhu .. my if statement is kinda wrong.. it needs to be calculated even if i do not enter a value to one of the textbox .. for example i entered a value in txtcufee1 and txtmed2 and it'll execute the command .. but my if statement seems wrong .. can someone help me ?