Dan said:
(Using Classic ASP & MS Access)
I have a page that has 120 fields on it (mostly checkboxes). I cannot
split this into smaller pages.
So what i want to do is write a class that handles this. in the code
it would be something like:
Set thisForm = new classBigForm
thisForm.valname = Request.Form("name")
thisForm.val1 = Request.Form("val1")
thisForm.val2 = Request.Form("val2")
thisForm.val3 = Request.Form("val3")
thisForm.xyz = Request.Form("xyz")
....
Writing the class wasn't too bad. But repeating the same code 120
times, the file got pretty big. Is there a better way to do this
dynamically?
One thing that may help is to use an array or dictionary object to store all
these boolean values, instead of using indifidual properties. (it sounds as
if your database structure would benefit from the same thinking). Are they
really named val1,...,valx? Or do they have real names with/without numbers?
if the former, just use an array. For refreshing the array values from the
Request object, you would do something like:
function RefreshFromRequest()
dim i, req
'First, set all the booleans to False
for i = 0 to ubound(arBools)
arBools(i)=false
next
'Then, set the ones that appear in Request to True
Set req=Request.Form
for i = 0 to ubound(arBools)
if len(req("val" & i)) > 0 then arBools(i)=true
next
end function
if the latter, you will need to persist the field names in order to avoid
going to the database every time you need to use the dictionary. You could
do this in a file or in in an xml document kept in Application (don't even
think about storing a Dictionary object in Application or Session). Let's
say you do the xml in Application route. You would do something like this in
global.asa (untested - I did not refer to the documentation so there may be
minor syntax errors - you can find te documentation at
http://msdn.microsoft.com/library):
sub application_onstart()
dim fld, xmldoc, root, node
set xmldoc=createobject("msxml2.FreeThreadedDomDocument")
set root=xmldoc.createelement("fields")
set xmldoc.documentelement=root
'connect to the database and open a recordset on the first table
'Use a "where 1=2" clause to prevent records from being retrieved -
'we only want the field names at this point
'then
for each fld in rs.Fields
set node=xmldoc.createelement("field")
node.setattribute "fieldname",fld.name
root.appendchild node
next
'do the same for the subsequent tables - again, I would reconsider
'the database design ...
'then:
Set Application("FieldNamesXML") = xmldoc
end sub
This will allow you to do this in your class's init sub:
set dicBools=createobject("scripting.dictionary")
set xmldoc=Application("FieldNamesXML")
nodes=xmldocs.selectNodes("fields/field")
for each node in nodes
dicBools.Add node.getattribute("fieldname"),false
next
set BoolVals = dicBools
To refresh it from Request:
dim dicBools, req,fieldname
set dicBools = BoolVals
'since it's already initialized in the init sub, no need to set
'all the values to false
Set req=Request.Form
for each fieldname in dicBools
dicBools(fieldname)=req(fieldname)
next
Also in the same respect to the amount of fields, is there an easy way
to compare all of these values, so I can run an sql update only on the
ones i need to?
I would not consider doing dynamic sql for this. Just create a single
parameterized statement or saved query and pass all the values to it. You
are not going to save much processing time by only updating the ones that
have changed.
Here are some links explaining this approach:
Saved parameter queries:
http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&[email protected]
http://groups.google.com/groups?hl=...=1&[email protected]
Using a Command object to pass values to string containing parameter
markers:
http://groups-beta.google.com/group/microsoft.public.inetserver.asp.db/msg/72e36562fee7804e
I will reply to your follow-up with a suggestion for your database design.
Bob Barrows