Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Use variables in sql statement

I've built an sql string to get records from a database and store to excel.

sSql = "SELECT * FROM myTableName"

I want to replace myTableName with a variable (TableVar) to reference the
tablename typed in Cell A1.
I assigned the variable TableVar by doing the following:
TableVar = ActiveWorkbook.Worksheets("sheet1").Range("B1").Va lue
When I replace myTableName with TableVar, I get an error message that there
is no such table as TableVar.
What is the correct way to replace a table name with a variable?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Use variables in sql statement

You already have the table name in a variable, so you just need to
concatenate that variable into your sSql variable (rather than typing the
literal text TableVar). Do so like this:

sSql = "SELECT * FROM " & TableVar

That should get you on your way, as long as TableVar contains a valid name.

"Markantesp" wrote:

I've built an sql string to get records from a database and store to excel.

sSql = "SELECT * FROM myTableName"

I want to replace myTableName with a variable (TableVar) to reference the
tablename typed in Cell A1.
I assigned the variable TableVar by doing the following:
TableVar = ActiveWorkbook.Worksheets("sheet1").Range("B1").Va lue
When I replace myTableName with TableVar, I get an error message that there
is no such table as TableVar.
What is the correct way to replace a table name with a variable?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Use variables in sql statement

Is your code constructed like this?

Dim TableVar, sSql as String
TableVar = trim(ActiveWorkbook.Worksheets("sheet1").Range("B1 ").Value)
sSql = "SELECT * FROM " & TableVar

Note that space after FROM; I've missed a few of those myself.

-Glenn Ray

"Markantesp" wrote:

I've built an sql string to get records from a database and store to excel.

sSql = "SELECT * FROM myTableName"

I want to replace myTableName with a variable (TableVar) to reference the
tablename typed in Cell A1.
I assigned the variable TableVar by doing the following:
TableVar = ActiveWorkbook.Worksheets("sheet1").Range("B1").Va lue
When I replace myTableName with TableVar, I get an error message that there
is no such table as TableVar.
What is the correct way to replace a table name with a variable?

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
IF Statement with two variables RoadKill Excel Worksheet Functions 9 April 4th 23 12:47 PM
how do i if statement for 3 variables wheefus Excel Worksheet Functions 7 December 17th 07 07:26 PM
Conditional Statement with many variables Krista Excel Discussion (Misc queries) 3 May 12th 06 10:58 PM
Too many variables for an if than statement? repke New Users to Excel 3 May 2nd 06 05:01 PM
Variables in a Range statement ChuckM[_2_] Excel Programming 1 December 15th 03 04:16 PM


All times are GMT +1. The time now is 11:42 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"