Results 1 to 5 of 5
  1. #1
    asanteza is offline Novice
    Windows 10 Access 2021
    Join Date
    Apr 2024
    Posts
    1

    Crosstab Form


    I create forms from crosstab queries. How can I make the fields on the form dynamic (new fields should be added to the form when the crosstab data changes)?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    To add new controls you would need to go into form design view. But there are other ways - see similar threads links at the bottom of this thread

  3. #3
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    dont. just use the query as the result.
    no form needed, nor the effort to create all permutations.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,945
    Here is a way I did it, quite a while back though.

    Code:
    Private Sub Form_Load()
    Dim n As Integer
    Dim fld As Variant
      
    For Each fld In Me.Recordset.Fields
        Do
            With Me.Controls(n)
                If .ControlType = acTextBox And Left(.Name, 5) = "vName" Then
                    .ControlSource = fld.Name
                    .Controls(0).Caption = fld.Name
                     n = n + 1
                     Exit Do
                  End If
               End With
               n = n + 1
          Loop
     Next
    End Sub
    
    
    Public Sub Form_Open(Cancel As Integer)
    Dim strCaller As String, strSql As String, strQuery As String
    
    
    strCaller = PersonList()
    
    
    strQuery = "qryWeekCallSummary_Crosstab"
    
    
    'strSQL = "PARAMETERS pCallSystem Text ( 255 );"
    strSql = strSql & " TRANSFORM Sum([SumOfCallSecs]/86400) AS Duration"
    strSql = strSql & " SELECT qryWeekCallSummary.WeekEndDate"
    strSql = strSql & " FROM qryWeekCallSummary "
    If Len(gCallSystem) > 0 Then
        strSql = strSql & " WHERE qryWeekCallSummary.CallSystem = '" & gCallSystem & "'"
    End If
    strSql = strSql & " GROUP BY qryWeekCallSummary.WeekEndDate"
    strSql = strSql & " ORDER BY qryWeekCallSummary.WeekEndDate DESC"
    strSql = strSql & " PIVOT qryWeekCallSummary.Caller IN (" & strCaller & ")"
    
    
    'CurrentDb.QueryDefs(strQuery).Parameters("[pCallSystem]") = gCallSystem
    CurrentDb.QueryDefs(strQuery).SQL = strSql
    Me.RecordSource = strSql
    End Sub
    Basically create enough controls for the columns, then populate as needed.

    Code:
    Public Function PersonList() As String
        Dim lngCount  As Long
        Dim strSql    As String
        Dim strPeople As String
        giMaxPeople = 10
        
        
        strSql = " SELECT [CallerName]" & _
                 " FROM tblCaller" & _
                 " WHERE [Required] = True" & _
                 " ORDER BY [CallerName]"
                 
        With CurrentDb.OpenRecordset(strSql)
            Do Until .EOF
                lngCount = lngCount + 1
                If lngCount > giMaxPeople Then Exit Do
            
                strPeople = strPeople & Chr(34) & !CallerName & Chr(34) & ","
                .MoveNext
            Loop
        End With
        
        If Right(strPeople, 1) = "," Then
            PersonList = Left(strPeople, Len(strPeople) - 1)
        End If
        
    End Function
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    As ranman noted, can just set subform container SourceObject to the query, no code needed.

    But why do you want a form with crosstab display? Too bad MS removed pivot table/form from Access.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Form with crosstab query
    By Rpschwar in forum Forms
    Replies: 4
    Last Post: 10-20-2017, 07:20 AM
  2. Crosstab Form or Lots of Work?
    By dynamictiger in forum Forms
    Replies: 2
    Last Post: 09-24-2016, 08:37 PM
  3. Replies: 2
    Last Post: 05-02-2014, 03:09 PM
  4. Form's Criteria used in Crosstab
    By onechriswhite in forum Queries
    Replies: 8
    Last Post: 08-27-2010, 05:48 PM
  5. Crosstab and Export Form example
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-24-2010, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums