473,395 Members | 2,006 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.

CrossTab Dates Query With blanks

365 100+
Hello

i have a holiday table with staff ID's and single entry dates (for holidays skipping weekends)

I need a crosstab to show the dates as the column, and the staff id as the row, and count the holidays respective to each person, so it will show a "1" or be empty, i want to be able to specify the range that the dates show (ie month by month) but i can only get it to header for dates where there is a holiday if that makes sense.

i have a table of dates which builds "from" and "to" to supply the headers

please help

Crosstab SQL so far

TRANSFORM Count(tbl_Holidays.Date) AS CountOfDate
SELECT tbl_Holidays.StaffID
FROM tbl_MDCodes, tbl_Holidays
WHERE (((tbl_Holidays.Date)=CDate([DCode] & "/" & [MCode] & "/" & [YCode])))
GROUP BY tbl_Holidays.StaffID
PIVOT CDate([DCode] & "/" & [MCode] & "/" & [YCode]);
Nov 25 '07 #1
2 1956
Jim Doherty
897 Expert 512MB
Hello

i have a holiday table with staff ID's and single entry dates (for holidays skipping weekends)

I need a crosstab to show the dates as the column, and the staff id as the row, and count the holidays respective to each person, so it will show a "1" or be empty, i want to be able to specify the range that the dates show (ie month by month) but i can only get it to header for dates where there is a holiday if that makes sense.

i have a table of dates which builds "from" and "to" to supply the headers

please help

Crosstab SQL so far

TRANSFORM Count(tbl_Holidays.Date) AS CountOfDate
SELECT tbl_Holidays.StaffID
FROM tbl_MDCodes, tbl_Holidays
WHERE (((tbl_Holidays.Date)=CDate([DCode] & "/" & [MCode] & "/" & [YCode])))
GROUP BY tbl_Holidays.StaffID
PIVOT CDate([DCode] & "/" & [MCode] & "/" & [YCode]);

From my understanding of what you want ...you want a query that will show as a column header a 'date' irrespective of whether there is any staff on holiday on that particular day and in which case the value should show null or where there is a holiday date assigned to a particular staff ID during the period to show itself as a value of 'one' for that date. All of this to be done as a crosstab.

you realise of course that to show a separate columns of dates like this you are likely to run out of columns if your expectation for instance was to display an entire year. My guess is though, you don't need a full year but something that displays a total range period inclusive of all dates including those where no one is on holiday.

OK well having played with this heres one method (
I don't know what you have specifically there with the tbl_MDCodes table but I can sense the logic:) however I'm going to suggest a slightly different method.

I have created a function for you to look at and play with that takes a DateFrom parameter and a DateTo parameter It then builds a table called tblMyDates and inserts into it all dates for that period. This table is then used in a crosstab query that is also created in which a comparison is made using your tbl_Holidays table.

All dates in the created table tblMyDates are displayed as columns along with the StaffID and the StaffID is used to count thus returning a value for the crosstab.

In order to display all dates as columns that have no comparison value against holidays it is necessary to raise a 'placeholder' value for the matrix display which you will see when you run the crosstab query (if this placeholder row doesn't give you any undue concern merely ignore that row. It really is only there in order to ensure all dates get displayed)

Now the method you can use to run this function is to place two textboxes on a form call them txtDateFrom and txtDateTo and also place a command button on the form. Then in the command buttons 'On Click' property simply type this

Expand|Select|Wrap|Line Numbers
  1. =HolidayPeriod([txtDateFrom],[txtDateTo])
The function tests to ensure you have a valid date range by calculating the difference between the two dates and rejecting anything less than zero or greater than 254 (remember column limitation)

Paste the following function into a module and give it a go. Its not the only way to do this by the way but it gives you a flavour or what you could do. If you don't like the place holder row you could always create a table to store the results and delete the placeholder row its up to you

Hope this helps

Regards Jim :)

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function HolidayPeriod(DateFrom As Date, DateTo As Date)
  3. On Error Resume Next
  4. 'I'll leave you to do your own error handling
  5.  
  6.     Dim db As DAO.Database
  7.     Dim tbl As DAO.TableDef
  8.     Dim qry As DAO.QueryDef
  9.     Dim mysql As String, myins As String, intdays As Long
  10.     Set db = CurrentDb
  11.  
  12.     'calculate the difference in days for the period
  13.     intdays = DateDiff("d", DateFrom, DateTo)
  14.     'if invalid ie:less than zero or greater than total columns
  15.     'that can be created in one xtab recordset then exit out
  16.     If intdays < 0 Or intdays > 254 Then
  17.         msg = "You must supply a valid date range and it must not exceed 254 days"
  18.         MsgBox msg, vbExclamation, "System Message"
  19.         Exit Function
  20.     End If
  21.  
  22.     'lets get rid of any pre-existing table
  23.     'this can be amended to delete from table and leaving the
  24.     'once its on your system
  25.     'turn of system messages and raise the hourglass
  26.     DoCmd.SetWarnings vbFalse
  27.     DoCmd.Hourglass vbTrue
  28.     DoCmd.DeleteObject acTable, "tblMyDates"
  29.  
  30.     'create the tbldates table
  31.     Set tbl = CurrentDb.CreateTableDef("tblMyDates")
  32.     With tbl
  33.     .Fields.Append .CreateField("MyDates", dbDate)
  34.     End With
  35.     CurrentDb.TableDefs.Append tbl
  36.     'and throw into it a sequence of dates commensurate with the period
  37.  
  38.     For i = 0 To intdays
  39.         myins = "INSERT INTO tblMyDates ( MyDates )" _
  40.                 & "SELECT #" & Format(DateAdd("d", i, DateFrom), "mm/dd/yyyy") & "# AS MyDates;"
  41.         DoCmd.RunSQL myins
  42.     Next i
  43.  
  44.     'define the SQL for the relevant crosstab query
  45.     mysql = "TRANSFORM Count(tbl_Holidays.StaffID) AS CountOfStaffID" _
  46.             & " SELECT nz([StaffID],'Placeholder') AS Staff_ID" _
  47.             & " FROM tblMyDates LEFT JOIN tbl_Holidays ON tblMyDates.MyDates = tbl_Holidays.Date" _
  48.             & " GROUP BY nz([StaffID],'Placeholder')" _
  49.             & " ORDER BY nz([StaffID],'Placeholder')" _
  50.             & " PIVOT tblMyDates.MyDates;"
  51.  
  52.     'and then create the query!! you can comment out the next line but one once its been
  53.     'created on your system. if you do remember to comment out the line following it
  54.     DoCmd.DeleteObject acQuery, "qryHolidayDates"
  55.     Set qry = db.CreateQueryDef("qryHolidayDates", mysql)
  56.     DoCmd.OpenQuery "qryHolidayDates", acNormal, acReadOnly
  57.  
  58.     'turn system messages back on drop the hourglass and refresh the database window
  59.     DoCmd.SetWarnings vbTrue
  60.     DoCmd.Hourglass vbFalse
  61.     RefreshDatabaseWindow
  62. End Function
  63.  
  64.  
Nov 26 '07 #2
Dan2kx
365 100+
Ok thanks for the advice, not had the chance to test it yet, the boss has dropped a load more "requirements" on me, he want the thing to output into am/pm for each date, but the biggest problem i face now is that he wants me to set up peoples shift patterns in minutes for 2 weeks (am and pm) so that if someone books off 2 weeks then it selects the correct ammounts of minutes for each am and pm per day, confusing i know,

anyways...

do you (or anyone else) know of any easy way to give odd and even weeks?

i will be storing the shift info in a big (monam, monpm, tueam, tuepm..........) with staff id and week number 1,2 so i want to select which week the day is and then return the values,

is there an easier way then listing week beginning dates next to 1,2??

thanks for ya help
Nov 26 '07 #3

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

Similar topics

8
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I...
1
by: Matthew Wells | last post by:
I have a crosstab query based on anothe query. The base query resultset has no null values in its "Quantity" column. However, when I create the new crosstab query from the base query, the records...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
2
by: carl.barrett | last post by:
Hi, I'm back with the same question as I still can't get it to display my data the way I want it to. The table lists information about a perpetrator involved with an anti social behaviour...
14
by: Tina | last post by:
My employer tracks productivity/performance of clinicians (how much they bill) each week, its averages for the month, and the 6 months. These averages are compared to their expected productivity....
3
by: pkj7461 | last post by:
Hi, I have a crosstab query that always displays last 8 days worth of data(Date(),Date()-1,Date()-3, etc)). Some of these columns are blanks and MS Access, by default, returns only those columns...
8
by: Paul H | last post by:
I want to base a form on a crosstab query. The query shows statistics for a user defined period. The column headings will look something like this: ClientID Month01 Month02 Month03 etc.. ...
2
by: Jim Devenish | last post by:
I wish to create a crosstab query as the record source for a report. It needs to count data between selected dates which are entered by the user in a popup window. The following Select query...
14
ollyb303
by: ollyb303 | last post by:
Hi, I am trying to create a dynamic crosstab report which will display number of calls handled (I work for a call centre) per day grouped by supervisor. I have one crosstab query (Query1) which...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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.