Using Variable Names in SQL queries..... 
Author Message
 Using Variable Names in SQL queries.....

Hi,

Does anyone know how to use variable names in SQL queries?  Here's my
problem:

I have a searchbox to find a record in a Dynaset.  I have a combo box that
lists all the field in my database and a text box that the user types in the
search pattern to look for.  Now, I can use a variable to represent the
search string, but I can't use a variable to represent the field name that I
want to search on.

ie.

  Select * From myTable WHERE COMPANY Like 'A*'

would return all the companies that start with A from myTable.

But, I want to replace myTable, COMPANY, and 'A*' all with variables.  That
way the user can choose what field to search on.  

I got the following syntax from the programmers reference:

                '" & variablename & "'

making the statement:

  Select * From myTable WHERE COMPANY Like '" & variable1 & "'

This line works, but when I try and change COMPANY to a variable, it fails.

Any ideas?  I can't imagine it being to different, but I am probably missing
something obvious.........  Any help is greatly appreciated.

Thanks.

Walter



Tue, 01 Sep 1998 03:00:00 GMT  
 Using Variable Names in SQL queries.....
Howdy Walter!

Quote:
>Does anyone know how to use variable names in SQL queries?...
>  Select * From myTable WHERE COMPANY Like '" & variable1 & "'
>This line works, but when I try and change COMPANY to a variable, it >fails.

Sure, I have routines which allow users to filter/sort recordsets based
on table field names and input values. We can't tell what you're doing
wrong from your description. When you say 'fails', what do you mean? If
you can be more specific (perhaps a code sample or error message), I'm
sure someone here can help you. The code I have would take too much
bandwidth to post here. Try reposting or just drop me a line.

Cordially,
dmc



Tue, 01 Sep 1998 03:00:00 GMT  
 Using Variable Names in SQL queries.....
I'm going to take a guess here:

are you by any chance trying to do something like
        "select * from MyTable where '" & CompanyStr & "' like etc"?
If this is the case then you should try
        "select * from MyTable where " & CompanyStr & " like etc"

Quotes should only surround string variables (considered as string
variables by SQL not by VB) so that tblname does not require quotes
around it whereas any string variable such as variable1 does.

Sometime I find it very useful to store the sql string in a variable
and then just display the completed sql string in a message box. If
what you see displayed looks like a valid sqlstring (taking special
care about the presence/absence of quotes) then it should be OK.

Fred.


Quote:
>Howdy Walter!
>>Does anyone know how to use variable names in SQL queries?...
>>  Select * From myTable WHERE COMPANY Like '" & variable1 & "'
>>This line works, but when I try and change COMPANY to a variable, it >fails.
>Sure, I have routines which allow users to filter/sort recordsets based
>on table field names and input values. We can't tell what you're doing
>wrong from your description. When you say 'fails', what do you mean? If
>you can be more specific (perhaps a code sample or error message), I'm
>sure someone here can help you. The code I have would take too much
>bandwidth to post here. Try reposting or just drop me a line.
>Cordially,
>dmc

Fred Baptiste



Wed, 02 Sep 1998 03:00:00 GMT  
 Using Variable Names in SQL queries.....

Quote:

>Hi,
>Does anyone know how to use variable names in SQL queries?  Here's my
>problem:
>I have a searchbox to find a record in a Dynaset.  I have a combo box that
>lists all the field in my database and a text box that the user types in the
>search pattern to look for.  Now, I can use a variable to represent the
>search string, but I can't use a variable to represent the field name that I
>want to search on.
>ie.
>  Select * From myTable WHERE COMPANY Like 'A*'
>would return all the companies that start with A from myTable.
>But, I want to replace myTable, COMPANY, and 'A*' all with variables.  That
>way the user can choose what field to search on.  
>I got the following syntax from the programmers reference:
>            '" & variablename & "'
>making the statement:
>  Select * From myTable WHERE COMPANY Like '" & variable1 & "'
>This line works, but when I try and change COMPANY to a variable, it fails.
>Any ideas?  I can't imagine it being to different, but I am probably missing
>something obvious.........  Any help is greatly appreciated.
>Thanks.
>Walter

Walter,

Do not use the double set of quote marks for the column name, as SQL
only want quotes around literals.  If you look at the string in the
debug window, you can see what VB is building.  It should look like:

select * from MYTABALE where COMPANY like 'A%'

not like:

select * from MYTABALE where 'COMPANY' like 'A%'

Good luck
--
Mark Wagoner



Fri, 04 Sep 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. SQL Query using variables

2. Sql query statement (using a variable)

3. How to use variables in a sql query using VBA

4. Using VB variable in SQL query

5. PLEASE HELP: Using Variables within SQL Queries

6. Using variables in SQL queries

7. Help with SQL query using LIKE and variable field

8. SQL query using a variable

9. Using a variable in a SQL Query

10. Help:SQL Query using a Date/time variable

11. Help with SQL query using LIKE and variable field

12. Find using sql query with variable having an apostrophe

 

 
Powered by phpBB® Forum Software