How would one go about using variable names in SQL statements? Is it possible?
Thanks for helping:-)
Ken Sturgeon
http://www.inlink.com/~fishhead
How would one go about using variable names in SQL statements? Is it possible?
Thanks for helping:-)
Ken Sturgeon
http://www.inlink.com/~fishhead
Function SelectFromAnyTable(sTableName As String) As String
SelectFromAnyTable = "SELECT * FROM " & sTableName
End Function
Did I misunderstand the question?
Joe
Never underestimate the power of a WAG.
http://www.citilink.com/~jgarrick/vbasic/
Dim SQL As String: SQL = "select * from people where name = """ Me!Text1.Text & """;"
Set MyRecordset = MyDB.OpenRecordset(SQL)
Dim QD As QueryDef: Set QD = MyDB.OpenQueryDef("my query")
QD!Param1 = Me!Text1.Text
Set MyRecordset = QD.OpenRecordset()
--
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!
Do you mean like SQL = "SELECT * FROM PEOPLE WHERE [LAST NAME] = ' " &
acLastName & " ' "
or do you mean Named Variables?
> Thanks for helping:-)
> Ken Sturgeon
> http://www.inlink.com/~fishhead
Yes, you can put variable values into an SQL statement. Here is some
sample code.
Important - You must embed all text in single quotes
Note:
myDatabase must be opened prior to executing the SQL
Field1 is a string
Field2 is an integer
Dim myVar as String
Dim myInt as Integer
Dim SQL$
myVar = "ABCD"
myInt = 20
SQL$ = "Update Table Set Field1='" & myVar & "' Where Field1='DEFG'"
myDatabase.Execute SQL$
SQL$ = "Update Table Set Field2=" & myInt & " Where Field2=10"
myDatabase.Execute SQL$
Hope this helps
Rob.
>Thanks for helping:-)
>Ken Sturgeon
>http://www.inlink.com/~fishhead
I am not sure if this is what you are looking for:
SELECT "MyVarName" = MyTable.MyColumnName FROM MyTable
> Important - You must embed all text in single quotes
> Note:
> myDatabase must be opened prior to executing the SQL
> Field1 is a string
> Field2 is an integer
> Dim myVar as String
> Dim myInt as Integer
> Dim SQL$
> myVar = "ABCD"
> myInt = 20
> SQL$ = "Update Table Set Field1='" & myVar & "' Where Field1='DEFG'"
> myDatabase.Execute SQL$
> SQL$ = "Update Table Set Field2=" & myInt & " Where Field2=10"
> myDatabase.Execute SQL$
> Hope this helps
> Rob.
> >How would one go about using variable names in SQL statements? Is it
possible?
> >Thanks for helping:-)
> >Ken Sturgeon
> >http://www.inlink.com/~fishhead
If you use RDO, it is possible to create a "parameter" query that allows
you to have replacable parameters. In fact, you use the rdoParameters
collection to set the values of the parameters for each execution. See the
subject "Creating Parameter Queries" in the VB help. BUD
> Important - You must embed all text in single quotes
> Note:
> myDatabase must be opened prior to executing the SQL
> Field1 is a string
> Field2 is an integer
> Dim myVar as String
> Dim myInt as Integer
> Dim SQL$
> myVar = "ABCD"
> myInt = 20
> SQL$ = "Update Table Set Field1='" & myVar & "' Where Field1='DEFG'"
> myDatabase.Execute SQL$
> SQL$ = "Update Table Set Field2=" & myInt & " Where Field2=10"
> myDatabase.Execute SQL$
> Hope this helps
> Rob.
> >How would one go about using variable names in SQL statements? Is it
possible?
> >Thanks for helping:-)
> >Ken Sturgeon
> >http://www.inlink.com/~fishhead
1. Plugging variables into a select statement
Two possibilities:
SELECT * FROM mytable
SET ROWCOUNT 0
or
If you're going to go the EXEC route, please read
http://www.algonet.se/~sommar/dynamic_sql.html
(This specific case is discussed at
http://www.algonet.se/~sommar/dynamic_sql.html#Top )
--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/
Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.
2. Looking for dBase ODBC Drivers
3. I am trying to store the result of a dynamic sql query in a variable
5. Plug-In Azuim Variables ???
6. MA-Cambridge-98790--ORACLE-DBA Skills-Oracle DBA
7. I am having a problem with the following SQL statements:
8. screens not generating when generating projects
9. ? Excuting a variable or variable columns in a create statement
10. Set a variable to equal a select statement containing variables
11. Can you pass a where with multiple statements without a variable for each where statement
12. Global variable as SQL statement on oracle connection