[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

passing a variable through ADO?

Okay normally when using ADO, to access a recordset, let's say for example Adodc1.Recordset![fieldname] now what I want to do is set it up so I could send a variable value... how do I do that?  Adodc1.Recordset!(a variable As String)  is that even possible, if not what are the alternatives?  I would like to use a lookup table to pick-up these fieldnames.
0
Trieu
Asked:
Trieu
1 Solution
 
mcriderCommented:
Youre going to have to use a case statement...


For example Lets say you have a field called "CustomerName".  You can do this:

Dim X as String
X="CustomerName"

Select Case X
   Case "CustomerAddress"
         Adodc1.Recordset!CustomerAddress
       
   Case "CustomerName"
         Adodc1.Recordset!CustomerName
End Select



Cheers!
0
 
twolCommented:
This is a much easier way. I hope you select this as an answer:


instead of

rs!fieldname

use

rs.fields("fieldname")

(use variable for "fieldname")
0
 
Éric MoreauSenior .Net ConsultantCommented:
twol made a typo! There are no quotes around the variable name.

dim x as string

  x= "SomeFieldName"

  msgbox rs(x)
or
  msgbox rs.fields(x)
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.

 
TrieuAuthor Commented:
With so many fields coming in, this is definitely the best method.  The first method would work too, but it would mean I have to make way too many select case statements.  This way is very simple for me.  Thanks to both of you.  Thanks to you to emoreau, but I did pick up the fact that the quotes were not required.  I am going to alot the points to twol.

Thanks everyone,
Trieu
0
 
mcriderCommented:
twol's way is also available to you as long as you don't use the rs!fieldname format, however, just remember if the field in the variable does not exist, VB WILL error out and you will need an error handler... Using the case, you get to check the validity of the fieldname before you make the call...

It's your choice...


Cheers!
0
 
TrieuAuthor Commented:
mcrider, that is a very good point.  I have created an error handler for that just in case the fieldname doesn't exist or if any other error occurs.  If I didn't have so many different tables and fieldnames to worry about, I would have used your meethod.  If there was a way, I would give you some points too....

Trieu
0
 
mcriderCommented:
If you want to throw a couple of points my way, you can create a new question with the title "FOR MCRIDER ONLY" and I will answer it...  Good-Luck!


Cheers!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now