How to Dynamically Change the Rowsource of an Access Chart - vba

I would like to know if there is any way I can set the rowsource property of a chart in my report at run time.
I intend to have a chart in my report's group header section. The rowsource of this chart should be updated according to the group header's value.
I got the error 2455 - invalid reference to the property RowSource when I tried to do this in VBA.
I am using Access 2003.
Thank you.

I just got an inspiration after searching over the internet for some time. Here is the solution I currently implement.
Firstly, it is true that the rowsource property of a chart cannot be changed programmatically at run time. However, what we can do is to set the rowsource property to be a Query object and later update this query object in VBA.
Here is part of my code.
CurrentDb.QueryDefs("myQuery").SQL = "a new query"
Me.myChart.Requery
I have set my chart's row source to a query object named "myQuery". I placed the above code in the Format event of my group header, so every time when the group header is loaded I can use the value of my group header to update the Query object.

Another approach would be to open the form or report the chart is embedded in two steps. In the example below I am using a report, but it works just as fine with forms:
First step: open report in design view but hidden mode. Now the chart
row source can be edited (because you are in design view) but the
process is invisible (because you are in hidden mode).
Second step: save and close hidden report and re-open it now in a
"visible" mode
'report name
strReportmName = "SomeReportName"
'open report in design view but hidden
DoCmd.OpenReport strReportmName , acViewDesign, , , , acHidden
'edit chart RowSource
strSQL = "TRANSFORM Sum(Cabecas) AS SomaDeCabecas " & _
"SELECT Data "
...etc...
"PIVOT Categoria In (" & Chr(34) & strTitColunas & Chr(34) & ")"
'update chart RowSource
Reports![SomeReportName].Controls![SomeChartName].RowSource = strSQL
'Save report with edited RowSource
DoCmd.Close acReport, strReportmName , acSaveYes
're-open it in normal, visible mode
DoCmd.OpenReport strReportmName , acViewPreview

Related

Programmatically find and navigate to record on subreport in MS Access using VBA?

In Access, I can navigate to a record in a subform by using code similar to:
Dim rs As Recordset
Set rs = Me.Form.RecordsetClone
rs.FindFirst "CodeID = " & MyID
Me.Form.Bookmark = rs.Bookmark
Is there any way to do something similar with subreports, or any other way to navigate to a record within a subreport without using filters to only show one record? I'm looking to still be able to see the other records in the report, having the focus jump to the relevant record.
I figured out a way to do it. Assuming the report is embedded as a subreport:
Me.rptTest.SetFocus
DoCmd.SearchForRecord , "", acFirst, "[IDField] = " & RelevantID

Code for button on form to update table in ms access

I'm trying to create a simple form using unbound combo boxes and a button that updates a table when clicked. I have already created another form that is similar to this that works great, so I tried to replicate it using the appropriate fields, but I get an error message- Run-time error '3061: Too few parameters. Expected 1.
The only two fields on the form are FrameID and FrameLocation and the button is MoveFrame. The table to be updated is StockFrames.
This is the code I have on the on-click event on the button:
Private Sub MoveFrame_Click()
CurrentDb.Execute "UPDATE StockFrames SET frameLocation = " & Me.frameLocation & _" WHERE FrameID = " & Me.FrameID
Me.frameLocation.Value = Null
Me.FrameID.Value = Null
End Sub
I appreciate any help anyone can offer. I'm very new to MS Access. If there is any other information that you need as far as the property settings, please let me know. I'm eager to learn and to get this form working properly.
Thank you.
If frameLocation is a text field, parameter needs apostrophe delimiters:
='" & Me.frameLocation & "' WHERE.
Remove that underscore character.
Date/Time fields use # delimiter.

Running a report directly and from a navigation form - reference issue

I have a form in MS Access (365) which accepts various selection criteria and runs a report with those criteria built into the wherecondition of a DoCmd.OpenReport command. The report is run via a button with On Click VBA code, since the wherecondition has to be built according to which, if any, criteria have been chosen. That all works fine when I open the selection criteria form directly. I want to open the selection criteria form via a navigation form, since there will be other, similar reports with selection criteria that I want to run from within the same navigation form.
As stated above, everything works when I open the selection criteria form directly and run the report but when I tried it via a navigation form, it didn't work. That's ok, I found a solution on the MS Dev Center site which works when I run the selection criteria form (and then the report) from the navigation form. All fine. But then (of course) the references within the button On Click code don't work when I open the selection criteria form directly and run the report. I would like to be able to run the selection criteria form and then the report from both positions - directly from MS Access and via the navigation form. There will presumably be some way to achieve this but (as I said above, I am new to MS Access and VBA) I could spend a lot of time clutching at shadows. Hopefully, someone will be able to tell me the simplest way to do this?
Code sample with relevant comments below. On runnning the selection criteria form and report directly, the line commented as AAA works ok, MsgBox ABB and DDD and those beyond all show; on running them via the navigation form, line BBB works ok, MsgBox ABB and DDD and those beyond all show (well, they would except that I haven't yet coded the [NavigationSubform] option into all the other whereconditon building stuff). When I switch the lines AAA and BBB (ie, comment the other one out) MsgBox AAA show ok, then it fails with:
Can't find referenced form "frmSelectSpeciesSiteDates"
before reaching MsgBox DDD.
MsgBox "ABB"
'If Not Forms![frmSelectSpeciesSiteDates]![cboCommonName] = "" Then
' AAA Works when frmSelectSpeciesSiteDates is run directly
If Not Forms![frmNavSelectiveReports]![NavigationSubform].[Form]![cboCommonName] = "" Then
' BBB Works when frmSelectSpeciesSiteDates is called from a navigation form
strWhereCondition = strWhereCondition & "[common name] = " & Chr(34) & Me![cboCommonName] & Chr(34)
End If
MsgBox "DDD"
You have a form named frmSelectSpeciesSiteDates which has a combobox named cboCommonName. The form also includes VBA code which uses the combobox value to modify a string variable ...
strWhereCondition = strWhereCondition & "[common name] = " & Chr(34) & Me![cboCommonName] & Chr(34)
However you only want to modify the string when the combobox contains something other than an empty string. And that is where you're struggling. You reference the combobox one way when the form is opened directly as a top-level form, and another way when it is contained in a navigation form ...
Forms![frmSelectSpeciesSiteDates]![cboCommonName]
Forms![frmNavSelectiveReports]![NavigationSubform].[Form]![cboCommonName]
I suggest you abandon both of those and refer to the combobox the same as where you modify the string (Me!cboCommonName) ...
If Not Me!cboCommonName = "" Then
strWhereCondition = strWhereCondition & "[common name] = " & Chr(34) & Me!cboCommonName & Chr(34)
End If

Set MS-Access Image Control Source from Attached Image Using VBA

I'm adding a photo to form to display for each user with other info of that user. I created a tabbed form, on page one I select a user and press a button that runs the following code:
Private Sub Command106_Click()
Dim qry_rs As DAO.Recordset
Dim qry_db As Database
Set qry_db = CurrentDb
SQLString = "SELECT [Clients Info].* FROM [Clients Info] WHERE ((([Clients Info].Info_Client)='" & [Forms]![Form-1]![Combo13_PageOne_Name] & "'));"
Set qry_rs = qry_db.OpenRecordset(SQLString)
Forms![Form-1].[Info_Member].Value = qry_rs![Info_Member]
Forms![Form-1].[Info_Tower].Value = qry_rs![Info_ID]
End Sub
I tried using the same way that I set the values of that textboxes in setting an image control value
Forms![Form-1].[Info_Picture].Value = qry_rs![Info_UserIDPhoto]
But it's not working, can anyone help?
I receive this message:
Run time error 2465:
Microsoft Office Access can't find the field "|" referred to in your expression
I'm using Office 2007, on Windows 7
To my knowledge, there is no way to refer to the ControlSource property through vba. However, using the property sheet in Design View works PERFECTLY. Use the value of your combo box as the criteria in a DLookup expression.
Enter this as the ControlSource of your image control:
=DLookUp("[YourAttachmentField]","[YourTable]","[YourNameField] = '" & [YourComboBox]. [Column] (x) & "'")
(Make sure you're referencing the correct column; I'm just using x as an example.)
This won't work if you alter the attachment's name or location AFTER it's been attached, so if any changes are made you must reattach.
VBA allows you to set the Picture property:
Forms![Form-1].[Info_Picture].Picture=qry_rs![Info_UserIDPhoto]
The Picture property value must be a file name--or complete path and file name if not found in the default folder set in Access options.
When set from VBA, the value may be a statement or variable whose value is a (path and) file name. In the case above, the field [Info_UserIDPhoto] would contain the complete path and file name of each user's ID photo.
(Unlike the ControlSource property, you cannot enter a variable or field name for this property directly in the design view property page.)
You can reference the control source property by doing the following:
Dim c As Control
Set c = Forms("FormName").Form.Controls("ControlName")
c.Properties("ControlSource") = "Your control source name here"

Text box to have its value based on combo box value using a query

I have a single form on which there exists several combo boxes and text box. one combo box value (for the Wells) will be filled independently, then I needs the text box to have its value based on the value of the wells combo box value. I have created a query that solved the propblem partially, it requires parameter which the wells combo box value. If I ran with query a part from the form, it wroks good and asks for the parameter and it is OK. I do think to make use of VBA, adding a code which process a SELECT statement (of the query mentioned above), then to tell it to take its parameter from the wells combo value which will ready on the form.
Can someone help on this. Can this works as I descriped.
Thanks in advance.
Mohamed
Further to my question above, I have tried the following solution:
Private Sub Well_ID_Change()
Last_Ref.ControlSource = " SELECT TOP1 New_Ref FROM" & _
" BSW_Transactions WHERE BSW_Transactions.New_Ref Is Not Null AND BSW_Transactions.Well_ID = " & Me.Well_ID.Value & _
" ORDER BY BSW_Transactions.Sample_Date DESC"
End Sub
the Last_Ref is the text box I want to fill in with result of the embedded SELECT statement in the code. The Well_ID is the combo box which value will be the parameter of the SELECT statement. The Well_ID is number field and it displays the well_name and stores the associated ID value in the table. Upon running the form after saving changes, the Last_Ref text box showed (#Name?). I guessed that the text box (is a number field) found a text in the combo box Well_ID, so I added ".Value" to the above syntax at the criteria Me.Well_ID. However the problem still exists.
May I mistaken in the syntax, would someone help on this. Can this works fine?
Thanks in advance. Mohamed
The problem is that text boxes cannot have their control source set like a combo box or a list box, it needs to be passed the value e.g:
Private Sub Well_ID_Change()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT TOP1 [New_Ref] FROM" & _
" [BSW_Transactions] WHERE [BSW_Transactions].[New_Ref] Is Not Null" & _
" AND [BSW_Transactions].[Well_ID] = " & Me.Well_ID & _
" ORDER BY [BSW_Transactions].[Sample_Date] DESC", dbOpenSnapShot)
rs.MoveFirst
Last_Ref = rs![New_Ref]
rs.Close
Set rs = Nothing
End Sub
I've also dropped the .Value again from your combo box as its not required