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

Prints Months Name in Brackets with Comma

102 64KB
Hello Friends,
One more problem to solve and I need your help. I have a crosstab query is below that runs and give me my results.
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(qBG.SumOfBG) AS SumOfSumOfBG
  2. SELECT qBG.Company, qBG.Region, qBG.Territory, Sum(qBG.SumOfBG) AS [Total Of SumOfBG]
  3. FROM qBG
  4. GROUP BY qBG.Company, qBG.Region, qBG.Territory
  5. PIVOT qBG.HeldOn In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
query runs and give me accurate results. But I do not want full months. I want only that months which contain data in brackets like (Jan,Feb,Mar) etc. Plz see picture attached.

Attached Images
File Type: jpg MyRequirement.jpg (35.4 KB, 503 views)
Aug 28 '19 #1
9 1776
twinnyfo
3,653 Expert Mod 2GB
Irsmalik,

Based upon your image, are you saying you only want the totals from those [articular months? Or, are you saying that the totals for those records is based upon values that happen to fall in those particular months, and you are annotating which months those values are from?

I’m not sure if my question made sense, but what I am getting at is, for the first record listed, what is the source of “Jan, Feb, Apr”? Because this is not a Calendar “quarter”, but rather a list of months, in order, but missing one month.

Thanks.

Hope we can hepp!
Aug 28 '19 #2
irsmalik
102 64KB
Thank you sir
Actually when I run query, it gives me all the months name (Jan ~ Dec) so it is long size to print in a report. Further, my boss required only the month which contain the data. Suppose if the data is in Jan, Feb & Apr... I want to print it in Bracket like this (Jan, Feb, Apr)... that same for other regions.

It is not quarterly base... it is just a query for all year. Query picture is attached.... but my requirement is the previous image.
Thanks
irsmalik

Attached Images
File Type: jpg Query.jpg (137.7 KB, 496 views)
Aug 28 '19 #3
twinnyfo
3,653 Expert Mod 2GB
irsmalik,

My initial thought is that you will need some form of either a subquery or a User-Defined function that returns just the months which apply to each record. Then, you no longer need a CrossTab Query, but can simply use an Aggregate Query (for the Total), and one of your Fields will be your function:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     qBG.Company, 
  3.     qBG.Region, 
  4.     qBG.Territory, 
  5.     Sum(qBG.SumOfBG) AS [Total Of SumOfBG], 
  6.     GetMonths(qBG.Company, qBG.Region, qBG.Territory) as Months 
  7. FROM 
  8.     qBG 
  9. GROUP BY 
  10.     qBG.Company, 
  11.     qBG.Region, 
  12.     qBG.Territory;
This code assumes you have a User-Defined Function GetMonths(), which receives three arguments and returns a String value of the months which apply.

Since I don't know any more about your data, going beyond this general recommendation is impossible.

Knowing more, we might could move forward.

Hope this hepps point you in a better direction.
Aug 28 '19 #4
Rabbit
12,516 Expert Mod 8TB
Alternatively, if you want to avoid VBA, use the crosstab as the source for another query and derive a field that concatenates 12 Nz() functions.
Aug 28 '19 #5
irsmalik
102 64KB
Dear Rabbit

What is concatenates 12 Nz() functions. ? I have no idea about this... can you please explain. ?
thanks
irsmalik
Aug 29 '19 #6
NeoPa
32,556 Expert Mod 16PB
That means you take each month column from the CrossTab Query and use it to create a Field which includes all the twelve columns - BUT handles those columns having no data in such a way that the resulting data makes sense.
Sep 4 '19 #7
NeoPa
32,556 Expert Mod 16PB
To give more help on Twinny's suggestion you may find Combining Rows-Opposite of Union helpful.
Sep 4 '19 #8
cactusdata
214 Expert 128KB
You could create a query that lists the months:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     qBG.Company, qBG.Region, qBG.Territory, qBG.HeldOn
  3. FROM 
  4.     qBG
  5. GROUP BY 
  6.     qBG.Company, qBG.Region, qBG.Territory, qBG.HeldOn
Save this as qBGmonths.

Then use my DJoin function found in my article here:
Link removed
and here:
VBA.DJoin

and create a query to list the joined months:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     qBG.Company, qBG.Region, qBG.Territory, Sum(qBG.SumOfBG) AS [BG], 
  3.     DJoin("[HeldOn]", "[qBGmonth]", "[Company]=" & [Company] & " And [Region]=" & [Region] & " And [Territory]=" & [Territory] & "", ", ") As Months
  4. FROM 
  5.     qBG
  6. GROUP BY 
  7.     qBG.Company, qBG.Region, qBG.Territory
Of course, if a group field holds text, apply quotes in the filter string.

Disclaimer: Air code
Sep 27 '19 #9
ADezii
8,834 Expert 8TB
  1. I sort of took a hybrid approach for generating the Months with Data.
  2. I used qGB_Crosstab (simulated CrossTab) as the source for another Query which I named qGB_Crosstab_2. qGB_Crosstab_2 displays the [Company], [Region], [Territory], and [Total] Fields from qGB_Crosstab as well as a Calculated Field name [Period].
  3. SQL for qGB_Crosstab:
    Expand|Select|Wrap|Line Numbers
    1. SELECT qBG_Crosstab.Company, qBG_Crosstab.Region, qBG_Crosstab.Territory, qBG_Crosstab.Total, 
    2. fCalcMonths([Region],[Territory]) AS Period FROM qBG_Crosstab;
  4. The [Period] Field passes the [Region] and [Territory] Fields to a Public Function named fCalcMonths(). Within this Function, a Recordset is created based on the Unique Combination of [Region] and [Territory]. For each Record, Fields 4 thru 15 (Jan thru Dec) are checked for NULL Values. If a Field contains Data, then the Field Name is concatenated to a Build String.
  5. Query OUTPUT (Sample Data):
    Expand|Select|Wrap|Line Numbers
    1. Company    Region    Territory    Total    Period
    2. GREENLET    A        North          5      Feb, Jul
    3. GREENLET    B        South          3      Jan, Feb, Apr
    4. GREENLET    C        East           7      Mar, Jun, Aug, Nov
    5. GREENLET    D        West           8      Jan, May, Sep, Oct, Dec
  6. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcMonths(strRegion As String, strTerritory As String) As String
    2. Dim MyDB As DAO.Database
    3. Dim rst As DAO.Recordset
    4. Dim strSQL As String
    5. Dim intFld As Integer
    6. Dim strBuild As String
    7.  
    8. strSQL = "SELECT * FROM qBG_Crosstab WHERE [Region] = '" & strRegion & _
    9.          "' AND [Territory] = '" & strTerritory & "'"
    10.  
    11. Set MyDB = CurrentDb
    12. Set rst = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
    13.  
    14. For intFld = 4 To 15    'Jan to Dec inclusive
    15.   If Not IsNull(rst.Fields(intFld)) Then
    16.     strBuild = strBuild & rst.Fields(intFld).Name & ", "
    17.   End If
    18. Next
    19.  
    20. fCalcMonths = Left$(strBuild, Len(strBuild) - 2)    'Remove Trailing ', '
    21.  
    22. rst.Close
    23. Set rst = Nothing
    24. End Function
  7. Finally, fCalcMonths() returns this Concatenated String to the [Period] Field in the Query. Each Field ends with a Comma and Space which are stripped prior to returning the completed String.
  8. Realizing that this all sounds very confusing, I uploaded the Demo for your Review.
Attached Files
File Type: zip Print Months.zip (25.2 KB, 42 views)
Sep 29 '19 #10

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

Similar topics

5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
2
by: Pmb | last post by:
I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to...
3
by: SteelDetailer | last post by:
Thnaks in advance for considering this post. It's probably very simple, but..... I have an old VB6 application that allows me to create, save and edit a "project information file" that is a...
6
by: Edwin Knoppert | last post by:
I can't find a way to obtain the months name from a date. Like 'August'
19
by: Kirk Strauser | last post by:
Given a class: how can I find its name, such as: 'foo' I'm writing a trace() decorator for the sake of practice, and am trying to print the name of the class that a traced method belongs...
4
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Why does System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames return 13 elements? Why does it not use the cuurent culture?
7
by: Kandiman | last post by:
Hello, Im hoping you can help, for now im trying to learn how to do something so an easy example may help me to gain the concept. I wish to create a hyperlink that when i click it, it prints my...
5
by: Jim in Arizona | last post by:
I built a webpage using vb.net (.net 2.0) that creates a form letter. This letter pulls data from a database. Although I populate the address with the person's full name, which comes from the name...
1
by: Kasu | last post by:
function month_name($monthNumber) { $months = array( 1 =>'January', 2 =>'February', 3 =>'March', 4 =>'April', 5 =>'May', 6 =>'June', 7 =>'July',
1
by: Biplab Tikader | last post by:
#ifndef _ASA_USER_ASA_H_ #define _ASA_USER_ASA_H_ #ifdef __cplusplus extern "C" { #endif /*********************************************************************** * Adaptive Simulated...
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...
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
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
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...

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.