Access Two Table SubReport - String Where - ms-access

I have a quick question about reports. I have a report that has two unbound subreports (the data is from two different databases and do not share any information. I usually opened these reports individually by using a simple UI to select different options and generate a string where and open the report ie,
DoCmd.OpenReport str_rptname, acViewReport, , strWhere
strWhere is made by the UI
Now that I have combined them into one report the string where does not work. Is it possible to pass a where string to a subform? If so how would I go about doing so? Is there another/better way to do this?
Thank you!

as far as I can see you pinpointed the problem correctly as you wrote in your comment:
I believe it is because the string where opens the main report and
sets the filter there, but the sub reports are not bound so open
unfiltered
Because the subreport are unbound you need to give them a complete recordset and not just a WHERE clause. Like this:
dim whereClause As String
dim strRecordset as String
'***Build your where clause
strRecordset = _
"SELECT yourColumns " & _
"FROM yourTables " & _
"WHERE " & whereClause
Me.subreport.recordset = strRecordset
Me.subreport.Requery
Requery might be optional.

Related

pass parameters to record source with query Access2007

I am relatively new to access and I have been tasked to improve the navigation of the app.
I have a form which is used to open the report.
The record source of the report is as follows,
SELECT
Projects.Project_Number,
Projects.Customer,
Projects.End_User,
Projects.Engineering_Company,
[Merged Ship and Delivery Dates].Sched_Next_Delivery_Date,
[Merged Ship and Delivery Dates].Sched_Next_Ship_Date,
Projects.QC_Pack_Req,
Projects.Target_QC_Pack_Date,
Projects.Invoice_QC_Pack
FROM
Projects LEFT JOIN [Merged Ship and Delivery Dates]
ON Projects.Project_Number = [Merged Ship and Delivery Dates].Project_Number
WHERE
(((Projects.Project_Number = [Project Number] )))
ORDER BY Projects.Project_Number;
I am trying to get the report to open up without prompting me every time. There are instances where I need to refresh the report or open it from other forms.
I have tried to use
DoCmd.OpenReport "Project Control Sheet", _
acViewReport, , _
"[Project_Number]=" & Me.ProjectNumber, , _
"[Project_Number]=" & Me.ProjectNumber
It is still unable to pass the parameters to the record source. Is there anyway for me to pass the parameters to the recordsource(query)?
I have tried to use
Forms![formName]ProjectNumber
in the where statement but this only works for a single form and I have other forms which opens up this report.
The purpose of refreshing the form is to allow users to view the changes made to the report after they have updated it.
If you remove the WHERE clause from the report's Record Source query, you can later filter the rows it returns with the WhereCondition option of the DoCmd.OpenReport method.
Dim strWhereCondition As String
strWhereCondition = "[Project_Number]=" & Me.ProjectNumber.Value
Debug.Print strWhereCondition ' <- view this in Immediate window; Ctrl+g will take you there
'DoCmd.OpenReport "Project Control Sheet", acViewReport, , strWhereCondition
DoCmd.OpenReport ReportName:="Project Control Sheet", _
View:=acViewReport, WhereCondition:=strWhereCondition
If you want a different approach, you could use a TempVar, since your Access version is 2007.
TempVars.Add "ProjectNumber", Me.ProjectNumber.Value
And change the Record Source query to use the TempVar ...
WHERE Projects.Project_Number = [TempVars]![ProjectNumber]

Filtering a report query

Still a beginning access programmer here. Trying to get the report to work. Here is what I am doing
I first created a report using the Report Wizard using the following query as input
SELECT EmployeeId, Project, StartDate
FROM Tasks;
I have a form wherein I select the employee-id. I want to filter the report based on the employee id selected. Here is what I have for invoking the report
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = " & strempid
qryEmpReport is the name of the query that holds the report query that I mentioned above. The strempid holds the value that was selected in the form. However when I get around to execute this, it prompts me to enter the employee id again. Any ideas as to why I am getting this? I have validated to make sure that the strempid does contain the value selected earlier.
I'll guess Tasks.EmployeeId is text datatype. If my guess is correct, add quotes around the value you supply for EmployeeId:
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = '" & strempid & "'"
Based on our trouble-shooting exercise in the comments, I think you should give yourself an opportunity to examine the actual string value you're giving to OpenReport for its WhereCondition argument. (It's better to view the actual string instead of trying to imagine what it looks like.)
Dim strWhereCondition As String
strWhereCondition = "EmployeeId = '" & strempid & "'"
Debug.Print "strWhereCondition ->" & strWhereCondition & "<-"
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", strWhereCondition
View the output from Debug.Print in the Immediate window; Ctrl+g will take you there.

MS Access enter parameter value

I have a form and in the form is a sub form that displays rows from a query. One of the columns in the subform is the DNANumber. The report works 100%. Problem is, when I call the report using the following code,
strWhereClause = "[DNANumber]=" & strText
DoCmd.OpenReport "Certificate", acViewPreview, , strWhereClause, , acHidden
I get a popup message asking for the parameter value, also displaying that exact value it is looking for above the textfield. I have checked all the spelling in the queries, forms , subforms and tables and controlls. Everything is fine. Why do I get this popup message. Further, If I type in the value, it displays the report without a problem.
If [DNANumber] is text data type, add quotes around the value of strText when you build strWhereClause.
strWhereClause = "[DNANumber]='" & strText & "'"

Wrong RecordCount on Filtered Form with SQL View DataSource

I am using an Access2010 project as frontend, referring to a MS SQL Server 2008 as backend. Within my Access project there is form frmKlientenÜbersicht. This form has a view abfKlientenÜbersicht as dataSource.
Now I am trying to get the current number of records showing up in my form by using this code:
Private Sub Form_Current()
Debug.Print "Form_Current: " & anzahlDatensätze
lblAnzahlDatensätze.Caption = anzahlDatensätze & " Klient(en)"
End Sub
Private Function anzahlDatensätze() As Integer
Dim rs As Recordset
Set rs = Me.RecordsetClone
rs.MoveLast
anzahlDatensätze = rs.RecordCount
End Function
This works fine until I am using some filters. If I am using any filter on my form, the number of records stays unchanged!
What do I have to change to get the current number of records showing up (if filtered or not)?
What is the reason why my code does not show the correct number of records?
EDIT: According to the given comments and answers I tried setting Count([pkKlient] onto a textbox (see new pic) and tried DCount("*", "abfKlientenÜbersicht", me.Filter) from within VBA Code.
Unfortunatelly it seems that the filterClause is not valid when using it as parameter value for DCount. (see pic for filterClause).
As you can see count(..) does not result in a correct number - and the filterClause (generated by access!!) seems not to be valid for use by DCount(..)
If someone wants to try it, on your own, just create an ADP, add a form, add a view, form dataSource is a view, set a filter, and try to get the number of records?!!
Looking forward for any comments/answers/hints!
with VBA, DCount will give you what you need
DCount("*", "MyTable", Me.Filter)
If you want to put this on the form, there's an easier way. use an unbound box, and set it to =count([FieldName])
This count should remain correct, regardless of if it's counted filtered records or not.
Some notes, there are a dozen things that could go wrong with this, it could hardly even be called tested. However, it was returning the correct count for me.
Apparently, the form filter just hides records, whereas this will apply a real filter. However, you need to get the format into the right shape for a valid filter. In the end, a WHERE statement would probably be easier.
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
With Me.Recordset
''Filter
If ApplyType = 1 Then
''Very, very roughly. Remove form name, use single quotes
''You will need a lot more code for safety
sfilter = Replace(Me.Filter, "[" & Me.Name & "].", "")
sfilter = Replace(sfilter, """", "'")
.Filter = sfilter
MsgBox "Recordset : " & Me.Recordset.RecordCount & vbCrLf _
& "Filtered : " & .RecordCount
Else
''Remove filter - ApplyType 0
.Filter = ""
End If
End With
End Sub
Additional note with similar caveats
You can also set a textbox to something on these lines:
=IIf([FilterOn]=True,DCount("id","ATable",
Replace(Replace([Filter],"[" & [Name] & "].",""),"""","'")),Count([id]))
(Remove the break in the line, it is cosmetic)

Is it possible to click on a report record to open relevant form in Access using VBA

I have a report with details of jobs/tasks, and also a form which contributes the majority of the data towards that report. Given that a report is a nice way of looking at the larger picture of the data, and a form is the best way of editing data, I would like to be able to click on a row, and have it open up the relevant record in the form view.
Does anyone know how to do this through VBA? In my mind it should be possible, though my knowledge of objects in Access is limited.
Update
I've implemented the following code for my report:
Private Sub Edit_Click()
Dim EntityName As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Entity & "'"
DoCmd.OpenForm DocName, acNormal, , EntityName
End Sub
It successfully opens the correct form, and it also grabs the correct entity name, however it isn't filtering properly. I can't see what the issue is with the code.
In your Edit_Click() procedure, you have EntityName as the WhereCondition parameter to OpenForm.
DoCmd.OpenForm DocName, acNormal, , EntityName
However, you haven't assigned anything to EntityName, so it's an empty string. I think you should use strWhere as the WhereCondition.
Private Sub Edit_Click()
Dim strWhere As String
Dim DocName As String
DocName = "Entity: Overview"
strWhere = "[Entity Name]='" & Me.Entity & "'"
DoCmd.OpenForm DocName, acNormal, , strWhere
End Sub
I assumed Entity is the name of a control, and that's where you get a value to build strWhere. If there is no control in the report by the name of Entity, then the code won't work even if there is an Entity in the original query.
You should be able to add an On Click event in the Detail section of the report, then add something like this in the VBA handler:
DoCmd.OpenForm "MyForm", acNormal, "", "ID=" & ID.Text
(where MyForm is the target form, and ID is a hidden or visible control in your report that identifies the current record).
You say report, but you should not be using a report but a continuous form or datasheet. You can then add events to any of the controls on any line, and add a double-click event, if you do not want to drive your users insane. You could also add a command button if that would be clearer to your users, or format the "link" textbox to underline. The record opened by the action shown by #dbaseman will open which even record has the focus (current record). And that will lead you to discover what you can and can't do with a continuous form :D