9 Replies - 946 Views - Last Post: 30 August 2012 - 01:59 AM Rate Topic: -----

#1 cerealkiller17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,778
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 cerealkiller17  Icon User is offline

  • New D.I.C Head

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

Re: type mismatch

Posted 30 August 2012 - 12:03 AM

View Postmaj3091, 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 :(
Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,778
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 cerealkiller17  Icon User is offline

  • New D.I.C Head

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

Re: type mismatch

Posted 30 August 2012 - 12:11 AM

View Postmaj3091, 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 ..
Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,778
  • Joined: 26-March 09

Re: type mismatch

Posted 30 August 2012 - 12:20 AM

View Postcerealkiller17, on 30 August 2012 - 08:11 AM, said:

View Postmaj3091, 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

Was This Post Helpful? 0
  • +
  • -

#7 cerealkiller17  Icon User is offline

  • New D.I.C Head

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

Re: type mismatch

Posted 30 August 2012 - 12:26 AM

View Postmaj3091, on 30 August 2012 - 12:20 AM, said:

View Postcerealkiller17, on 30 August 2012 - 08:11 AM, said:

View Postmaj3091, 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 !! :*
Was This Post Helpful? 0
  • +
  • -

#8 cerealkiller17  Icon User is offline

  • New D.I.C Head

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

Re: type mismatch

Posted 30 August 2012 - 12:34 AM

thank you so much !! yay !!
Was This Post Helpful? 0
  • +
  • -

#9 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,778
  • Joined: 26-March 09

Re: type mismatch

Posted 30 August 2012 - 01:35 AM

Glad you sorted it.

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.
Was This Post Helpful? 0
  • +
  • -

#10 cerealkiller17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ? :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1