• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

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

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
muchowebdotcom
Asked:
muchowebdotcom
  • 7
  • 4
  • 2
1 Solution
 
epeeleCommented:
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
 
epeeleCommented:
Could you explain a little more about the logic you are using or post the section of code affected?
0
 
muchowebdotcomAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
muchowebdotcomAuthor Commented:
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
 
muchowebdotcomAuthor Commented:
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
 
epeeleCommented:
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
 
muchowebdotcomAuthor Commented:
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
 
muchowebdotcomAuthor Commented:
I may have the problem.. BRB
0
 
nigelroweCommented:
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
 
muchowebdotcomAuthor Commented:
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
 
epeeleCommented:
Glad you were able to track down the problem! And thanks for the points! :)
0
 
nigelroweCommented:
Yes I think that should be ...

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

0
 
muchowebdotcomAuthor Commented:
Ahh thanks..
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now