[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I substitute a variable here rs2.Fields("fieldName")

Posted on 2001-07-03
13
Medium Priority
?
286 Views
Last Modified: 2008-03-10
OK in this code...

rs2.Fields("fieldName")

I want to substitute the hard coded field name with a variable but it does not seem to work..

  Something like this..

MyVariable = "FirstName"

rs2.Fields(MyVariable)

  I really need to get this working because there is no way in hell I can hard code the field name. The MyVariable will change on each loop.

 Any help?
0
Comment
Question by:muchowebdotcom
  • 7
  • 4
  • 2
13 Comments
 
LVL 4

Expert Comment

by:epeele
ID: 6248528
What you have should work. I tried it on some code I have and it seemed to work fine.  So maybe there is some other problem. Another way to go about trying it would be:

eval("rs2.Fields(" & MyVariable & ")")
0
 
LVL 4

Expert Comment

by:epeele
ID: 6248539
Could you explain a little more about the logic you are using or post the section of code affected?
0
 
LVL 1

Author Comment

by:muchowebdotcom
ID: 6248550
Basically I am pulling all the fields from an HTML form one at a time by looping through something like this:


Dim ix, fieldName, fieldValue

For ix = 1 to Request.Form.Count

    fieldName = Request.Form.Key(ix)
    fieldName = Replace(fieldName, "'", "''")
   
    fieldValue = Request.Form.Item(ix)
    fieldValue = Replace(fieldValue, "'", "''")

SNIP etc..

  Thing is that I don't know what my database filed name will be until I loop and read the field name on the HTML form. See my field names in the database are identical to the field names in the HTML form.. I had this all working by doing an insert but now I changed ti so I caould ge the AutoID field before closing the recordset.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:muchowebdotcom
ID: 6248587
What deos the eval do for me?

I get this when I use your above example..

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'eval'

/addvehicle.asp, line 80
0
 
LVL 1

Author Comment

by:muchowebdotcom
ID: 6248604
Basically with my code I get this:

ADODB.Recordset error '800a0cc1'

Item cannot be found in the collection corresponding to the requested name or ordinal.

/addvehicle.asp, line 81


I assume this means that it can't find the fieldName..

I know the variable is populated so ????
0
 
LVL 4

Accepted Solution

by:
epeele earned 800 total points
ID: 6248660
Have you tried response.writing the fieldnames as they are inserting into the database to make sure that there are no typos or syntax errors?  

eval executes or evaluates a string.  I wouldn't really waste time on that, because your initial scenario should work provided you're not trying to insert a value for a field that doesn't exist.  You are taking into account your submit and reset buttons aren't you as they get passed as form elements too?
0
 
LVL 1

Author Comment

by:muchowebdotcom
ID: 6248700
Yes, I am taking into account those fileds and I have eliminated them with some

If fieldName <> Submit Then

 Etc..

See the reason I don't suspect a field missing is because I have not changed them from a few hours ago when it was all working using an insert statement. The onyl thing I am changing is the way I am add the record.. Hmmm.. Let me see???
0
 
LVL 1

Author Comment

by:muchowebdotcom
ID: 6248711
I may have the problem.. BRB
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6248740
First of all Eval does not exist in VBScript, although something like it exists in another MS object (for the life of me I cannot remeber what it's called).

I suppose another way to do it would be to iterate through your form field names and Recordset field names until you found a match. Then you would have the physical field position, something like...

For ix = 1 to Request.Form.Count
  For iy = 0 To Rs.Fields.Count - 1
    If Rs.Fields(iy).Name= Request.Form.Key(ix).Name Then
      phys_pos = iy
      Exit For
    End If
  Next
Next

Just a shot in the dark....
0
 
LVL 1

Author Comment

by:muchowebdotcom
ID: 6248753
Fixed.. Somehow or another my elimination of one of the fields was not working..

I had this basically...

If (fieldName <> "Submit") OR (fieldName <> "Password") Then

Do the code

End If

  Anyhow.. The password one did not eliminate for some reason.. I probably am not writing the OR code right.. Oh well .. Thanks.. I will give you the points of course for helping me through this..
0
 
LVL 4

Expert Comment

by:epeele
ID: 6248787
Glad you were able to track down the problem! And thanks for the points! :)
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6248869
Yes I think that should be ...

If (fieldName <> "Submit") And (fieldName <> "Password") Then

0
 
LVL 1

Author Comment

by:muchowebdotcom
ID: 6248907
Ahh thanks..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question