Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To Val() or not to Val() 1

Status
Not open for further replies.

rayna

Programmer
Sep 24, 2001
54
0
0
CA
Hello everyone and Happy Holidays!

I have a query I am writing where I am getting data from another database and the data is in the form of a string - 0.89% and 0.74% (represented below by aa1 and aa2. I need to use that percentage along with another in a calculation and tried this:

TY/LY2: IIf(Val(Mid([aa2],1,InStr([aa2],"%"))),Format(Val(Mid([aa1],1,InStr([aa1],"%")))/Val(Mid([aa2],1,InStr([aa2],"%"))),"Percent"),"0.00%")

However, for some reason, VAL() insists on rounding UP, and so instead of 0.89/0.74, I get 1/1 and get a consistent 100%, which of course is totally inaccurate. I have tried all the 'C' functions as well (CDbl etc...) and they give me no better results.

Does anyone have an idea as to how I can accomplish this so I get a true percentage?

Thanks so much!
Rayna Man plans. God laughs.
 
How 'bout using CDbl instead of Val.

TY/LY2: IIf(CDbl(Mid([aa2],1,InStr([aa2],"%"))),Format(CDbl(Mid([aa1],1,InStr([aa1],"%")))/CDbl(Mid([aa2],1,InStr([aa2],"%"))),"Percent"),"0.00%")
 
In some instances Access will convert strings automatically in calculations. Try this:

TY/LY2: Format(Mid([aa1],1,InStr([aa1],"%")-1)/Mid([aa2],1,InStr([aa2],"%")-1),"Percent")

You need the -1 after the instr() function so the ending point for the mid() function doesn't include the "%" symbol.

Hope this helps

Ron
 
Ron,

Oh my Gosh!!! Thank you, thank you, THANK YOU!!! You have no idea how long I have been messing with this trying to make it work!!!!! :)

That "-1" thing I have never seen before and had no idea it was necessary.

Again, I can't tell you how much I appreciate this. [thumbsup]

Rayna


Man plans. God laughs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top