Solved

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

Posted on 2001-07-03
13
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript to allow login/password authorization 4 63
wordpress display sub menu only when click 12 88
ASP AND XML 3 40
Passing ASP variables in JQuery 4 49
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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