473,404 Members | 2,137 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,404 software developers and data experts.

CreateQueryDef failure

3
I have several queries that I create from SQL statements using Currentdb.CreateQueryDef in one of these cases the code breaks and refuses to create the query giving me a:
Run-time error 3131. Syntax error in FROM clause. I'm supposing that it won't create the query because it analyzes the SQL string and determines that there is a syntax error in the FROM clause of the query I am trying to create.

The odd thing is that if I take this same SQL string and paste it into the Access QBE it runs just fine. Does anyone have a clue as to what might be wrong. Below is both the "offending" line of code as well as the entire function containing the SQL statement.

==== Function below ========

Expand|Select|Wrap|Line Numbers
  1. Function AggAlloc()
  2. Dim db As Database, strSQL As String
  3. Set db = CurrentDb
  4.  
  5. strSQL = "SELECT HR_Subset.[Person Number], HR_Subset.[Company/Cost Center Code/Name], HR_Subset.[Band], " & _
  6. "HR_Subset.[First Name], HR_Subset.[Last Name], HR_Subset.FullName, HR_Subset.[Middle Name], " & _
  7. "HR_Subset.[Preferred Name], HR_Subset.[NB ID], HR_Subset.[Associate E-mail address], HR_Subset.[Work Phone], " & _
  8. "HR_Subset.[Work Fax], HR_Subset.[Mail Code], HR_Subset.[Work Address 1], HR_Subset.[Work Address 2], " & _
  9. "HR_Subset.[Work City], HR_Subset.[Work County], HR_Subset.[Work State], HR_Subset.[Work Zip], " & _
  10. "HR_Subset.[Work Country], qryManagers.[First Name] AS [Manager First Name], qryManagers.[Last Name] " & _
  11. "AS [Manager Last Name], qryManagers.[Middle Name] AS [Manager Middle Name], qryManagers.[Associate E-mail address] " & _
  12. "AS [Manager E-mail Address], qryManagers.[Work Phone] AS [Manager Work Phone], qryManagers.[Mail Code] " & _
  13. "AS [Manager Mail Code], qryManagers.[Work Address 1] AS [Manager Work Address 1], qryManagers.[Work Address 2] " & _
  14. "AS [Manager Work Address 2], qryManagers.[Work City] AS [Manager Work City], qryManagers.[Work County]" & _
  15. "AS [Manager Work County], qryManagers.[Work State] AS [Manager Work State], qryManagers.[Work Zip] AS " & _
  16. "[Manager Work Zip], HR_Subset.[Job Code], HR_Subset.[Job Title], tblAllocations_Crosstab.[200607], " & _
  17. "tblAllocations_Crosstab.[200608], tblAllocations_Crosstab.[200609], tblAllocations_Crosstab.[200610], " & _
  18. "tblAllocations_Crosstab.[200611], tblAllocations_Crosstab.[200612], tblAllocations_Crosstab.[200701], " & _
  19. "tblAllocations_Crosstab.[200702], tblAllocations_Crosstab.[200703], tblAllocations_Crosstab.[200704], " & _
  20. "tblAllocations_Crosstab.[200705], tblAllocations_Crosstab.[200706], tblAllocations_Crosstab.[200707], " & _
  21. "tblAllocations_Crosstab.[200708], tblAllocations_Crosstab.[200709], tblAllocations_Crosstab.[200710], " & _
  22. "tblAllocations_Crosstab.[200711], tblAllocations_Crosstab.[200712] FROM (qryManagers RIGHT JOIN HR_Subset " & _
  23. "ON qryManagers.[Person Number] = HR_Subset.[Person Number]) LEFT JOIN tblAllocations_Crosstab" & _
  24. "ON HR_Subset.[NB ID] = tblAllocations_Crosstab.[Resource ID]"
  25. If QueryExists("qryReport_AggAlloc") Then DoCmd.DeleteObject acQuery, "qryReport_AggAlloc"
  26. db.CreateQueryDef "qryReport_AggAlloc", strSQL '<----Problem line
  27. Set db = Nothing
  28. End Function

Thanks,
Expat
Aug 1 '06 #1
8 3517
Expat
3
Well....it's been more than half a day since I posted my question and no one has responded. I'm new to this site so I'm guessing that not many people use this site, no one knows the answer to my question or....?

At any rate no one needs to bother answering my question because I have the answer which i will share with you in case the same ever happens to you in the future.

The reason my createQueryDef failed was because, indeed as the Error message stated, there was an error in the FROM portion of my SQL statement. it was merely a problem of having used a couple of line continuation characters in such a place so as to introduce too many spaces between certain elements of the statement. the solution was to simply put the whole FROM portion of the SQL Statement onto one line, thereby avoiding syntax errors.
Aug 2 '06 #2
1.) Add one space to the start of each and every line.
to avoid there being NO space between TWO REAL words of the SQL

for example in your SQL
tblAllocations_Crosstab" & _
"ON HR_Subset.[NB ID] = tblAllocations_Crosstab.[Resource ID]"
There is no space between the end of crosstabs and the start of next line.

2.) prior to executing the SQL do a ......debug.print SQLstr . then cntl-g to see the printout. copy and past into NOTEPAD with a large font and look at it carefully for two words that a NOT seperated by a space. MAKE SURE that notepad is set to UNCHECK wordwrap
Aug 3 '06 #3
Expat
3
Good idea, Tuckerman, to paste into Notepad with a large font. I almost never have a problem with doing this because I usually do make sure that there are enough spaces between words in the statement but your suggestion to paste it from the immediate window into Notepad makes it easier still to see that there are indeed the proper spaces between elements of the SQL statement.

Thanks for your reply and suggestion.

EXPAT

1.) Add one space to the start of each and every line.
to avoid there being NO space between TWO REAL words of the SQL

for example in your SQL
tblAllocations_Crosstab" & _
"ON HR_Subset.[NB ID] = tblAllocations_Crosstab.[Resource ID]"
There is no space between the end of crosstabs and the start of next line.

2.) prior to executing the SQL do a ......debug.print SQLstr . then cntl-g to see the printout. copy and past into NOTEPAD with a large font and look at it carefully for two words that a NOT seperated by a space. MAKE SURE that notepad is set to UNCHECK wordwrap
Aug 3 '06 #4
I also encountered the same problem and tried adding space but it still didn't work! Can anyone spot my mistake, please?
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtSearch_Change()
  2.  
  3. If txtSearch.Text = vbNullString Then
  4.     Set rstSearch = dbsNormah.OpenRecordset("Children General Record", dbOpenTable)
  5.  
  6. Else
  7.  
  8. Set rstSearch = dbsNormah.OpenRecordset("Select * from Children General Record where surname = ' " & txtSearch.Text & " ' " & " & '*'")
  9.  
  10. End If
  11.  
  12. list
  13.  
  14. End Sub 
Dec 8 '06 #5
NeoPa
32,556 Expert Mod 16PB
Please do not Hijack another thread for your question.
For an answer to your question you need to post a new thread for it.
However, I notice that Tuckerman's advice would also help you to resolve your problem.
Remember also, to put [] around any table or field name with embedded spaces.
Dec 8 '06 #6
I'm really sorry but I thought since it is about the same topic, it will be easier if everything is grouped together.

I'll post a new post :D Thank you
Dec 9 '06 #7
MMcCarthy
14,534 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  LEFT JOIN tblAllocations_Crosstab " & _
  2. "ON HR_Subset.[NB ID] = tblAllocations_Crosstab.[Resource ID]"
  3.  
Expat,

I think you were missing a space between tblAllocations_Crosstab and ON.

Mary
Dec 9 '06 #8
NeoPa
32,556 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1.  LEFT JOIN tblAllocations_Crosstab " & _
  2. "ON HR_Subset.[NB ID] = tblAllocations_Crosstab.[Resource ID]"
  3.  
Expat,

I think you were missing a space between tblAllocations_Crosstab and ON.

Mary
Mary,

I know you're half-asleep and hung-over still - but he had an answer in early August! lol.
Don't worry - I almost did exactly the same thing when I was fully awake :(.

Babyspring,

You make a fair point and I hear where you're coming from.
In this case though, the problems don't seem to be similar enough to make it a good idea.
I haven't checked all the new threads yet but if you've posted then we'll get you an answer if we can.
Alternatively, I didn't want to answer you before directly, but certainly there are ideas posted as answers in this thread that should help you if you think about it.

-Adrian.
Dec 9 '06 #9

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

Similar topics

5
by: Andrew | last post by:
Hi: I run into the following problem where I would use CreateQuerydef to create a new Table and then use "Execute" to execute the statement. However, I keep getting an error message saying that...
3
by: priscilla.jenkins | last post by:
Alright, I'm really new to SQL and VBA and all this, so I might be completely off course...but just tell me. I know C and Assembly, but that doesn't help me much here. I'm trying to create a...
2
by: oh4real | last post by:
Howdy all. I have pretty much NO VB/Access coding experience, but am forced to come up with a generic script/process for users with varying database designs and varying levels of expertise to use...
6
by: rahuldev999 | last post by:
Hi I am beginner in the access project. can anyone suggest how to deal with this " Set rq = Db.CreateQueryDef("r_temp_rslt_research", strSql)" in the below code.As its a MDB application...
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: 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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.