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.
Related
I have a button in an Access 2007 Form with the on click VBA code as follows:
Private Sub Command53_Click()
DoCmd.OpenReport "BolLSW", acViewNormal, , "[bolnum] = '" & [bolnum] & "'"
End Sub
Essentially this will open a report (based on which number you enter for bolnum when pressing the button) which has the same information as the form and then print it. However when the report prints it print all the records instead of just the one entered.
Is there a way to specify to only print the desired record?
To clarify: Bolnum is a unique field within the form that auto increments. Clicking the "Generate BoL" prompts a dialog box for you to enter the Bolnum to print. Upon entering 2, both record 1 and 2 will print.
I'd try giving [bolnum] (between the 2 &, i.e. the variable ) a different name. I guess Access is taking the value of the current row - so the where-clause is always true.
I've solved this by removing some of the quotes from
DoCmd.OpenReport "BolLSW", acViewNormal, , "[bolnum] = '" & [bolnum] & "'"
specifically:
removing
>'<" & [bolnum] >& "'"<
resulting in:
DoCmd.OpenReport "BolLSW", acViewNormal, 1, "[bolnum] = " & [bolnum]
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]
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.
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 & "'"
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)