Solved

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

Posted on 2001-07-03
13
271 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now