MS Access Forum / General 1 / May 2008
How to create a form based on a crosstab query? (I don't know how many columns there will be!)
|
|
Thread rating:  |
Paul H - 29 Apr 2008 09:33 GMT 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..
So if the user selects 3 months in the criteria form, there will be 3 (month) column headings, if he selects 6 months, the will be 6 headings.
At the moment my solution is to somehow (i haven't figured out the VBA yet) construct a form dynamically. Is there a simpler solution to this problem?
Thanks,
Paul
Roger - 29 Apr 2008 11:07 GMT > 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 [quoted text clipped - 13 lines] > > Paul assuming that there will be a maximum of 12 months edit the query, hilight the column heading field, I assume it is called 'months' and show its properties (right click the column) in the 'column headings' property add "month01", "month02", ..., "month12"
this will cause the query to already have 12 columns regardless of which, if any, have data
Paul H - 29 Apr 2008 11:43 GMT > > 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 [quoted text clipped - 24 lines] > > - Show quoted text - Roger,
Thank you...I should have explained in more detail:
The crosstab query actually shows the number of issues of a magazine that have been delivered to customers, by issue date, between two user selectable dates. So the crosstab query actually looks like this:
ClientID 01/01/2007 01/02/2007 01/03/2007 etc.. B&Q 55 110 110 Halfords 110 110 110
Notice that the column headings represent the issue date, also, some months the magazine may not be published at all, so unfortunately your solution will not work.
Paul
Salad - 29 Apr 2008 14:20 GMT >>>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 [quoted text clipped - 42 lines] > > Paul The following link is for a report. But I'm sure you can modify it for a continuous form. http://www.fmsinc.com/tpapers/access/Reports/monthly/index.html
Flight http://www.youtube.com/watch?v=LtnDUSJfbzU
Paul H - 29 Apr 2008 17:57 GMT > >>>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 [quoted text clipped - 49 lines] > > - Show quoted text - Salad, Thank you for your reply.
I noticed in the text it says:
"Microsoft Access reports reference field names directly. If we want to use a crosstab query as the RecordSource of a report, its column names should not change (unless we want to write a lot of code to handle that)."
Considering the fact that my users may select any number of months, am I screwed? Have I got to write a load of code to dynamically create my form?
:O( Paul
paii, Ron - 29 Apr 2008 18:43 GMT On 29 Apr, 14:20, Salad <o...@vinegar.com> wrote:
> Paul H wrote: > > On 29 Apr, 11:07, Roger <lesperan...@natpro.com> wrote: [quoted text clipped - 54 lines] > > - Show quoted text - Salad, Thank you for your reply.
I noticed in the text it says:
"Microsoft Access reports reference field names directly. If we want to use a crosstab query as the RecordSource of a report, its column names should not change (unless we want to write a lot of code to handle that)."
Considering the fact that my users may select any number of months, am I screwed? Have I got to write a load of code to dynamically create my form?
:O( Paul
Will your user select any number of months? Or will there be some limit, 12, 24, 36 ect. Access has limits on report/form width and control count. Once you define the max number of months you can use Salad's posting to help create your query and report(s).
Salad - 29 Apr 2008 21:07 GMT >>>>>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 [quoted text clipped - 58 lines] > names should not change (unless we want to write a lot of code to > handle that)." Ron's reponse was spot on. You REALLY need to study the article to understand it. It is an excellent article on displaying crosstab data.
> Considering the fact that my users may select any number of months, am > I screwed? Have I got to write a load of code to dynamically create my [quoted text clipped - 3 lines] > > Paul If you want to give your users real flexibility I suppose you can open up a form in design mode and write a bunch of code to create the form's controls. But in crosstabs, afaic, that's nuts. Give your users an acceptable time frame; 12 months; 24 months, whatever. Then work within your format.
Hanging Out http://www.youtube.com/watch?v=hiFOvrDAxxw
Bob Quintal - 01 May 2008 00:10 GMT Paul H <google@comcraft.freeserve.co.uk> wrote in news:94ede0d0-61eb-459d-b875- 50d7b05ff13b@a1g2000hsb.googlegroups.com
> Considering the fact that my users may select any number of > months, am I screwed? Have I got to write a load of code to [quoted text clipped - 3 lines] > > Paul Sometimes you have to compromise. Instead of allowing them to choose a start date and end date, have them choose a start date and a number of following months, say 3, 6 or 12.
By limiting the number of choices to a reasonable set of options, you have mead it easier for them to decide what report to generate, and easier for you to give them those reports.
The next step is to code the periods in the query to a number, so say you've decided they will get 12 month reports, you set the first month to 0, the second to 1, and so on to 11.
So your query contains the following columns: RptStartDate, ClientId, datediff("m",rptStartDate,[Issue Date]) as monthOffset, Count(delivery) as NumDeliveries.
In the report, you can set the column header labels to a Date using dateadd("m",monthoffset, rptStartDate)
The report will look and feel like the dates were the actual column headers.
There is a way to have columns appear in a crosstab even if there is no data so that you just get a blank column if the magazine is only published bi-monthly.
 Signature Bob Quintal
PA is y I've altered my email address. ** Posted from http://www.teranews.com **
lyle fairfield - 29 Apr 2008 21:26 GMT Constructing a form dynamically is a challenging but not impossible task. It has three upsides: 1. you learn a lot about forms; 2. the code is largely re-usable not just for dynamic forms, but for dynamic reports as well; 3. you're in control.
I'm looking for some code to create a form but can't put my finger on any right now, although I'm pretty sure I have some. Here's some basic code for creating a dynamic report and the method is largely the same:
Const ReportName As String = "rptSchoolOrganizations" Const ReportCaption As String = "School Organizations (right click for menu)" Const Spacing As Long = 57 Const StartField As Long = 6 Const Width As Long = 567
Private ClassStructure As ADODB.Recordset Private OrganizationType As String
Public Sub ViewSchoolOrganizationsReportofActualClasses() Dim Cancel As Integer VerifyLogin Cancel If Cancel = 1 Then Exit Sub OrganizationType = "Actual" ShowStatusMessage "Updating Actual Grade Split-Grade Designations" UpDateGrade_SplitGradeDesignations "dbo.tblActualClasses" ViewSchoolOrganizationsReport End Sub
Public Sub ViewSchoolOrganizationsReportofTheoreticalClasses() Dim Cancel As Integer VerifyLogin Cancel If Cancel = 1 Then Exit Sub OrganizationType = "Theoretical" ShowStatusMessage "Updating Theoretical Grade Split-Grade Designations" UpDateGrade_SplitGradeDesignations "dbo.tblTheoreticalClasses" ViewSchoolOrganizationsReport End Sub
Sub ViewSchoolOrganizationsReport() Dim AnyReport As Report Dim AvRecordSource() As String Dim Control As Control Dim FieldName As String Dim Height As Long Dim Label As Label Dim LastPosition As Long Dim Left As Long Dim Line As Line Dim NewReportName As String Dim NextTop As Long Dim Position As Long Dim SQL As String Dim vRecordSource As String Dim Report As Report Dim TextBox As TextBox Dim TSQL As String Dim ReportWidth As Long Dim z As Long
' ------------------------- ' if the report is open ' ask that it be closed ' and exit ' ------------------------- If SysCmd(acSysCmdGetObjectState, acReport, ReportName) <> 0 Then MsgBox "Please, close all reports and try again.", vbExclamation Or vbOKOnly, "FFDBA" GoTo ViewSchoolOrganizationsReportExit End If
' ------------------------- ' delete the report, is if exists ' ------------------------- On Error Resume Next DoCmd.DeleteObject acReport, ReportName On Error GoTo 0 ' ------------------------- ' set error handling ' ------------------------- ' On Error GoTo ViewSchoolOrganizationsReportErr
' ------------------------- ' stop screen updating ' ------------------------- Application.Echo 0
' ------------------------- ' get class structure ' ------------------------- GetClassStructure
' ------------------------- ' create the report ' ------------------------- ShowStatusMessage "Creating School Organization Report"
Set Report = Application.CreateReport With Report .Caption = "School Organizations" End With
' ------------------------- ' create groupings ' ------------------------- Application.CreateGroupLevel Report.Name, "fldSchoolName", True, True Application.CreateGroupLevel Report.Name, "fldProgramName", True, True With Report.GroupLevel(0) .KeepTogether = 1 End With With Report.GroupLevel(1) .KeepTogether = 1 End With
' ------------------------- ' create the textbox fields that access the data in the table ' ------------------------- With ClassStructure For z = StartField To .Fields.Count - 1 FieldName = .Fields(z).Name Set TextBox = Application.CreateReportControl(Report.Name, acTextBox, acDetail, , FieldName) With TextBox .Format = "#0;=#0;" & Chr$(34) & Chr$(34) .Left = Left .Name = "txt" & FieldName .TextAlign = 3 .Width = Width End With Left = Left + Width Next z End With
' ------------------------- ' set some report dimensions ' ------------------------- ReportWidth = Left Report.Width = ReportWidth Report.Section(acDetail).Height = TextBox.Height
' ------------------------- ' design the page header ' ------------------------- Set Line = Application.CreateReportControl( _ Report.Name, acLine, acPageHeader, , , 0, _ Spacing, ReportWidth) With Line .BorderWidth = 0 .Name = "Line1" NextTop = .Top + .Height + Spacing End With
Set Label = Application.CreateReportControl( _ Report.Name, acLabel, acPageHeader, , , 0, NextTop) With Label .BorderStyle = 0 .Caption = "Halton District School Board" .Name = "lblDistrictName" .SizeToFit .Width = ReportWidth NextTop = .Top + .Height + Spacing End With
Set Label = Application.CreateReportControl( _ Report.Name, acLabel, acPageHeader, , , 0, NextTop) With Label .BorderStyle = 0 .Caption = "School Organizations" .Name = "lblSchoolOrganizations" .SizeToFit .Width = ReportWidth NextTop = .Top + .Height + Spacing End With
Set Line = Application.CreateReportControl( _ Report.Name, acLine, acPageHeader, , , 0, _ NextTop, ReportWidth) With Line .BorderWidth = 0 .Name = "Line2" NextTop = .Top + .Height + Spacing End With
Report.Section(acPageHeader).Height = NextTop
' ------------------------- ' design the school grouping header ' ------------------------- Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel1Header, , , 0, _ Spacing, ReportWidth) With Line .BorderWidth = 3 .Name = "Line3" NextTop = .Top + .Height + Spacing End With
Set TextBox = Application.CreateReportControl( _ Report.Name, acTextBox, acGroupLevel1Header, , , 0, NextTop) With TextBox .BorderStyle = 0 .ControlSource = "fldSchoolName" .Name = "txtSchoolName" .SizeToFit .Width = ReportWidth NextTop = .Top + .Height + Spacing * 2 End With
Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel1Header, , , 0, _ NextTop, ReportWidth) With Line .BorderWidth = 3 .Name = "Line4" NextTop = .Top + .Height + Spacing End With
Report.Section(acGroupLevel1Header).Height = NextTop
' ------------------------- ' design the school grouping footer ' ------------------------- Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel1Footer, , , 0, _ Spacing, ReportWidth) With Line .BorderWidth = 3 .Name = "Line5" NextTop = .Top + .Height + Spacing End With
Set TextBox = Application.CreateReportControl( _ Report.Name, acTextBox, acGroupLevel1Footer, , , 0, NextTop) With TextBox .BorderStyle = 0 .ControlSource = "= fldSchoolName & " & Chr(34) & " Totals" & Chr(34) .Name = "txtSumSchool" .SizeToFit .Width = ReportWidth NextTop = .Top + .Height + Spacing End With
Left = 0
With ClassStructure For z = StartField To .Fields.Count - 1 FieldName = .Fields(z).Name Set TextBox = Application.CreateReportControl(Report.Name, acTextBox, acGroupLevel1Footer) With TextBox .ControlSource = "=Sum(" & FieldName & ")" .Left = Left .Name = "txt" & FieldName & "SumSchool" .TextAlign = 3 .Top = NextTop .Width = Width Height = .Top + .Height End With Left = Left + Width Next z End With
NextTop = Height + Spacing
Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel1Footer, , , 0, _ NextTop, ReportWidth) With Line .BorderWidth = 3 .Name = "Line6" NextTop = .Top + .Height + Spacing End With
Report.Section(acGroupLevel1Footer).Height = NextTop
' ------------------------- ' design the program grouping header ' ------------------------- Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel2Header, , , 0, _ Spacing, ReportWidth) With Line .BorderWidth = 0 .Name = "Line7" NextTop = .Top + .Height + Spacing End With
Set TextBox = Application.CreateReportControl( _ Report.Name, acTextBox, acGroupLevel2Header, , , 0, NextTop) With TextBox .BorderStyle = 0 .ControlSource = "fldProgramName" .Name = "txtProgramName" .SizeToFit .Width = ReportWidth NextTop = .Top + .Height + Spacing End With
Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel2Header, , , 0, _ NextTop, ReportWidth) With Line .BorderWidth = 0 .Name = "Line8" NextTop = .Top + .Height + Spacing End With
Left = 0 With ClassStructure For z = StartField To .Fields.Count - 1 FieldName = .Fields(z).Name Set Label = Application.CreateReportControl(Report.Name, acLabel, acGroupLevel2Header, , Replace(FieldName, "fld", "")) With Label .Left = Left .Name = "lbl" & .Caption .TextAlign = 3 .Top = NextTop .Width = Width Left = Left + Width End With Next z End With
Report.Section(acGroupLevel2Header).Height = Height
' ------------------------- ' design the program grouping footer ' ------------------------- Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel2Footer, , , 0, _ Spacing, ReportWidth) With Line .BorderWidth = 0 .Name = "Line9" NextTop = .Top + .Height + Spacing End With
Set TextBox = Application.CreateReportControl( _ Report.Name, acTextBox, acGroupLevel2Footer, , , 0, NextTop) With TextBox .BorderStyle = 0 .ControlSource = "= fldProgramName & " & Chr(34) & " Totals" & Chr(34) .Name = "txtSumProgram" .SizeToFit .Width = ReportWidth NextTop = .Top + .Height + Spacing End With
Left = 0
With ClassStructure For z = StartField To .Fields.Count - 1 FieldName = .Fields(z).Name Set TextBox = Application.CreateReportControl(Report.Name, acTextBox, acGroupLevel2Footer) With TextBox .ControlSource = "=Sum(" & FieldName & ")" .Left = Left .Name = "txt" & FieldName & "SumProgram" .TextAlign = 3 .Top = NextTop .Width = Width Height = .Top + .Height End With Left = Left + Width Next z End With
NextTop = Height + Spacing
Set Line = Application.CreateReportControl( _ Report.Name, acLine, acGroupLevel2Footer, , , 0, _ NextTop, ReportWidth) With Line .BorderWidth = 0 .Name = "Line10" NextTop = .Top + .Height + Spacing End With
Report.Section(acGroupLevel2Footer).Height = NextTop
' ------------------------- ' set page footer grouping properties ' ------------------------- Set TextBox = Application.CreateReportControl(Report.Name, acTextBox, acPageFooter, , , 0, Spacing * 2) With TextBox .ControlSource = "= " & Chr$(34) & "Page " & Chr$(34) & " & [Page] & " & Chr$(34) & " of " & Chr$(34) & " & [Pages]" .Name = "txtPage" .TextAlign = 1 .Width = ReportWidth NextTop = .Top + .Height + Spacing End With Set TextBox = Application.CreateReportControl(Report.Name, acTextBox, acPageFooter, , , 0, NextTop + Spacing * 2) With TextBox .ControlSource = "= Now()" .Format = "YYYY-MM-DD HH:NN" .Name = "txtNow" .TextAlign = 1 .Width = ReportWidth NextTop = .Top + .Height + Spacing End With
' ------------------------- ' set some more report properties ' ------------------------- With Report .Section(acPageFooter).Height = NextTop .Caption = ReportCaption .ShortcutMenuBar = "Report Preview" .Width = ReportWidth
' ------------------------- ' get and assign vRecordSource sql ' ------------------------- vRecordSource = GetSQLStringFromSP("SpSchoolOrganizationReport") AvRecordSource = Split(vRecordSource, "ELSE") If OrganizationType = "Actual" Then vRecordSource = AvRecordSource(0) vRecordSource = Trim(vRecordSource) StripString vRecordSource vRecordSource = Replace(vRecordSource, "@SchoolID", "0") Position = 0 Do LastPosition = Position Position = InStr(LastPosition + 1, vRecordSource, "ORDER BY") Loop Until Position = 0 If LastPosition > 0 Then vRecordSource = Mid$(vRecordSource, 1, LastPosition - 1) End If TSQL = "SELECT * FROM SysObjects o WHERE o.Name = 'viewActualSchoolOrganizationReport'" If CurrentProject.Connection.Execute(TSQL).BOF Then CurrentProject.Connection.Execute "CREATE VIEW dbo.viewActualSchoolOrganizationReport AS " & vRecordSource End If vRecordSource = "SELECT * FROM dbo.viewActualSchoolOrganizationReport" Else vRecordSource = AvRecordSource(1) vRecordSource = Trim(vRecordSource) StripString vRecordSource vRecordSource = Replace(vRecordSource, "@SchoolID", "0") Position = 0 Do LastPosition = Position Position = InStr(LastPosition + 1, vRecordSource, "ORDER BY") Loop Until Position = 0 If LastPosition > 0 Then vRecordSource = Mid$(vRecordSource, 1, LastPosition - 1) End If TSQL = "SELECT * FROM SysObjects o WHERE o.Name = 'viewTheoreticalSchoolOrganizationReport'" If CurrentProject.Connection.Execute(TSQL).BOF Then CurrentProject.Connection.Execute "CREATE VIEW dbo.viewTheoreticalSchoolOrganizationReport AS " & vRecordSource End If vRecordSource = "SELECT * FROM dbo.viewTheoreticalSchoolOrganizationReport" End If If LoginID <> 0 Then vRecordSource = vRecordSource & " WHERE SCHOOLID = " & LoginID End If vRecordSource = vRecordSource & " ORDER BY fldSchoolName , fldProgramName, fldSequence"
.RecordSource = vRecordSource
NewReportName = .Name
End With
' ------------------------- ' close and save the report ' -------------------------
DoCmd.Close acReport, Report.Name, acSaveYes
' ------------------------- ' Rename the report ' ------------------------- DoCmd.Rename ReportName, acReport, NewReportName ' ------------------------- ' View the report ' ------------------------- Application.Echo 1 ShowStatusMessage ReportCaption With DoCmd .OpenReport ReportName, acViewPreview .ShowToolbar "Print Preview", acToolbarNo .Maximize End With Reports(ReportName).ZoomControl = 0
ViewSchoolOrganizationsReportExit: ShowStatusMessage Exit Sub ViewSchoolOrganizationsReportErr: With Err If .Number <> 91 Then MsgBox .Description, vbCritical, "Error Number " & .Number End With For Each AnyReport In Reports DoCmd.Close acReport, AnyReport.Name, acSavePrompt Next AnyReport Application.Echo 1 Resume ViewSchoolOrganizationsReportExit End Sub
Private Sub GetClassStructure() If Not ClassStructure Is Nothing Then Exit Sub If OrganizationType = "Actual" Then CreateRecordset ClassStructure, "SpGetEmptyActualClassRecordet", , True ElseIf OrganizationType = "Theoretical" Then CreateRecordset ClassStructure, "SpGetEmptyTheoreticalClassRecordet", , True End If End Sub
> 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 [quoted text clipped - 13 lines] > > Paul Paul H - 01 May 2008 13:35 GMT Thanks to all who posted.
I have agreed with the client that he can view the results for a 6 month period only.
Paul
> 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 [quoted text clipped - 13 lines] > > Paul
|
|
|