473,395 Members | 1,742 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,395 software developers and data experts.

using a VBA Module result in a SELECT Query

I'm running a SELECT Querry that has a Expression in it [Turnaround days]that calls a VB Function that returns the number of Workdays within a specified time period. The querry works fine except when I try to use a HAVING Clause to limit the results to >= six days. The error that returns" The expression is typed incorrectly or too complex to be evaluated. (It works fine except when I want to place criteria on it.)

I can ORDER BY the [Turnaround days] and even use it in other expressions, but I just can't apply criteria to it in a HAVING or WHERE Clause.

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW [Consultant Sub-Category_Sub].Activity_Nr, [Consultant Sub-Category_Sub].[Initial Assignment]
  2.    , [Consultant Sub-Category_Sub].Activity_Role, [Consultant Sub-Category_Sub].Activity_Status, [Consultant Sub-Category_Sub].Activity_Received
  3.    , [Consultant Sub-Category_Sub].Activity_Start, [Consultant Sub-Category_Sub].Activity_End
  4.    , Timings.Category, Timings.[Sub-Category]
  5.    , Count(*) AS [Count Of Consultant Sub-Category_Sub]
  6.    , Workdays([Activity_Received],[Activity_End])
  7.       AS [Turnaround days]
  8.    , Timings.[Total Time], [Consultant Sub-Category_Sub].On_Hold_Start
  9.    , Timings.Comments
  10.    , Int([Activity_End]-[Activity_Received]) AS [Previous calc Turnaround days]
  11. FROM Timings INNER JOIN [Consultant Sub-Category_Sub] 
  12.    ON Timings.Activity_Nr = [Consultant Sub-Category_Sub].Activity_Nr
  13. GROUP BY [Consultant Sub-Category_Sub].Activity_Nr, [Consultant Sub-Category_Sub].[Initial Assignment]
  14.    , [Consultant Sub-Category_Sub].Activity_Role, [Consultant Sub-Category_Sub].Activity_Status
  15.    , [Consultant Sub-Category_Sub].Activity_Received, [Consultant Sub-Category_Sub].Activity_Start
  16.    , [Consultant Sub-Category_Sub].Activity_End, Timings.Category
  17.    , Timings.[Sub-Category], Timings.[Total Time], [Consultant Sub-Category_Sub].On_Hold_Start
  18.    , Timings.Comments, Int([Activity_End]-[Activity_Received])
  19. HAVING (((Timings.Category)="questions") 
  20.    AND ((Workdays([Activity_Received],[Activity_End]))
  21.       >=6));
  22.  
Jun 19 '15 #1
8 1502
zmbd
5,501 Expert Mod 4TB
Open your query in SQL view and run from there... often the line with the error will be pointed to you.
Jun 20 '15 #2
Workdays As Turnarounddays. The function works. I just can't put any criteria on it. Does anyone know how it should be written??
Jun 20 '15 #3
Thank you. I fixed this by using an INTO Clause to save the results of the query that made use of the User Defined Function [Turnaround days]. The function returns a number in the same way that an expression would return a number. I can do operations on the expression in the querry, but not so with the user returned values. The expression in the query is [Previous calc Turnaround days].

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW [Consultant Sub-Category_Sub].Activity_Nr, [Consultant Sub-Category_Sub].[Initial Assignment]
  2.    , [Consultant Sub-Category_Sub].Activity_Role, [Consultant Sub-Category_Sub].Activity_Status, [Consultant Sub-Category_Sub].Activity_Received
  3.    , [Consultant Sub-Category_Sub].Activity_Start, [Consultant Sub-Category_Sub].Activity_End, Timings.Category, Timings.[Sub-Category]
  4.    , Count(*) 
  5.       AS [Count Of Consultant Sub-Category_Sub]
  6.    , Workdays([Activity_Received],[Activity_End]) 
  7.       AS Turnaround_days
  8.    , Timings.[Total Time], [Consultant Sub-Category_Sub].On_Hold_Start
  9.    , Timings.Comments
  10.    , Int([Activity_End]-[Activity_Received]) 
  11.       AS [Previous calc Turnaround days] 
  12. INTO tblTest
  13. FROM Timings 
  14.    INNER JOIN [Consultant Sub-Category_Sub] 
  15.       ON Timings.Activity_Nr = 
  16.          [Consultant Sub-Category_Sub].Activity_Nr
  17. GROUP BY [Consultant Sub-Category_Sub].Activity_Nr
  18.    , [Consultant Sub-Category_Sub].[Initial Assignment], [Consultant Sub-Category_Sub].Activity_Role
  19.    , [Consultant Sub-Category_Sub].Activity_Status, [Consultant Sub-Category_Sub].Activity_Received
  20.    , [Consultant Sub-Category_Sub].Activity_Start, [Consultant Sub-Category_Sub].Activity_End
  21.    , Timings.Category, Timings.[Sub-Category], Timings.[Total Time]
  22.    , [Consultant Sub-Category_Sub].On_Hold_Start, Timings.Comments
  23. HAVING (((Timings.[Sub-Category])='General Questions' 
  24.       Or (Timings.[Sub-Category])='Compliance Testing' 
  25.       Or (Timings.[Sub-Category])='Conference Calls' 
  26.       Or (Timings.[Sub-Category])='Operational Issues' 
  27.       Or (Timings.[Sub-Category])='PPA' 
  28.       Or (Timings.[Sub-Category])='Plan Document Interpretation' 
  29.       Or (Timings.[Sub-Category])='Plan Administration Questions or Errors' 
  30.       Or (Timings.[Sub-Category])='Controlled Group/Ownership' 
  31.       Or (Timings.[Sub-Category])='Loan And/Or Corrections')
  32.    AND (([Consultant Sub-Category_Sub].On_Hold_Start)
  33.        Is Null));
Jun 22 '15 #4
zmbd
5,501 Expert Mod 4TB
Sorry, I re-read your OP just now, I was completely off the mark. Let me take a closer look at your original code.

BTW: I'm not a fan of temporary tables, they can bloat your database something fierce if you have a lot of data moving in and out or deleting them....
Jun 22 '15 #5
zmbd
5,501 Expert Mod 4TB
Try replacing your having clause with this one:
Expand|Select|Wrap|Line Numbers
  1. HAVING ((Timings.Category ="questions") AND ((Workdays([Activity_Received],[Activity_End])>=6));
Jun 22 '15 #6
generates the same issues. I tested a simple function that returns the sqaure of an integer. Works in the SELECT portion of the Query, but not in the HAVING Clause.
Jun 22 '15 #7
zmbd
5,501 Expert Mod 4TB
Hmm... you've got something funky going on... this is from one my production databases:
Expand|Select|Wrap|Line Numbers
  1. HAVING (((zturnaround([timeline].[est_startdate],[timeline].[est_enddate]))<=30));
  2.  
and it pulls... well... a lot of data.

Have you tried removing the (Timings.Category ="questions") AND leaving only the function in the HAVING clause?
Jun 22 '15 #8
NeoPa
32,556 Expert Mod 16PB
Why don't you try stripping the problem down to its simplest form and posting the SQL of that, its results and why they aren't correct in here for us to consider.

Wading through mountains of SQL which is complicated by more than just volume is not a good way to work (That's actually one of the most important debugging techniques you'll ever learn as well).

Don't use GROUP BY unless it doesn't fail unless you do. Keep it as simple as you can do it.
Jun 22 '15 #9

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

Similar topics

1
by: Phil Powell | last post by:
Here is the scope of what I need to do; want: enrollment_year allowed (even if null) all of ica criteria:
2
by: Vic Spainhower | last post by:
Hello, I am new to php and MySQL and I'm attempting to run a select query on a MySQL database which is working fine except prior to displaying the table with the results from the select query it...
1
by: Rhanda Zak | last post by:
Hi all, my question is maybe so trivial, that I even could not find it in Google's NG seach, so I annoy you now: I simply want to copy the result of a SELECT query to another table which has...
2
by: Wei Wang | last post by:
Hi, I want to do a select in dynamic command, something like: TRIGGER FUNCTION DECLARE table_name_suffix text; temp_result RECORD; temp_result2 RECORD;
2
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and...
5
by: MARIEDB2 | last post by:
Hello, I am struggling to build a query on a DB2 db wich does an update for multiple fields which are the result of a select query with where clause (using multiple tables in the condition). ...
30
by: iheartvba | last post by:
Hi, I already have 3 Databases running: A. they all have the same tables and the same structure B. There is no 1 Master table they are all separate tables What I want to do is to merge them...
1
by: cbjones | last post by:
Can I lable the output column resulting from a SELECT query with a variable from one of the tables which are the source of the data? I know I can hard code the lables in the SELECT statement but...
0
by: bala venkata siva ram kum | last post by:
hi I want autocompletetextview in android here we are using sqlite database with query using cursor. public void cal() { SQLiteDatabase db= null; String TableName = "enquiryhead"; db...
4
semanticnotion
by: semanticnotion | last post by:
Hi guys how can i retrieve the last inserted record from datebase. my primary key is not auto_increment its type is bigint because i want to insert SSN no as a primary key. when i retrive the data...
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: 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
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.