Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Variable names in SQL

My VBA code has an SQL statement to call. I'm assigning the statement to a
string variable and use this variable to call it later. The statement has a
condition (the "where" clause) that needs to compare a column name to
another variable's value.

What is the syntax for this? For example:

testvalue="boo"
sqlstring="select * from mytable where mycolumn=testvalue"

How do I mark testvalue in the select string?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Variable names in SQL

Try something like the following:

sqlstring = "select * from mytable where mycolumn='" & testvalue
& "'"


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hall" wrote in message
...
My VBA code has an SQL statement to call. I'm assigning the

statement to a
string variable and use this variable to call it later. The

statement has a
condition (the "where" clause) that needs to compare a column

name to
another variable's value.

What is the syntax for this? For example:

testvalue="boo"
sqlstring="select * from mytable where mycolumn=testvalue"

How do I mark testvalue in the select string?




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Variable names in SQL

That usually works, but after 2 months you'll spend a few hours tracking
this down as a bug.
If testvalue contains a single-quote, the SQL fails.
One could write replace(testvalue, "'", "''"), but that's just nasty.

Whenever you want to pass values for SQL execution, use parameters. That's
what they are there for and they'll make your life easier.

I can't provide examples - I don't know what technology you're using.
ADO and ODBC certainly allow parameters. Even Excel's "Get External Data"
allows parameters.


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Chip Pearson" wrote in message
...
Try something like the following:

sqlstring = "select * from mytable where mycolumn='" & testvalue
& "'"


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Hall" wrote in message
...
My VBA code has an SQL statement to call. I'm assigning the

statement to a
string variable and use this variable to call it later. The

statement has a
condition (the "where" clause) that needs to compare a column

name to
another variable's value.

What is the syntax for this? For example:

testvalue="boo"
sqlstring="select * from mytable where mycolumn=testvalue"

How do I mark testvalue in the select string?






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable file names Paul Peterson - Velox Consulting, LLC Excel Discussion (Misc queries) 8 February 4th 10 01:54 PM
Variable Worksheet Names Karen53 Excel Worksheet Functions 4 August 28th 07 01:12 PM
Variable Names in Formulas Paulo Excel Discussion (Misc queries) 2 March 29th 06 01:16 PM
Combining variable names Bernie Gaile Excel Programming 2 December 28th 03 09:24 PM
Variable Control Names Garry Jones Excel Programming 4 October 3rd 03 10:45 PM


All times are GMT +1. The time now is 12:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"