473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Syntax problem using string for table name in SQL statement

Good morning everybody.
I have the following problem. I would like to use a table name as variable (readed directly from a form) in a SELECT sql statement. I can't find the proper syntax to make it work and I get always a syntax error for this particular statement. It works perfectly when I use a variable for a field name, but stops working when I use a variable for table name. Any help will be more than appreciated.

Here is the working code where the variable is a field name:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.Run SQL "SELECT FMFactorsCodes.Factor_RiskPremium, Count(FMFactorsCodes.Factor_Code) AS CountOfFactor_Code INTO tblFactorsCodes1 " & _
  2. " FROM FMFactorsCodes WHERE [FMFactorsCodes].[" & Me.FactListSelector1 & "]=1 GROUP BY FMFactorsCodes.Factor_RiskPremium", False
And here is the NON-working code that I'm trying to run where the variable is a table name:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.Run SQL "SELECT [" & Me.FactListSelector1 & "].[Factor_RiskPremium], Count([" & Me.FactListSelector1 & "].Factor_Code) AS CountOfFactor_Code INTO tblFactorsCodes1 " & _
  2. " FROM [" & Me.FactListSelector1 & "] WHERE [" & Me.FactListSelector1 & "].[Factor_Name] IS NOT Null GROUP BY [" & Me.FactListSelector1 & "].Factor_RiskPremium", False
Thank you in advance.
Mar 26 '10 #1
3 2698
I think I found myself a straightforward way to do it, still keeping table name as variable:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "SELECT Count(Factor_Code) AS CountOfFactor_Code, Factor_RiskPremium INTO tblFactorsCodes1 " & _
  2. "FROM " & Me.FactListSelector1 & " GROUP BY Factor_RiskPremium", False
M.
Mar 26 '10 #2
NeoPa
32,556 Expert Mod 16PB
I've checked both your working and your non-working SQL. They're both perfect as far as the usage of the variable as the table name goes.

It's always a good idea when selecting from a single record source (as you are in this case) to omit it from field references. It's not necessary as with a single record source there is no ambiguity.

This brings us to the fundamental difference between the two versions. The problem one has a WHERE clause which is missing from the version which works. The format of the WHERE clause is perfect, so I can only assume there is a problem with [Factor_Name] field, at least in the table you were working with when you had the problem.

Would it be possible to learn what problem was reported when you attempted the code which didn't work? I'm not sure why this wasn't part of the original question. Perhaps you overlooked it.
Mar 26 '10 #3
NeoPa
32,556 Expert Mod 16PB
Actually, forget that question. Your working version uses DoCmd.RunSQL, whereas the problem version tries to use DoCmd.Run SQL. That would certainly explain the problem. I thought the SQL looked good.
Mar 26 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
1
by: Donald Canton | last post by:
Hi, I'm using Bjarne's book to learn C++ and am stuck on the Calc program in Section 6. Everything works fine except when I try to use istringstream to parse a token from the command line. I...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
8
by: tom | last post by:
I am new to SQL administration. >From a list of IDs that are the primary key in one table (i.e. Customer Table), I want to make changes in tables that use those IDs as a foreign key. ...
12
by: ColinWard | last post by:
Hi. I am trying to run the following code when the user clicks a button, but I am getting a syntax error in the SQL. I have a feeling it has to do with brackets. Can anyone help? here is the...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
5
by: amitbadgi | last post by:
Hi guys, I am getting the following error in teh insert statement , I am converting this asp application to asp.net, here is teh error, Exception Details:...
1
by: solomon_13000 | last post by:
connection.asp: <% Sub RunQueryString (pSQL,parms) on error resume next Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &...
3
by: teddymeu | last post by:
Hi Guys, new to development using visual studio and vb.net 2.0. and SQL express. Have a small problem. I have an update command which im using to change the values of a blob image table to NULL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.