Link to home
Start Free TrialLog in
Avatar of muchowebdotcom
muchowebdotcom

asked on

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?
Avatar of epeele
epeele
Flag of United States of America image

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 & ")")
Could you explain a little more about the logic you are using or post the section of code affected?
Avatar of muchowebdotcom
muchowebdotcom

ASKER

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.

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
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 ????
ASKER CERTIFIED SOLUTION
Avatar of epeele
epeele
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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???
I may have the problem.. BRB
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....
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..
Glad you were able to track down the problem! And thanks for the points! :)
Yes I think that should be ...

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

Ahh thanks..