OK there are absolutely no good articles on the internet that I can find that explain or have code examples on how to create a pivot chart using VBA. I need this because I need the pivot chart to show different results depending on user selection in a form. There are some for Excel but the syntax is different for Access. I know this is a lame question, but if anyone has an example of how to create a pivot chart in VBA I would really appreciate the help.
Well after about 3 days of searching I think I found it. Not that anyone really cares, this only has like 6 views, says a lot for VBA's utter horribleness. Anyway, MSDN had this hidden under "Office XP" instead of under Access, but whatever.
http://msdn.microsoft.com/en-us/library/aa662945.aspx#
I've create a PivotChart form in Access 2007. On another form I have the user selection controls and the pivotform as a subform. Then I use code like this in the main form. The object model is the same as OWC11 (Office Web Components 11).
Private Function DisplayChart()
With mysubform.Form.ChartSpace
.Clear
.AllowFiltering = True
.HasChartSpaceTitle = True
.ChartSpaceTitle.Caption = "test"
.DisplayFieldButtons = False
.DisplayToolbar = False
.ConnectionString = ...
.CommandText = "SELECT rSeries, rCategory, rDate, rValue " & _
"FROM myTable"
.Charts(0).Type = chChartTypePie
.SetData chDimSeriesNames, chDataBound, "rSeries"
.SetData chDimCategories, chDataBound, "rCategory"
.SetData chDimValues, chDataBound, "rValue"
.HasChartSpaceLegend = True
End With
End Function
the constants can be derived from OWC11
C:\Program Files\Common Files\Microsoft Shared\Web Components\11\OWC11.DLL
You need them at the top of the module. At this stage I'm not sure how to extract them from the Access pivotchart. Make a reference to OWC11 and set the subform ChartSpace to a variable declared as an OWC11.ChartSpace. After writing the code change to type 'Object', and remove the reference for late binding (and re-test). This way your refs won't come unstuck on a 64bit machine when you deploy.
Private Enum ChartConstants
chDimSeriesNames = 0
chDimCategories = 1
chDimValues = 2
chDataBound = 0
chAxisPositionValue = -8
chAxisPositionCategory = -7
chChartTypePie = 18
End Enum
Remember you can also let the user have access to the PivotChart properties form, field lists and drop zones. Or they can right-click the chart to get to them.
(Note - this is still a new discovery for me so I will endeavor to update this answer if I find any gotcha's.)
Related
I have an access database that basically has a form where you can lookup some of the database entries. User types in part number, I check that it exists and if it does I return a report with all the data organized in a nice easy-to-read way.
The problem is that, say I generate the report. And then go back to the Form to query for a different part, the report doesnt update or a new report isnt generated (even if I click the "View report" button multple times).
I was wondering if there is a way to generate a new report (with the same template, from same form, different parameters) and just be able to have multiple reports of the same template with different parts to compare or just to analyse.
As forms can be used multiple times this should work with reports too. Try:
Dim rtp1 As Report_yourReportsName
Dim rtp2 As Report_yourReportsName
Set rpt1=New Report_yourReportsName
Set rpt2=New Report_yourReportsName
rpt1.visible=True
rpt2.visible=True
Keep in mind to put Report_ in front of reports name, like it is done with Form_.
Code to open up to 25 report with one button (button named CommandOpenReports):
'1) Declare a global variable (in the module header, outside of the subroutine) like this:
'Assume we'll never need more than 25 instances of the
'same report open at once
Dim rpt(1 To 25) As Report
Private Sub CommandOpenReports_Click()
Dim I As Long
'2) set a member of the global array equal to the specific instance, filter according to whatever criteria you'd like, and make the instance visible.
For I = 1 To 25
If rpt(I) Is Nothing Then
Exit For
End If
Next
'open rptPOs in separate windows
Set rpt(I) = New Report_Bericht1
rpt(I).Filter = stLinkCriteria
rpt(I).Visible = True
End Sub
Code stolen from Multiple Instances of a Report
I am relatively new to Access but have a little experience on VBA. I am trying to create a database of daily work checklists with various questions for different clients. A friend suggested I create a dynamic form through VBA that will create multiple questions in textboxes every time a client is selected from a dropdown box. I am trying to create textboxes in VBA but Im getting an error message saying I need to be in design mode to create text boxes.
I lifted the below code from the internet, is it possible to activate the design view once the code is running as I need to start in form view to click the drop-down or if anyone has a suggestion for another way I'd appreciate it too. Thanks
Private Sub ComClient_AfterUpdate()
Dim x As Integer
Dim frm As Form
Dim ctrl As Control, ctlText As Control, CtlLabel As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
Dim count As Integer
count = RecordCount
For x = 1 To count
Set ctrl = CreateControl("TestControlCreate", acTextBox, acDetail, , "", 0 + (x * 300), 0, 300, 240)
frm.RecordSource = "Get_Questions"
ctrl.ControlName = "TxtBx" & x
Set ctlText = CreateControl("Question " & x, acTextBox, , "", "", _
intDataX, intDataY)
Set CtlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
DoCmd.Restore
Next
End Sub
Working in MS Access can be somewhat frustrating, because its version of forms, etc. is not the full-blown SDK that you got in VB6. Therefore, a lot of VB forms code you find on the Internet will not work.
The approach I would recommend is to create a form laid out (that is you this by hand) with the maximum number checkboxes you think you will possibly need, and use VBA code to programmatically hide/display them (by manipulating the visible property). You can similarly change the labels and data-bindings at runtime. This may seem like a hack, but is a valid way of accomplishing your goal.
if you have a table or query containing all the questions for a given client, you can also display a continuous subform with a hidden textbox (ClientID), a textbox (Question) and a Checkbox (answer), in order to display all questions with a checkbox.
Your 'main-form' will basically only contain a Combobox in order to select your client, then you can link the clientID of the main-form (Combobox value) with the ClientID of your sub-form and MS Access will do the rest, with no need to code your business logic.
In this way you can, if needed, also store your answers in your database, have a nice User Interface and a dynamic design with no hard-coded questions...
I am working on a Microsoft Access Database (Office 2010) and I have a tab control on the bottom of my form that displays equipment information based on the type of equipment you select. I am trying to make it dynamically display and hide tabs as determined by the name of the tabs.
To accomplish this I have used the following naming convention for my tabs.
Tab_Static_Description
Tab_Static_Comments
Tab_Static_Maintenance
Tab_Config_Computer1
Tab_Config_Computer2
Tab_Config_Printer1
Tab_Config_Scanner1
Tab_Config_Telephone1
Tab_Config_Display1
My TabControl is called "Tabs"
I have set the form to execute the function below(the function i need help with) on form load and onchange of the equipment drop down menu.
To call the function i use the following code.
DisplayTab
Here is the code to far. I have been googling this a bit and I have yet to find someone doing something similar to me and have found myself a little lost on this one. Any help would be greatly appreciated.
Function DisplayTab(EquipmentType As String)
Dim Ctl As Control
For Each Ctl In Me.Tabs
If Ctl.Name.contains("Tab_Static_") Then
Me.Tabs.Pages.Item(Ctl).Visible = True
ElseIf Ctl.Name.contains("Tab_Config_") Then
If Ctl.Name.contains("Tab_Congig_" & EquipmentType) Then
Me.Tabs.Pages.Item(Ctl).Visible = True
Else
Me.Tabs.Pages.Item(Ctl).Visible = False
End If
Else
MsgBox "There is an unusually named tab. Please contact the database adminsitrator."
End If
Next Ctl
End Function
The error I am getting is "Invalid qualifier" but after googling that message it doesn't exactly make sense.
If Ctl.Name.contains("Tab_Static_") Then
is not valid VBA syntax. You are probably thinking of some similar method that might be availabe in VB.NET. In VBA you would do something like
If InStr(Ctl.Name, "Tab_Static_") > 0 Then
Okay, friends, I'm leaving my job in a week and a half, and I'm trying to make what I've done easier for my boss to do. He has no access knowledge, so I'm trying to create a form that will automate the reports I've been generating. Rather than create a different form for all the different reports, I'm trying to automate it from a table of parameters. Here's what I'm going for:
I have a table, which I have created, which is comprised of 5 fields. I'd like to use these fields to fill parameter fields in a standard form template. The five fields in my table are as follows:
The type of query being run (the result spit out)
The queries that generate this report, separated by a comma and no space. "QRYNAMEA,QRYNAMEB"
The Table which these queries generate, which will be used by transferspreadsheet
The destination excel file, which already has a pivot table set up to feed of the data.
The input sheet of this excel file. Currently, all of these sheets are called "Input". (that isn't important)
My issue comes with having no idea where to go after I've made my combo box. I know enough visual basic to automate my queries, but not enough to populate the form with the information in 3,4 and 5 (so far, I've been manually changing these for different queries). I have no idea how to look up the record in the table from the choice in the 'choosebox', and then select individual fields from that in my automation.
I'm pretty confident in my ability to parse #2 and automate the queries, and to put the values into the fields I'm looking at, but I don't know how to actually pull those values from the table, before I can do these things. I also can't seem to describe this well enough for google to help me.
Has anyone done something like this before? I'm assuming I just lack knowledge of one of the VBA libraries, but I've not had any luck finding out which.
edit:
my inclination at this point is to create a query for this table, which will return a single field depending on the input I give. I can imagine doing this in SQL, but I still don't know how to populate the forms, nor extract the field object from the table once I get it.
I have to head out for the day, but I'll be back on Friday to keep working on this, and I'll post my solution, once I find it. This seems like a unique conundrum, and it would be nice to give an answer to it.
Final edit: code is polished (does not have much in the way of error handling):
The first method, which pulls the fields from the table and populates the form, is activated by choosing a new entry in the combo box and looks like this:
Private Sub QuerySelect_Change()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("SELECT [Queries to Run], [Source Table], [Destination Spreadsheet], [Destination Sheet Name] FROM TBL_QRY_SETTINGS WHERE TBL_QRY_SETTINGS.[Query Type] Like '" & [Forms]![QuerySelector]![QuerySelect] & "';")
[Forms]![QuerySelector]![QueriesToRun].Value = rec("Queries to Run")
[Forms]![QuerySelector]![SourceTable].Value = rec("Source Table")
[Forms]![QuerySelector]![FileDest].Value = rec("Destination Spreadsheet")
[Forms]![QuerySelector]![SheetName].Value = rec("Destination Sheet Name")
Set rec = Nothing
Set db = Nothing
End Sub
The second code pulls that data to run the query. I like how this turned out. It runs when a button near the combobox is clicked.
Private Sub DynamicQuery_Click()
Dim qryArray As Variant
Dim i As Integer
qryArray = Split([Forms]![QuerySelector]![QueriesToRun], ",")
DoCmd.SetWarnings False
For i = LBound(qryArray) To UBound(qryArray)
Debug.Print qryArray(i)
DoCmd.OpenQuery (qryArray(i))
Next
DoCmd.SetWarnings True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, [Forms]![QuerySelector]![SourceTable], _
[Forms]![QuerySelector]![FileDest], _
True, [Forms]![QuerySelector]![SheetName]
End Sub
Note that the final code for part (1) is almost the same as the selected answer, except that I am grabbing more than one field. This works because I know that I have unique "Query Types", and my recordset will only contain one record.
Anyway, I hope some people stumble upon this and find it useful. Send me a message if you do. As far as I can tell from brief googling, this sort of automation work has not been done in access. It should make it easier for access-illiterate to run their own queries, and be simple for designers to add to, if they want all their queries available after a few clicks.
Someone could conceivably use this to automate a variety of reports in sequence, by iterating through a table like the one I reference.
I may be massively misunderstanding what you're doing, but I think it's as easy as creating a new form using the form wizard. It will let you choose the table that contains the data, and it will let you choose which fields you want to add.
You can later change any of the textboxes to combo boxes which will allow you to limit the choices available to fill in.
Am I understanding that correctly?
EDIT: This will fill a variable (MyRandomField) with the contents of a field in a table
Dim db as Database
Dim rec as Recordset
set db = CurrentDB
set rec = db.OpenRecordSet("Select SomeField from SomeTable Where Something = 'SomethingElse'")
MyRandomField = rec("SomeFieldName")
set rec = Nothing
set db = Nothing
How to enumerate MS Access reports in windows dot net application.
I have a listbox that should show the available reports in a MS Access db. Tried following code, but it did not show anything.
oAccess = GetAccessDBObject(DBLocation);
Reports accessReports = oAccess.Reports; // oAccess.Reports.Count = 0!!! No data comes out here!!!
How do I iterate ms-access reports in .net?
I tried this example in C# and it works:
Access.Application oAccess = new Access.Application();
oAccess.OpenCurrentDatabase(#"D:\tmp\dbtest.mdb", false, "");
Access.AllObjects reports = oAccess.CurrentProject.AllReports;
Console.WriteLine(reports.Count);
foreach (Access.AccessObject report in reports)
{
Console.WriteLine(report.Name);
}
(you have to add a reference to Microsoft Access Object Library to your project, of course.)
As Tony Toews mentions, if Access is not installed, you can't automate. This SQL will work on any system:
SELECT MSysObjects.Name
FROM MSysObjects
WHERE (((MSysObjects.Type)=-32764))
ORDER BY MSysObjects.Name;
However, note that permissions can get in the way of accessing the Jet/ACE system tables.
Also, there's not much you can do with a list of Access reports without Access! But I thought it was good for completeness to mention this.
foreach ( Reports rprt in oAccess.Reports )
{
listbox.items.Add(rprt);
}
// Disclaimer: Wrote it off the top of my head, don't know if a Reports object has a name property to show it's name in your listbox.