Forums
This topic is locked
Simple Division Conversions
Posted 13 Apr 2007 19:01:03
1
has voted
13 Apr 2007 19:01:03 Melissa Ferris posted:
ASP | AccessOk so I am making a website for a flooring company, They sell their flooring by the carton and they want a calculator on the site, for each product (all cartons can cover different square footage), that will convert how many square feet a person needs (which will be a form field and they enter the figure) to how many cartons they will need. I thought this was going to be an easy process to figure out, however I am lost more now after surfing for answers than I was before. My first idea was to incorporate a macro into my database, but that won't get me anywhere. So does anyone know how I would code an input field to be divided by a number that will be a record field? Thanks for any and all help!
Replies
Replied 13 Apr 2007 21:16:24
13 Apr 2007 21:16:24 Mike Haez replied:
<%
' Define Variables
Dim sqFootageRequired, sqFootagePerCarton, CartonsRequiredTemp, intNumberOfCartons, ProductID, PricePerCarton, TotalPrice
Dim strSQL, rsTemp
' sub to connect to a database. Use an adovbs.inc include file for this function (which can be found via google). Set up the variable adoCon as your database connection string - dnsless.
sub GetFromDB(strSQL, rsTemp)
Set rsTemp = server.createObject("adodb.recordset"
' set locktype
rsTemp.lockType = adLockReadOnly
' set the cursor
rsTemp.cursorType = adOpenForwardOnly
rsTemp.open strSQL, adoCon
end sub
' Function to close the database and free up resources
function CloseDB(rsTemp)
on error resume next
rsTemp.close
set rsTemp = nothing
end function
' Get Sq. Footage Required from user form input
sqFootageRequired = Request.Form("sqFootage"
' If this is blank input do something here, like redirect with an error - use yaromat DW extension to check form input is a number
If Len(sqFootageRequired) = 0 Then
' Do something here
Else
'convert to Cdbl (floating point) value
sqFootageRequired = Cdbl(sqFootageRequired)
End If
' Get the product ID from user input (drop down list of product styles, with product ID values would be good) - add any extra info you want to get from the Database and add this to the SQL query too
ProductID = CInt(request.Form("ProductID")
' Build SQL query to grab db values WHERE the product ID = the var we got from the form - db example name is dbProducts - use your own
strSQL = "SELECT sqFtPerCarton, Price FROM dbProducts WHERE Product_ID = " & Product_ID & " "
' Use the GetfromDB sub above to connect to the DB, grab the data and pass it to the recordset (rsTemp)
Call GetFromDB(strSQL, rsTemp)
sqFootagePerCarton = rsTemp("sqFtPerCarton"
PricePerCarton = rsTemp("Price"
' we're finished with the db, so close it
Call CloseDB()
' do the math, format the number to one decimal place
CartonsRequiredTemp = formatnumber((sqFootageRequired / sqFootagePerCarton), 1)
' round the number up to the next whole number
If CartonsRequiredTemp > Int(CartonsRequiredTemp) Then
intNumberOfCartons = Int(CartonsRequiredTemp) + 1
Else
intNumberOfCartons = Int(CartonsRequiredTemp)
End If
'work out the price
TotalPrice = FormatCurrency(intNumberOfCartons * PricePerCarton)
' do a response to tell user how many cartons is required and how much it will cost
Response.Write "<b>Total Cartons Required for " & sqFootageRequired & " Sq. Ft. = " & intNumberOfCartons & " Cartons. Total Cost = " & TotalPrice & "</b>"
%>
---------------------------
I wrote this of the top of my head, so there could be a couple of mistakes here.
Hope this helps
' Define Variables
Dim sqFootageRequired, sqFootagePerCarton, CartonsRequiredTemp, intNumberOfCartons, ProductID, PricePerCarton, TotalPrice
Dim strSQL, rsTemp
' sub to connect to a database. Use an adovbs.inc include file for this function (which can be found via google). Set up the variable adoCon as your database connection string - dnsless.
sub GetFromDB(strSQL, rsTemp)
Set rsTemp = server.createObject("adodb.recordset"
' set locktype
rsTemp.lockType = adLockReadOnly
' set the cursor
rsTemp.cursorType = adOpenForwardOnly
rsTemp.open strSQL, adoCon
end sub
' Function to close the database and free up resources
function CloseDB(rsTemp)
on error resume next
rsTemp.close
set rsTemp = nothing
end function
' Get Sq. Footage Required from user form input
sqFootageRequired = Request.Form("sqFootage"
' If this is blank input do something here, like redirect with an error - use yaromat DW extension to check form input is a number
If Len(sqFootageRequired) = 0 Then
' Do something here
Else
'convert to Cdbl (floating point) value
sqFootageRequired = Cdbl(sqFootageRequired)
End If
' Get the product ID from user input (drop down list of product styles, with product ID values would be good) - add any extra info you want to get from the Database and add this to the SQL query too
ProductID = CInt(request.Form("ProductID")
' Build SQL query to grab db values WHERE the product ID = the var we got from the form - db example name is dbProducts - use your own
strSQL = "SELECT sqFtPerCarton, Price FROM dbProducts WHERE Product_ID = " & Product_ID & " "
' Use the GetfromDB sub above to connect to the DB, grab the data and pass it to the recordset (rsTemp)
Call GetFromDB(strSQL, rsTemp)
sqFootagePerCarton = rsTemp("sqFtPerCarton"
PricePerCarton = rsTemp("Price"
' we're finished with the db, so close it
Call CloseDB()
' do the math, format the number to one decimal place
CartonsRequiredTemp = formatnumber((sqFootageRequired / sqFootagePerCarton), 1)
' round the number up to the next whole number
If CartonsRequiredTemp > Int(CartonsRequiredTemp) Then
intNumberOfCartons = Int(CartonsRequiredTemp) + 1
Else
intNumberOfCartons = Int(CartonsRequiredTemp)
End If
'work out the price
TotalPrice = FormatCurrency(intNumberOfCartons * PricePerCarton)
' do a response to tell user how many cartons is required and how much it will cost
Response.Write "<b>Total Cartons Required for " & sqFootageRequired & " Sq. Ft. = " & intNumberOfCartons & " Cartons. Total Cost = " & TotalPrice & "</b>"
%>
---------------------------
I wrote this of the top of my head, so there could be a couple of mistakes here.
Hope this helps