Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
IF Statement with two variables | Excel Worksheet Functions | |||
how do i if statement for 3 variables | Excel Worksheet Functions | |||
Conditional Statement with many variables | Excel Discussion (Misc queries) | |||
Too many variables for an if than statement? | New Users to Excel | |||
Variables in a Range statement | Excel Programming |