Programmierung einer PivotTable in Access VBA

Lassen Sie mich beginnen, indem Sie die Dinge in Zusammenhang ersten.
Ich habe drei Fragen, und hoffte, zu erhalten etwas Hilfe auf ein problem, das ich angetroffen habe.
Ich entwickle eine Anwendung, in denen gefährdungsbeurteilungen durchgeführt werden, die auf Substanzen, die in spezifischen Arbeitsplatz/Tätigkeit-Kombinationen. Was ich tun wollte, war, erstellen Sie eine matrix wie folgt berichten:

[Arbeitsplatz Name] (als Spaltenüberschrift)

[Name der Aktivität] (als überschrift)

[Risk Assessment Score] (als Daten)

Alle diese Werte können abgerufen werden über eine Abfrage. Ich habe versucht, mit einer Kreuztabellenabfrage aber es scheint, Sie können nur verwendet werden, für die Zusammenfassung der Daten. Ich war woundering, Wenn meine Annahme richtig ist, denn das ist, was ich erst nach suchen auf der microsoft-website. Aber da bin ich kein erfahrener Acces Programmierer, ich könnte falsch sein.

Basiert auf meiner Annahme, ich dachte mir, ein PivotTable war der Weg zu gehen. Ich fand ein Beispiel, http://msdn.microsoft.com/en-us/library/aa662945(Büro.11).aspx
das habe ich versucht, zu replizieren. Der Grund für die Programmierung ist so, dass ein PivotTable erzeugt werden können, basierend auf der Benutzer-Kriterien in einem Formular.

Den code unten ist eine leichte variation auf das Beispiel-code in dem oben genannten link.

Private Sub cmdPivotTable_Click()

Dim strDefaultName As String
Dim strRecordSource As String

'Create an empty form with a PivotTable
'default view and a record source
strRecordSource = "SELECT *" _
& " FROM ((OEL RIGHT JOIN Substance ON OEL.OEL_ID = Substance.OEL_ID) INNER JOIN" _
& " ((Product INNER JOIN Proportions ON (Product.Product_Name = Proportions.Product_Name) AND (Product.Product_ID = Proportions.Product_ID) AND (Product.Product_Name = Proportions.Product_Name) AND (Product.Product_ID = Proportions.Product_ID)) INNER JOIN (STM_Workplace INNER JOIN (Respiratory_PPE INNER JOIN (STM_LocalControls INNER JOIN (STM_Diffuse INNER JOIN ((STM_Vent_NF_FF INNER JOIN Workplace ON (STM_Vent_NF_FF.STM_Vent_FF_ID = Workplace.STM_Vent_NF_ID) AND (STM_Vent_NF_FF.STM_Vent_FF_ID = Workplace.STM_Vent_NF_ID) AND (STM_Vent_NF_FF.STM_Vent_FF_ID = Workplace.STM_Vent_FF_ID)) INNER JOIN (STM_Activity_Score INNER JOIN (Activity INNER JOIN (PBMCode INNER JOIN Product_Activity ON PBMCode.ID_PBMCode = Product_Activity.ID_PBMCode) " _
& " ON Activity.Activity_ID = Product_Activity.ID_Activity) ON STM_Activity_Score.STM_ActivityScore_ID = Activity.STM_ActivityScore_ID) ON (Workplace.Workplace_ID = Product_Activity.Workplace_ID) AND (Workplace.Workplace_ID = Product_Activity.Workplace_ID)) ON STM_Diffuse.STM_Diffuse_ID = Workplace.STM_Diffuse_ID) ON STM_LocalControls.STM_LocalControls_ID = Workplace.STM_LocalControls_ID) ON (Respiratory_PPE.STM_PPE_ID = PBMCode.STM_PPE_ID) AND (Respiratory_PPE.STM_PPE_ID = PBMCode.STM_PPE_ID)) ON STM_Workplace.STM_Workplace_ID = Workplace.STM_Workplace_ID) ON (Product.Product_Name = Product_Activity.Product_Name) AND (Product.Product_ID = Product_Activity.Product_ID) " _
& " AND (Product.Product_Name = Product_Activity.Product_Name) AND (Product.Product_ID = Product_Activity.Product_ID)) ON (Substance.Substance_Name = Proportions.Substance_Name) AND (Substance.Substance_ID = Proportions.Substance_ID)) INNER JOIN Risk_Assessment ON (Substance.Substance_Name = Risk_Assessment.Substance_Name) AND (Substance.Substance_ID = Risk_Assessment.Substance_ID) " _
& " WHERE Product_Activity.ID_Product_Task = Product_Activity_ID"

strDefaultName = CreatePivotTable(strRecordSource)

'Rename the form from its default name
Dim strFormName As String
strFormName = "TestCreatePivotTableForm"
If (AssignPivotTableName(strDefaultName, strFormName)) = False Then
    Exit Sub
End If

'Configure the PivotTable
ConfigurePivotTable (strFormName)
End Sub



Public Function AssignPivotTableName(strDefaultName As String, strFormName As String As Boolean
Dim acc1 As AccessObject

AssignPivotTableName = True

For Each acc1 In CurrentProject.AllForms
    If acc1.Name = strFormName Then
        MsgBox "Choose a form name other " & _
            "than '" & strFormName & "' that " & _
            "does not match an existing form."
        AssignPivotTableName = False
        DoCmd.DeleteObject acForm, strDefaultName
        Exit Function
    End If
Next acc1

DoCmd.Rename strFormName, acForm, strDefaultName
End Function



Public Function CreatePivotTable(strRecordSource As String) As String
Const acFormPivotTable = 3
Dim frm1 As Access.Form

Set frm1 = CreateForm
frm1.DefaultView = acFormPivotTable
frm1.RecordSource = strRecordSource

CreatePivotTable = frm1.Name
DoCmd.Close acForm, CreatePivotTable, acSaveYes
End Function



Public Sub ConfigurePivotTable(strFormName As String) 
Dim fst1 As PivotFieldSet

DoCmd.OpenForm strFormName, acFormPivotTable
Set frm1 = Forms.Item(strFormName)

With frm1.PivotTable.ActiveView
    ***Set fst1 = .FieldSets("Product_Name")***
    .FilterAxis.InsertFieldSet fst1
    Set fst1 = .FieldSets("WorkPlace_Name")
    .ColumnAxis.InsertFieldSet fst1
    Set fst1 = .FieldSets("Activity_Name")
    .RowAxis.InsertFieldSet fst1
    Set fst1 = .FieldSets("Imission_Score")
    .DataAxis.InsertFieldSet fst1
End With

DoCmd.Close acForm, frm1.Name, acSaveYes
End Sub

Das problem ist wie folgt: ich bin immer ein Fehler 13 "Type mismatch" bei der Einstellung der PivotFieldSet und bin ratlos, was ist falsch gehen. Die form ist erstellt mit den entsprechenden recordset und öffnet in PivotTableView und das ist so weit wie es geht. Ich war in der Hoffnung, abrufen, einige Vorschläge, was das problem sein könnte.

Außerdem könnte es noch einen Weg, das zu erreichen mein Ziel, eine matrix wie beschrieben? Da ein export nach Excel benötigt, um den Druck aus der PivotTable-das ist zwar ein Umweg, würde ich eher den end-user nicht haben, zu nehmen.

Vielen Dank im Voraus für jegliche Anregungen oder Antworten zu meinen post.

Dank einem Haufen! Ich habe versucht, vor, aber nicht die Ausgabe wie erwartet. Dank deiner Antwort hatte ich ein anderes Aussehen und das problem gefunden, die in der Abfrage, die ich verwendete. Ich vergaß zu hinzufügen einer where-Klausel, die führte zu eine Menge doppelte Risiko-Kennzahlen. Es stellt sich also heraus eine Kreuztabellenabfrage war in der Tat geeignet für mein problem. Verzeihen Sie mir meine Unwissenheit, und wieder, vielen Dank!

InformationsquelleAutor Makki | 2013-11-15

Schreibe einen Kommentar