PC Review


Reply
Thread Tools Rate Thread

Confused by CDbl behaviour

 
 
Risky Dave
Guest
Posts: n/a
 
      30th Dec 2008
Hi,

Can someone please tell me why this does not work (it throws up a type
mismatch):

Sub Cost_Update()

Dim dMitCost As Double

dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
FmRiskCost.TextBox23.Value = dMitCost
End

I am trying to capture (up to) five values entered into textboxes on a form,
add them and then send the total value to another text box on the same form.
I can do this by replacing the CDbl with a Val, but the calculation then goes
haywire when lage numbers are inputted.

Is this a variation on the old thing of XL assuming that anything joined by
a '+' sign is a string not a number? If so, how do I get around it?

Hope this makes some sense!

TIA

Dave
 
Reply With Quote
 
 
 
 
Eduardo
Guest
Posts: n/a
 
      30th Dec 2008
Try
If IsNumeric(Me.FmRiskCost.TextBox9.Value) _
And IsNumeric(Me.FmRiskCost.TextBox10.Value) _
And IsNumeric(Me.FmRiskCost.TextBox11.Value) _
And IsNumeric(Me.FmRiskCost.TextBox12.Value) _
And IsNumeric(Me.FmRiskCost.TextBox13.Value) Then

Me.dMitCost.value = (CDbl(FmRiskCost.TextBox9.Value) +
> CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> Me.FmRiskCost.TextBox23.Value = Me.dMitCost.Value
> End




"Risky Dave" wrote:

> Hi,
>
> Can someone please tell me why this does not work (it throws up a type
> mismatch):
>
> Sub Cost_Update()
>
> Dim dMitCost As Double
>
> dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
> CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> FmRiskCost.TextBox23.Value = dMitCost
> End
>
> I am trying to capture (up to) five values entered into textboxes on a form,
> add them and then send the total value to another text box on the same form.
> I can do this by replacing the CDbl with a Val, but the calculation then goes
> haywire when lage numbers are inputted.
>
> Is this a variation on the old thing of XL assuming that anything joined by
> a '+' sign is a string not a number? If so, how do I get around it?
>
> Hope this makes some sense!
>
> TIA
>
> Dave

 
Reply With Quote
 
Risky Dave
Guest
Posts: n/a
 
      30th Dec 2008
Eduardo,

Thanks for the quick response.

I'm not quite sure what you mean with this (I'm not a programmer - just
learnign this stuff as I go along).

If I insert another IF statement around the existing code as per your
posting, XL throws back an error about invalid use of "Me." If I remove the
Me, then the code simply skips past the line intended to capture the input,
ie. the IF statement is false so it is just ignored so I don't get the output
I need.

Obviously, I'm missing something here.

TIA

Dave

"Eduardo" wrote:

> Try
> If IsNumeric(Me.FmRiskCost.TextBox9.Value) _
> And IsNumeric(Me.FmRiskCost.TextBox10.Value) _
> And IsNumeric(Me.FmRiskCost.TextBox11.Value) _
> And IsNumeric(Me.FmRiskCost.TextBox12.Value) _
> And IsNumeric(Me.FmRiskCost.TextBox13.Value) Then
>
> Me.dMitCost.value = (CDbl(FmRiskCost.TextBox9.Value) +
> > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > Me.FmRiskCost.TextBox23.Value = Me.dMitCost.Value
> > End

>
>
>
> "Risky Dave" wrote:
>
> > Hi,
> >
> > Can someone please tell me why this does not work (it throws up a type
> > mismatch):
> >
> > Sub Cost_Update()
> >
> > Dim dMitCost As Double
> >
> > dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
> > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > FmRiskCost.TextBox23.Value = dMitCost
> > End
> >
> > I am trying to capture (up to) five values entered into textboxes on a form,
> > add them and then send the total value to another text box on the same form.
> > I can do this by replacing the CDbl with a Val, but the calculation then goes
> > haywire when lage numbers are inputted.
> >
> > Is this a variation on the old thing of XL assuming that anything joined by
> > a '+' sign is a string not a number? If so, how do I get around it?
> >
> > Hope this makes some sense!
> >
> > TIA
> >
> > Dave

 
Reply With Quote
 
Eduardo
Guest
Posts: n/a
 
      30th Dec 2008
Hi Dave,
what version of excel are you using

"Risky Dave" wrote:

> Eduardo,
>
> Thanks for the quick response.
>
> I'm not quite sure what you mean with this (I'm not a programmer - just
> learnign this stuff as I go along).
>
> If I insert another IF statement around the existing code as per your
> posting, XL throws back an error about invalid use of "Me." If I remove the
> Me, then the code simply skips past the line intended to capture the input,
> ie. the IF statement is false so it is just ignored so I don't get the output
> I need.
>
> Obviously, I'm missing something here.
>
> TIA
>
> Dave
>
> "Eduardo" wrote:
>
> > Try
> > If IsNumeric(Me.FmRiskCost.TextBox9.Value) _
> > And IsNumeric(Me.FmRiskCost.TextBox10.Value) _
> > And IsNumeric(Me.FmRiskCost.TextBox11.Value) _
> > And IsNumeric(Me.FmRiskCost.TextBox12.Value) _
> > And IsNumeric(Me.FmRiskCost.TextBox13.Value) Then
> >
> > Me.dMitCost.value = (CDbl(FmRiskCost.TextBox9.Value) +
> > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > Me.FmRiskCost.TextBox23.Value = Me.dMitCost.Value
> > > End

> >
> >
> >
> > "Risky Dave" wrote:
> >
> > > Hi,
> > >
> > > Can someone please tell me why this does not work (it throws up a type
> > > mismatch):
> > >
> > > Sub Cost_Update()
> > >
> > > Dim dMitCost As Double
> > >
> > > dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
> > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > FmRiskCost.TextBox23.Value = dMitCost
> > > End
> > >
> > > I am trying to capture (up to) five values entered into textboxes on a form,
> > > add them and then send the total value to another text box on the same form.
> > > I can do this by replacing the CDbl with a Val, but the calculation then goes
> > > haywire when lage numbers are inputted.
> > >
> > > Is this a variation on the old thing of XL assuming that anything joined by
> > > a '+' sign is a string not a number? If so, how do I get around it?
> > >
> > > Hope this makes some sense!
> > >
> > > TIA
> > >
> > > Dave

 
Reply With Quote
 
Risky Dave
Guest
Posts: n/a
 
      30th Dec 2008
Office '07 under Vista

"Eduardo" wrote:

> Hi Dave,
> what version of excel are you using
>
> "Risky Dave" wrote:
>
> > Eduardo,
> >
> > Thanks for the quick response.
> >
> > I'm not quite sure what you mean with this (I'm not a programmer - just
> > learnign this stuff as I go along).
> >
> > If I insert another IF statement around the existing code as per your
> > posting, XL throws back an error about invalid use of "Me." If I remove the
> > Me, then the code simply skips past the line intended to capture the input,
> > ie. the IF statement is false so it is just ignored so I don't get the output
> > I need.
> >
> > Obviously, I'm missing something here.
> >
> > TIA
> >
> > Dave
> >
> > "Eduardo" wrote:
> >
> > > Try
> > > If IsNumeric(Me.FmRiskCost.TextBox9.Value) _
> > > And IsNumeric(Me.FmRiskCost.TextBox10.Value) _
> > > And IsNumeric(Me.FmRiskCost.TextBox11.Value) _
> > > And IsNumeric(Me.FmRiskCost.TextBox12.Value) _
> > > And IsNumeric(Me.FmRiskCost.TextBox13.Value) Then
> > >
> > > Me.dMitCost.value = (CDbl(FmRiskCost.TextBox9.Value) +
> > > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > > Me.FmRiskCost.TextBox23.Value = Me.dMitCost.Value
> > > > End
> > >
> > >
> > >
> > > "Risky Dave" wrote:
> > >
> > > > Hi,
> > > >
> > > > Can someone please tell me why this does not work (it throws up a type
> > > > mismatch):
> > > >
> > > > Sub Cost_Update()
> > > >
> > > > Dim dMitCost As Double
> > > >
> > > > dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
> > > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > > FmRiskCost.TextBox23.Value = dMitCost
> > > > End
> > > >
> > > > I am trying to capture (up to) five values entered into textboxes on a form,
> > > > add them and then send the total value to another text box on the same form.
> > > > I can do this by replacing the CDbl with a Val, but the calculation then goes
> > > > haywire when lage numbers are inputted.
> > > >
> > > > Is this a variation on the old thing of XL assuming that anything joined by
> > > > a '+' sign is a string not a number? If so, how do I get around it?
> > > >
> > > > Hope this makes some sense!
> > > >
> > > > TIA
> > > >
> > > > Dave

 
Reply With Quote
 
Eduardo
Guest
Posts: n/a
 
      30th Dec 2008
Hi Dave,
I am not an expert as well but some nice people has helped me a lot and the
code is working beautifully to me, there is only one difference and it is I
don't include the form name and I missed some Me in the totals, sorry, try
this
Try
If IsNumeric(Me.TextBox9.Value) _
And IsNumeric(Me.TextBox10.Value) _
And IsNumeric(Me.TextBox11.Value) _
And IsNumeric(Me.TextBox12.Value) _
And IsNumeric(Me.TextBox13.Value) Then

Me.dMitCost.value = (CDbl(Me.TextBox9.Value) +
> CDbl_(Me.TextBox10.Value) + CDbl(Me.TextBox11.Value) +
> CDbl_(Me.TextBox12.Value) + CDbl(Me.TextBox13.Value))_
> Me.TextBox23.Value = Me.dMitCost.Value
> End



"Risky Dave" wrote:

> Office '07 under Vista
>
> "Eduardo" wrote:
>
> > Hi Dave,
> > what version of excel are you using
> >
> > "Risky Dave" wrote:
> >
> > > Eduardo,
> > >
> > > Thanks for the quick response.
> > >
> > > I'm not quite sure what you mean with this (I'm not a programmer - just
> > > learnign this stuff as I go along).
> > >
> > > If I insert another IF statement around the existing code as per your
> > > posting, XL throws back an error about invalid use of "Me." If I remove the
> > > Me, then the code simply skips past the line intended to capture the input,
> > > ie. the IF statement is false so it is just ignored so I don't get the output
> > > I need.
> > >
> > > Obviously, I'm missing something here.
> > >
> > > TIA
> > >
> > > Dave
> > >
> > > "Eduardo" wrote:
> > >
> > > > Try
> > > > If IsNumeric(Me.FmRiskCost.TextBox9.Value) _
> > > > And IsNumeric(Me.FmRiskCost.TextBox10.Value) _
> > > > And IsNumeric(Me.FmRiskCost.TextBox11.Value) _
> > > > And IsNumeric(Me.FmRiskCost.TextBox12.Value) _
> > > > And IsNumeric(Me.FmRiskCost.TextBox13.Value) Then
> > > >
> > > > Me.dMitCost.value = (CDbl(FmRiskCost.TextBox9.Value) +
> > > > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > > > Me.FmRiskCost.TextBox23.Value = Me.dMitCost.Value
> > > > > End
> > > >
> > > >
> > > >
> > > > "Risky Dave" wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > Can someone please tell me why this does not work (it throws up a type
> > > > > mismatch):
> > > > >
> > > > > Sub Cost_Update()
> > > > >
> > > > > Dim dMitCost As Double
> > > > >
> > > > > dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
> > > > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > > > FmRiskCost.TextBox23.Value = dMitCost
> > > > > End
> > > > >
> > > > > I am trying to capture (up to) five values entered into textboxes on a form,
> > > > > add them and then send the total value to another text box on the same form.
> > > > > I can do this by replacing the CDbl with a Val, but the calculation then goes
> > > > > haywire when lage numbers are inputted.
> > > > >
> > > > > Is this a variation on the old thing of XL assuming that anything joined by
> > > > > a '+' sign is a string not a number? If so, how do I get around it?
> > > > >
> > > > > Hope this makes some sense!
> > > > >
> > > > > TIA
> > > > >
> > > > > Dave

 
Reply With Quote
 
Risky Dave
Guest
Posts: n/a
 
      30th Dec 2008
Eduardo,

Still haven't been able to get it to work so I've just written it as a set
of If statements - really ugly (and inefficient I suspect) but it works

"Eduardo" wrote:

> Hi Dave,
> I am not an expert as well but some nice people has helped me a lot and the
> code is working beautifully to me, there is only one difference and it is I
> don't include the form name and I missed some Me in the totals, sorry, try
> this
> Try
> If IsNumeric(Me.TextBox9.Value) _
> And IsNumeric(Me.TextBox10.Value) _
> And IsNumeric(Me.TextBox11.Value) _
> And IsNumeric(Me.TextBox12.Value) _
> And IsNumeric(Me.TextBox13.Value) Then
>
> Me.dMitCost.value = (CDbl(Me.TextBox9.Value) +
> > CDbl_(Me.TextBox10.Value) + CDbl(Me.TextBox11.Value) +
> > CDbl_(Me.TextBox12.Value) + CDbl(Me.TextBox13.Value))_
> > Me.TextBox23.Value = Me.dMitCost.Value
> > End

>
>
> "Risky Dave" wrote:
>
> > Office '07 under Vista
> >
> > "Eduardo" wrote:
> >
> > > Hi Dave,
> > > what version of excel are you using
> > >
> > > "Risky Dave" wrote:
> > >
> > > > Eduardo,
> > > >
> > > > Thanks for the quick response.
> > > >
> > > > I'm not quite sure what you mean with this (I'm not a programmer - just
> > > > learnign this stuff as I go along).
> > > >
> > > > If I insert another IF statement around the existing code as per your
> > > > posting, XL throws back an error about invalid use of "Me." If I remove the
> > > > Me, then the code simply skips past the line intended to capture the input,
> > > > ie. the IF statement is false so it is just ignored so I don't get the output
> > > > I need.
> > > >
> > > > Obviously, I'm missing something here.
> > > >
> > > > TIA
> > > >
> > > > Dave
> > > >
> > > > "Eduardo" wrote:
> > > >
> > > > > Try
> > > > > If IsNumeric(Me.FmRiskCost.TextBox9.Value) _
> > > > > And IsNumeric(Me.FmRiskCost.TextBox10.Value) _
> > > > > And IsNumeric(Me.FmRiskCost.TextBox11.Value) _
> > > > > And IsNumeric(Me.FmRiskCost.TextBox12.Value) _
> > > > > And IsNumeric(Me.FmRiskCost.TextBox13.Value) Then
> > > > >
> > > > > Me.dMitCost.value = (CDbl(FmRiskCost.TextBox9.Value) +
> > > > > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > > > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > > > > Me.FmRiskCost.TextBox23.Value = Me.dMitCost.Value
> > > > > > End
> > > > >
> > > > >
> > > > >
> > > > > "Risky Dave" wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > Can someone please tell me why this does not work (it throws up a type
> > > > > > mismatch):
> > > > > >
> > > > > > Sub Cost_Update()
> > > > > >
> > > > > > Dim dMitCost As Double
> > > > > >
> > > > > > dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
> > > > > > CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
> > > > > > CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> > > > > > FmRiskCost.TextBox23.Value = dMitCost
> > > > > > End
> > > > > >
> > > > > > I am trying to capture (up to) five values entered into textboxes on a form,
> > > > > > add them and then send the total value to another text box on the same form.
> > > > > > I can do this by replacing the CDbl with a Val, but the calculation then goes
> > > > > > haywire when lage numbers are inputted.
> > > > > >
> > > > > > Is this a variation on the old thing of XL assuming that anything joined by
> > > > > > a '+' sign is a string not a number? If so, how do I get around it?
> > > > > >
> > > > > > Hope this makes some sense!
> > > > > >
> > > > > > TIA
> > > > > >
> > > > > > Dave

 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      31st Dec 2008
On Tue, 30 Dec 2008 08:20:05 -0800, Risky Dave
<(E-Mail Removed)> wrote:

>Hi,
>
>Can someone please tell me why this does not work (it throws up a type
>mismatch):
>
>Sub Cost_Update()
>
>Dim dMitCost As Double
>
> dMitCost = (CDbl(FmRiskCost.TextBox9.Value) +
>CDbl_(FmRiskCost.TextBox10.Value) + CDbl(FmRiskCost.TextBox11.Value) +
>CDbl_(FmRiskCost.TextBox12.Value) + CDbl(FmRiskCost.TextBox13.Value))_
> FmRiskCost.TextBox23.Value = dMitCost
>End
>
>I am trying to capture (up to) five values entered into textboxes on a form,
>add them and then send the total value to another text box on the same form.
>I can do this by replacing the CDbl with a Val, but the calculation then goes
>haywire when lage numbers are inputted.
>
>Is this a variation on the old thing of XL assuming that anything joined by
>a '+' sign is a string not a number? If so, how do I get around it?
>
>Hope this makes some sense!
>
>TIA
>
>Dave


I would assume that one of the values is text that cannot be coerced to
represent a numeric value.
--ron
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between CDec and CDbl James Microsoft VB .NET 7 25th Sep 2006 02:03 AM
CDbl Function =?Utf-8?B?d25maXNiYQ==?= Microsoft Access 2 31st May 2006 08:01 PM
Cdbl & International Settings michele Microsoft VB .NET 6 12th Feb 2006 06:19 PM
ASP 3.0 : CDbl Error? Sam Microsoft VB .NET 2 12th Jan 2005 12:34 AM
CDbl function snax500 Microsoft Excel Programming 4 22nd Oct 2003 04:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:09 AM.