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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript dynamic element

Status
Not open for further replies.

707

Programmer
Nov 25, 2001
4
IR

Could anyone tell me how to pass dynamic variable within the ASP tag. Following is my code:

Error generate at:&quot;<% = ObjRecordSet.Fields.Item(cint(i)).Name %>&quot;. It works for :

&quot;<% = ObjRecordSet.Fields.Item(0).Name %>&quot;
&quot;<% = ObjRecordSet.Fields.Item(2).Name %>&quot;
&quot;<% = ObjRecordSet.Fields.Item(3).Name %>&quot;
&quot;<% = ObjRecordSet.Fields.Item(4).Name %>&quot;
and so on.. but will not work at for loop


<script language=&quot;VBScript&quot;>
Sub onAdd()
Dim FieldNames()
Dim DocRecordFieldCount
Dim i
DocRecordFieldCount = &quot;<% = ObjRecordset.Fields.Count %>&quot;
ReDim FieldNames(DocRecordFieldCount)
for i = 0 to DocRecordFieldCount - 1
FieldNames(i) = &quot;<% = ObjRecordSet.Fields.Item(cint(i)).Name %>&quot;
next


End Sub
</Script>
 
FieldNames(i) = <%response.write(&quot;ObjRecordSet.Fields.Item(&quot; & cint(i) & &quot;).Name&quot;)%>

See if that might clear it up --

I don't think you need those quotation marks
 

This code is in VBScript for a button action. I don't think response.write will work. Also, I tried your suggestion, it didn't work
 
Then maybe you shouldn't be enclosing them in the asp tags...

try:
for i = 0 to DocRecordFieldCount - 1
FieldNames(i) = ObjRecordSet.Fields.Item(i).Name
next

 
Actually, I want utilise the ObjRecordSet created within the page. I am populating the ObjRecordSet values in html table. When user click on button I want to be in the same page and utilise the created ObjRecordSet and extract fieldnames for the same. To write function either I can user JScript or VBScript. I choosed VBScript. Since I'm utilising the created objRecordSet I have to write like

<% = var_Name %>

i.e.

&quot;<% = ObjRecordSet.Fields.Item(cint(i)).Name %>&quot;

Hope you can help me.................

 
Ok, I **think** I finally get it now. You are trying to assign the client side array, fieldNames, the values of all the fieldNames in your recordset --

just take the quotes off the above statement...

FieldNames(i) = <% = ObjRecordSet.Fields.Item(cint(i)).Name %>

that should clear it up, and sorry for the confusion--
paul
 
Paul,

Thanks for your help. Unfortunately it didn't work!!!!!!. Array stores with &quot;[object]&quot; instead of field name.

Anyway, I had tweak a bit in my code to achieve what I want. Once again thanks a lot for your answers. I will check back If you have better idea.

Dilip
 
Ok, so I whip out Interdev, and here are my findings. It's not going to help you, but maybe someone will read this and be able to shed some light on it. Here is a simplified version of what you were trying to do:

dim fields(), objField
redim fields(rs.Fields.Count)
for each objField in rs.fields
fields(i) = (objField.Name)
next

The above produces a bunch of empty output when I write the contents of the array, fields, to the screen with a simple for loop... HOWEVER --

dim fields(), objField
redim fields(rs.Fields.Count)
for each objField in rs.fields
response.write(objField.Name & &quot;<br>&quot;)
next

The above writes each of my field names to the screen. What am I missing here??? I tried to cstr the results of the .name query and to no avail... I'm stumped.



 
Okie doke, then -- I was forgeting to initialize and increment i up there...

707, I figured it out, and it takes a full fledged **hack** to make it work

I finally saw what the real underlying problem was, and that was that you were trying to access a server side array element with a client side variable.... the two don't mix --

Use this:
Code:
<%
dim objFields, fields(), i
i = 1
redim fields(ObjRecordset.fields.count)
for each objFields in ObjRecordset.fields
  fields(i) = ObjRecordset.fields.name
  i = i + 1
next
%>

Ok, so now you have a server side array with all the field names in it... now for the hack -- you are going to write the client side vbscript function with server side response.writes -- and here goes:
Code:
<%
response.write(&quot;<script language=&quot;VBScript&quot;>&quot; & vbcr)
response.write(&quot;Sub onAdd()&quot; & vbcr)
response.write(&quot;  Dim FieldNames(&quot; & ObjRecordset.fields.count & &quot;)&quot;& vbcr)
for i = 1 to ObjRecordset.fields.count 
  response.write(&quot;    FieldNames(&quot; & i & &quot;) = &quot;&quot;&quot; & fields(i) &quot;&quot;&quot;&quot; & vbcr)
next
response.write(&quot;End Sub&quot;)
response.write(&quot;</script>&quot;)
%>

It was getting a little personal there.

I tested what I've posted here, and the resulting vbscript function looks great! ;-)
paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top