Solved

SQL Statement

Posted on 2014-12-18
9
114 Views
Last Modified: 2014-12-18
I hate coding....Someone help

strSQL = "SELECT DISTINCT Final_Table.ID, dbo_tblOrgLook_master.Analyst, dbo_tblOrgLook_master.Org, dbo_tblOrgLook_master.OrgName, dbo_tblOrgLook_master.CostCenter, dbo_tblOrgLook_master.Fund, dbo_tblOrgLook_master.PEC, dbo_tblOrgLook_master.ProgramName, Final_Table.[Line Item:], Final_Table.[Item Number:], Final_Table.[Total Initial:], Final_Table.BC1Chng1, Final_Table.BC1Chng2, Final_Table.BC1Chng3, Final_Table.BC1Chng4, Final_Table.BC1Chng5, Final_Table.BC1Chng6, Final_Table.BC1Chng7, Final_Table.TotalBC1, Final_Table.BC2Chng1, Final_Table.BC2Chng2, "
Final_Table.BC2Chng3, Final_Table.BC2Chng4, Final_Table.BC2Chng5, Final_Table.BC2Chng6, Final_Table.BC2Chng7, Final_Table.TotalBC2, Final_Table.BC3Chng1, Final_Table.BC3Chng2, Final_Table.BC3Chng3, Final_Table.BC3Chng4, Final_Table.BC3Chng5, Final_Table.BC3Chng6, Final_Table.BC3Chng7, Final_Table.TotalBC3, Final_Table.BC4Chng1, Final_Table.BC4Chng2, Final_Table.BC4Chng3, Final_Table.BC4Chng4, Final_Table.BC4Chng5, Final_Table.BC4Chng6, Final_Table.BC4Chng7, Final_Table.TotalBC4, Final_Table.[Adjust Category], Final_Table.Remarks, Final_Table.UpdatedBy, Final_Table.UpdatedDate"
strSQL = strSQL & "FROM Final_Table INNER JOIN dbo_tblOrgLook_master ON (Final_Table.CostCen = dbo_tblOrgLook_master.CostCenter) AND (Final_Table.PEC = dbo_tblOrgLook_master.PEC) WHERE dbo_tblOrgLook_master.Analyst " & Analyst & " AND dbo_tblOrgLook_master.Org " & Org & " AND dbo_tblOrgLook_master.CostCenter " & CostCenter & " AND dbo_tblOrgLook_master.Fund " & Fund & " AND dbo_tblOrgLook_master.PEC " & PEC & " ORDER BY Final_Table.[Item Number:]"

Open in new window


For some reason this does not work?
0
Comment
Question by:Jass Saini
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40507785
Define "does not work".

Does it give you an error message or is it not returning the records you expect.  

If the former, please post the error message.  If the latter, can you describe what you want the query to do.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40507786
At least you missed a Space here:

strSQL & " FROM ...

/gustav
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40507788
try inserting a space before "FROM Final"

strSQL = strSQL & "FROM

should be
----------------------------v-----
strSQL = strSQL & " FROM
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40507805
My guess is that it has to do with your Where clause, which looks like:

"WHERE dbo_tblOrgLook_master.Analyst " & Analyst & " AND dbo_tblOrgLook_master.Org " & Org & " AND dbo_tblOrgLook_master.CostCenter " & CostCenter & " AND dbo_tblOrgLook_master.Fund " & Fund & " AND dbo_tblOrgLook_master.PEC " & PEC

Generally, you would need to put an = sign in there somewhere, and for fields that are non-numeric, you would have to offset the value with quotes.  So, assuming that Org is a string and the others are all numeric, it might look like:

" WHERE dbo_tblOrgLook_master.Analyst = '" & Analyst & "' " _
& " AND dbo_tblOrgLook_master.Org =" & Org _
& " AND dbo_tblOrgLook_master.CostCenter =" & CostCenter _
& " AND dbo_tblOrgLook_master.Fund =" & Fund _
& " AND dbo_tblOrgLook_master.PEC =" & PEC
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40507806
how are the variables Analyst, Org, CostCenter, Fund, PEC defined?
there should be an equal sign before them and should be treated depending on the Data Type
i.e.,
if Analyst is Number
WHERE dbo_tblOrgLook_master.Analyst = " & Analyst & "

if Analyst is Text
WHERE dbo_tblOrgLook_master.Analyst = '" & Analyst & "'

etc...
0
 

Author Comment

by:Jass Saini
ID: 40507959
Sorry guys..line 2 is red....
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40507982
You miss between line 1 and 2:

" & _
"

and the previously mentioned missing Space on line 3:

strSQL = "SELECT DISTINCT Final_Table.ID, dbo_tblOrgLook_master.Analyst, dbo_tblOrgLook_master.Org, dbo_tblOrgLook_master.OrgName, dbo_tblOrgLook_master.CostCenter, dbo_tblOrgLook_master.Fund, dbo_tblOrgLook_master.PEC, dbo_tblOrgLook_master.ProgramName, Final_Table.[Line Item:], Final_Table.[Item Number:], Final_Table.[Total Initial:], Final_Table.BC1Chng1, Final_Table.BC1Chng2, Final_Table.BC1Chng3, Final_Table.BC1Chng4, Final_Table.BC1Chng5, Final_Table.BC1Chng6, Final_Table.BC1Chng7, Final_Table.TotalBC1, Final_Table.BC2Chng1, Final_Table.BC2Chng2, " & _
"Final_Table.BC2Chng3, Final_Table.BC2Chng4, Final_Table.BC2Chng5, Final_Table.BC2Chng6, Final_Table.BC2Chng7, Final_Table.TotalBC2, Final_Table.BC3Chng1, Final_Table.BC3Chng2, Final_Table.BC3Chng3, Final_Table.BC3Chng4, Final_Table.BC3Chng5, Final_Table.BC3Chng6, Final_Table.BC3Chng7, Final_Table.TotalBC3, Final_Table.BC4Chng1, Final_Table.BC4Chng2, Final_Table.BC4Chng3, Final_Table.BC4Chng4, Final_Table.BC4Chng5, Final_Table.BC4Chng6, Final_Table.BC4Chng7, Final_Table.TotalBC4, Final_Table.[Adjust Category], Final_Table.Remarks, Final_Table.UpdatedBy, Final_Table.UpdatedDate"
strSQL = strSQL & " FROM Final_Table INNER JOIN dbo_tblOrgLook_master ON (Final_Table.CostCen = dbo_tblOrgLook_master.CostCenter) AND (Final_Table.PEC = dbo_tblOrgLook_master.PEC) WHERE dbo_tblOrgLook_master.Analyst " & Analyst & " AND dbo_tblOrgLook_master.Org " & Org & " AND dbo_tblOrgLook_master.CostCenter " & CostCenter & " AND dbo_tblOrgLook_master.Fund " & Fund & " AND dbo_tblOrgLook_master.PEC " & PEC & " ORDER BY Final_Table.[Item Number:]"

Open in new window

0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 40508030
Unless there is some reason for this query to be embedded, you will have better success using the QBE to build SQL statements if you are unfamiliar with SQL syntax.  You can have criteria that references forms to supply variable values. The WHERE clause will end up looking like:

WHERE dbo_tblOrgLook_master.Analyst = Forms!YourForm!Analyst AND dbo_tblOrgLook_master.Org = Forms!YourForm!Org AND dbo_tblOrgLook_master.CostCenter = Forms!YourForm!CostCenter AND dbo_tblOrgLook_master.Fund = Forms!YourForm!Fund  AND dbo_tblOrgLook_master.PEC = Forms!YourForm!PEC

If you use embedded SQL, always build it into a variable as you are doing.  Then, if it isn't running correctly, you can put a stop in the code and print the contents of the variable to the immediate window.  Usually simply seeing the string is usually enough.  If you can't "see" the issue, copy the string from the immediate window and paste it into the QBE and run it from there.  In almost all cases, you will get different and usually better error messages.

PS, each embedded SQL string must be compiled and an execution plan generated every time the string runs whereas, querydefs are compiled the first time they run and the execution plan is saved for later use.  So, it is more efficient to use querydefs to avoid the overhead of constantly recreating an execution plan.  Of course if you only run the query once per day, the hit is miniscule but if you run the query multiple times, you are simply adding unnecessary overhead.
0
 

Author Closing Comment

by:Jass Saini
ID: 40508057
Thanks
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now