Calculating Averages

1. Member
Join Date
Dec 1969
Posts
96

## Calculating Averages

Trying to calculate the average of numbers returned from a recordset. I have a database that collects information from a satisfaction survey. Each question has a rating from 1 to 5. After the completion of this form I would like to calculate the average rating based on the input from the user. I know this has to be simple but I keep getting Type Mismatch errors when I try to do this.<BR><BR>Dim str1,str2,str3<BR>strAvg = (str1+str2+str3) / 3<BR><BR>Thanks in advance,<BR>Tom

2. Senior Member
Join Date
Dec 1969
Posts
1,274

## Time to debug!

I hope you&#039;re putting something in str1, str2 and str3 before you calculate the average. Have you done a response.write of the values of str1, str2 and str3 before you calculate strAvg? If so, what are they?

3. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## RE: Calculating Averages

You might play with Cint() and Cstr(), depending on what types your variables are expecting. Example:<BR>strAvg = Cstr((Cint(str1) + Cint(str2) + Cint(str3)) / 3)

4. Member
Join Date
Dec 1969
Posts
96

## RE: Calculating Averages

Excellent that worked. Guess my data type set to numeric was causing that, not really sure though. Okay so now it&#039;s working how do I round the number up to the nearest whole?

5. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## RE: Calculating Averages

Put another Cint() around the algabreic part of the expression. Example:<BR>mynewstring = Cstr(Cint((Cint(var1) + Cint(var2) + Cint(var3)) / 3))

6. Junior Member
Join Date
Dec 1969
Posts
14

## RE: Calculating Averages

You could also use this SQL statement if you data are not stored as BIT:<BR><BR>select AVG(question1), AVG(question2) -- etc.<BR>from answerTable<BR><BR>so that the RecordSet will already have the averages in it. These values will have the same type as in your database (int will be truncated to an int value). If you really want the next higher integer value, you could do some fancy conversion stuff, or store the number as a decimal and use CEILING(AVG(column))<BR><BR>-Joseph Bui

#### Posting Permissions

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