americaneldercare
asked on
ASP calling a variable with a variable
Sorry for the ambiguous title, not quite sure how to explain this. Basically what I have is an ASP page that will dynamically build a 'form' based upon information from a Database. The DB holds information such as what type of options to give the user (radial, checkboxes, drop down, text box...) along with what the information that should be displayed.
All of this builds perfectly fine. However, now I have a situation in which I want to add in that the DB will also store references to a known variable or DB Call.
So in the DB I added a new column for these sections which indicates what variable to call. However, if I simply 'grab' this variable from the DB it is simply going to print it. How can I pass the variable that I am grabbing from the DB and tell my ASP page that I want it to use a matching variable that it has.
I am hoping to avoid using something as static as If or Case statements. The ultimate goal is for the variable we are grabbing from the first table to be able to inform the ASP page of what variable to use from another DB call it is making.
For example: The first DB call is to 'rsForms' and the second DB call is 'rsEmployee'. The rsForm("question") is 'First Name' and within that rsForm("variable") column we put 'FirstName'. The hope being that we could do something like rsEmployee("rsForm("Variab le")").
So each time that the rsForms("Variable") field is not null, it would use a separate statement which is prefilling the value of the Form with the information from rsEmployee using the field that is being specified by the rsForms("Variable") value.
All of this builds perfectly fine. However, now I have a situation in which I want to add in that the DB will also store references to a known variable or DB Call.
So in the DB I added a new column for these sections which indicates what variable to call. However, if I simply 'grab' this variable from the DB it is simply going to print it. How can I pass the variable that I am grabbing from the DB and tell my ASP page that I want it to use a matching variable that it has.
I am hoping to avoid using something as static as If or Case statements. The ultimate goal is for the variable we are grabbing from the first table to be able to inform the ASP page of what variable to use from another DB call it is making.
For example: The first DB call is to 'rsForms' and the second DB call is 'rsEmployee'. The rsForm("question") is 'First Name' and within that rsForm("variable") column we put 'FirstName'. The hope being that we could do something like rsEmployee("rsForm("Variab
So each time that the rsForms("Variable") field is not null, it would use a separate statement which is prefilling the value of the Form with the information from rsEmployee using the field that is being specified by the rsForms("Variable") value.
Please simplify your question. List out your table structure and your desired output. This is the basic way I have done the same thing.
1) I create a form entering the the name and description. An ID and datestamp are auto generated
tblForm
ID
Date
Name
Description
2) I have another table that is used to generate the detailes for each question in the form and store the data returned.
tblFormDetail
ID
FormID (key from tblForm)
FieldName
Question
FieldType (radio, checkbox,select text)
FieldOptions (radio, checkbox, select data)
DefaultOption
Answer (store the answer)
1) I create a form entering the the name and description. An ID and datestamp are auto generated
tblForm
ID
Date
Name
Description
2) I have another table that is used to generate the detailes for each question in the form and store the data returned.
tblFormDetail
ID
FormID (key from tblForm)
FieldName
Question
FieldType (radio, checkbox,select text)
FieldOptions (radio, checkbox, select data)
DefaultOption
Answer (store the answer)
There are many ways to achieve this but one of the easiest way is as below
Use the recordSet.Fields Collection and determine the index of the field and the use the rs(index).
for example in the above code, you can write it as (note it is just a skeleton, not the real code, so please implement it with the correct syntax.
func_A
do while i<=rs.Fields.count
itemfields=rs.Fields.Item( i)
if (itemFields == rsForm("Variable"))
return i
LOOP
now you can use it as
var index = func_a
rsEmployee(index)
Use the recordSet.Fields Collection and determine the index of the field and the use the rs(index).
for example in the above code, you can write it as (note it is just a skeleton, not the real code, so please implement it with the correct syntax.
func_A
do while i<=rs.Fields.count
itemfields=rs.Fields.Item(
if (itemFields == rsForm("Variable"))
return i
LOOP
now you can use it as
var index = func_a
rsEmployee(index)
ASKER
Manumd - the problem is that the "variable" is coming from the other DB.
Neo_Jarvis - I am hoping to accomplish this without using If/Case statements.
Padas - simplified question below:
I have two DB connections open on my ASP page. While the ASP page is looping through the first DB, that DB has a field that can contain variable which is used for the second DB. So for example:
DB1 Fields - ID, vName, Variable
DB1 - 1, Roger, iName
DB2 Fields - ID, iName
DB2 - 1, Pepper
do while not DB1.EoF
if DB1("Variable") is not null then
wscript.echo(DB2(DB1("Vari able"))) - so the call it would be doing is DB2("iName")
end if
loop
so the output of the Echo statement would say "Pepper".
The issue is we can't use a view because the DB2 can change based upon other Criteria being assessed by the ASP page.
Neo_Jarvis - I am hoping to accomplish this without using If/Case statements.
Padas - simplified question below:
I have two DB connections open on my ASP page. While the ASP page is looping through the first DB, that DB has a field that can contain variable which is used for the second DB. So for example:
DB1 Fields - ID, vName, Variable
DB1 - 1, Roger, iName
DB2 Fields - ID, iName
DB2 - 1, Pepper
do while not DB1.EoF
if DB1("Variable") is not null then
wscript.echo(DB2(DB1("Vari
end if
loop
so the output of the Echo statement would say "Pepper".
The issue is we can't use a view because the DB2 can change based upon other Criteria being assessed by the ASP page.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
OP, please explain why:
You say:
If you are saying that the table you want to draw from may be different, then you could store and call a sub routine:
Please expand on what may change, and what it may change to.
GH
do while not DB1.EoF...is not working for you?
if DB1("Variable") is not null then
wscript.echo(DB2(DB1("Variable"))) - so the call it would be doing is DB2("iName")
end if
loop
so the output of the Echo statement would say "Pepper".
You say:
DB2 can change based upon other Criteria... but you do not say what to?
If you are saying that the table you want to draw from may be different, then you could store and call a sub routine:
Execute(rs("DB1Field"))
Sub SomeSubForDoingSomethingWithDB2()
End Sub
Sub SomeSubForDoingSomethingWithDB3()
End Sub
Please expand on what may change, and what it may change to.
GH
ASKER
The problem is I can't figure out a way to use the value from DB1("Variable") as the field in the DB2 call. If you try to put in DB2(DB1("variable")) you will get a "type mismatch" error.
If you try and do DB2( & DB1("Variable") & ) it gives a syntax error. I have tried many other variations as well. The issue is I cannot find a way to insert the DB1("Variable") as the field for the DB2 statement.
If you try and do DB2( & DB1("Variable") & ) it gives a syntax error. I have tried many other variations as well. The issue is I cannot find a way to insert the DB1("Variable") as the field for the DB2 statement.
Variable="LastName"
response.write DB1(Variable) ' No Quotes
response.write DB1(Variable) ' No Quotes
The problem may well be that the Variable is empty... This does not mean that it is NULL, just that it has NO value set (an empty string).
Change:
GH
Change:
if DB1("Variable") is not null then
...to...if len(DB1("Variable")) > 0 then
...then you will only run the code if it is not NULL and is not just empty. IE, it will actually have something in it.GH
ASKER
modify that:
the issue appears to have been the syntax of:
DB2(DB1("Variable")
if I assigned DB1("Variable") to another variable like "Dbfield" then did DB2(DBField) that would work.
the issue appears to have been the syntax of:
DB2(DB1("Variable")
if I assigned DB1("Variable") to another variable like "Dbfield" then did DB2(DBField) that would work.
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for americaneldercare's comment #a39320617
for the following reason:
The answer I put above is what finally resolved the error.
Accepted answer: 0 points for americaneldercare's comment #a39320617
for the following reason:
The answer I put above is what finally resolved the error.
@americaneldercare, this is the suggestion I have gave you twice #a39317996 and #a39318878
but perhaps i don't understand