Access report - Recordsetclone as. Recordsource - ms-access

I have a form, which has button for report. I want to set Report .Recordsource to whatever on screen is, so basically I need .RecordsetClone of form send to Report. Here is what I tried, but It doesn't work:
Me.Recordsource= Forms!Myform.RecordsetClone
I get an invalid argument on that. Any ideas how to solve this ?
EDIT:
I tried this too - this button is placed on form which has records and opens Report :
Private Sub cmdOpenReport_Click()
DoCmd.OpenReport "MyReport", acViewReport
Reports![MyReport].RecordSource = Me.RecordSource
Reports![MyReport].Filter = Me.Filter
Reports![MyReport].FilterOn = True
End Sub

You can't do that, but you may get away with:
Me.RecordSource = Forms!Myform.RecordSource
though that will not include a filter applied to the form. However, the Filter can be copied the same way, and then:
Me.Filter = Forms!Myform.Filter
Me.FilterOn = True
while sorting must be specified in the report the usual way.
Proof of concept
Private Sub Report_Open(Cancel As Integer)
If MsgBox("Mod 2?", vbQuestion + vbYesNo, "Select") = vbYes Then
Me.RecordSource = "Select * From TestTable Where Id Mod 2 = 0"
End If
End Sub

Gustav, this is correct answer. I have opened another thread for that, but I wasn't aware of what is wrong and where. Sorry for crossposting. here is link to my thread:
Access Report - show current recordsource of another form
Dim strWhere As String
Me.Dirty = False
With Me.Recordset.Clone
Do Until .EOF
strWhere = strWhere & "," & !ID
.MoveNext
Loop
End With
strWhere = Mid(strWhere, 2)
DoCmd.OpenReport "MyReport", acViewReport, WhereCondition:="ID In (" & strWhere & ")
Moderators can delete one of the threads, I can't do that.

Related

Error On click Button Docmd Report Error 3464

Hi Everyone How Are You I hope You Are Well.
I Am Working On A MS Access Application And Want Fetch Result From List Box With On Click Button
But I'm Facing A Error Data Type Mismatch In Criteria expression
Here is My Code On Click Button
Private Sub ViewBalance_Click()
'Dim listrpt As String
If IsNull(listrpt) Then
MsgBox "Please Select The Account Name", vbOKOnly, "Warning"
Exit Sub
End If
DoCmd.OpenReport "List All Parties Balance", acViewPreview, , "[Account ID] = """ & listrpt &
""""
Me.FilterOn = True
End Sub
And Here is My On Report Load Filter But This Filter Working Fine Above Code Give me Error
Report Load Code
Private Sub Report_Load()
DoCmd.Maximize
If MsgBox("Do You Want To Filter Available Balance Only?", vbYesNo + vbQuestion) = vbYes Then
Dim strFilter As String
strFilter = "[RQ]>0"
'verify that it looks good
Me.Filter = strFilter
Me.FilterOn = True
End If
End Sub
Report Name: List All Parties Balance
Query Name Where is Field Name: Account ID is: ListBalance
And listrpt Response Account ID = 60023
Please Anyone Give me A Support & Help Thanks in Advance

Move to another record via combobox doesn't work in some circumstances

I have two forms: ‘frmClient’, (which has a subform that lists applicants), and ‘frmDisclosure’, which shows details of applicants. On frmClient there is a command button that opens a specified record in frmDisclosure. The procedure is Private Sub Command10_Click() - see below. This works.
The problem is that once in frmDisclosure via frmClient, it is not possible to move to another record. The procedure for opening another record in frmDiscloure is in a combobox control: Private Sub ComboFind_AfterUpdate().
This normally works, but it never works if frmDiscloure has been opened via frmClient. I have tried ‘requery’ and ‘refresh’ in various situations, and have tried closing frmClient once frmDisclosure is open. None of this works. If I want to get to a different record, the only solution I have at present is to close frmDisclosure and reopen it.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Sub Command10_Click()
If NumForms > 0 Then
DoCmd.OpenForm "frmDisclosure"
Forms!frmDisclosure.FilterOn = False
DoCmd.OpenForm "frmDisclosure", acNormal, "", "[DiscPK]=" & Me.DiscPK, , acNormal
Else
DisplayMessage ("No form ref for this application.")
Exit Sub
End If
End Sub
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Sub ComboFind_AfterUpdate()
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[DiscPK] = " & Str(Nz(Me![ComboFind], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
frmDisclosure is opened to a single record, there are no other records to navigate. The RecordsetClone has only one record, so of course code won't find any others. Turn off the filter first:
Private Sub ComboFind_AfterUpdate()
Me.FilterOn = False
With Me.RecordsetClone
.FindFirst "[DiscPK] = " & Nz(Me.ComboFind, 0)
If Not .NoMatch Then Me.Bookmark = .Bookmark
End With
End Sub
As you can see, declaring and setting a recordset object variable is not required. .EOF would probably work just as well, I have just always used NoMatch. This will set focus to record, not filter the form.
If you prefer to display single record, then set the Filter property.
Private Sub ComboFind_AfterUpdate()
Me.Filter = "DiscPK=" & Nz(Me.ComboFind, 0)
End Sub

Filter from VBA gets error when I add another filter manually (Access 2016)

Got a database with a bunch of forms. One of them is my "starting page" with several buttons, each opening a form. I have entries from may years so, to make it more functional, I have a combo box on the starting page with all the years whose value I use in a code on all my buttons to filter the forms when they open. The code looks like that:
Private Sub Edit_Sale_Contract_Click()
'Opens the selected form'
DoCmd.OpenForm "new Sale Form", , , , acFormEdit
'Applies the filter'
If Forms![Main Menu].Combo10 = "All" Then
Forms![new Sale Form].FilterOn = False
Else
Forms![new Sale Form].Filter = "[crop]=" & Chr(34) & Forms![Main Menu].Combo10 & Chr(34)
Forms![new Sale Form].FilterOn = True
End If
End Sub
I also have several reports. What I am trying to do is make a form with a Tab control, each Tab holding one report. For now, I have this similar code on the On Open event of my reports:
Private Sub Report_Open(Cancel As Integer)
If Forms![Main Menu].Combo10 = "All" Then
Me.FilterOn = False
Else
Me.Filter = "[crop]=" & Chr(34) & Forms![Main Menu].Combo10 & Chr(34)
Me.FilterOn = True
End If
End Sub
This works fine so far. I open the form with a button(no filter code) and the reports open correctly in the tabs, already filtered.
The problem: When I try to to apply a secondary filter on "Name" for example with RightClick->equals"Name" it refreshes the report and does not apply it.
I tried changing the event on the reports from On Open to On Load. The second filter then applies correctly, but when I click anywhere I get Run-time error '5': Invalid procedure call or argument. Debug indicates the line
MeFilterOn = True
Closing the debugger, the second filter on the "Name" is cancelled, the first one on "Crop" is still on.
Any advice is appreciated. Please note that I am learning Access and VBA myself and I am terribly new at it. Thanks in advance
Your current code overwrites any set filter.
If you want to combine filters, you need to append your new filter to the old one:
Private Sub Report_Open(Cancel As Integer)
If Forms![Main Menu].Combo10 = "All" Then
'Do nothing, because else you would deactivate the custom filter
Else
If Me.Filter <> "" Then
Me.Filter = Me.Filter & " AND [crop]=" & Chr(34) & Forms![Main Menu].Combo10 & Chr(34)
Else
Me.Filter = "[crop]=" & Chr(34) & Forms![Main Menu].Combo10 & Chr(34)
End If
Me.FilterOn = True
End If
End Sub
Note that Access tends to save filters with the report, especially when using layout view. You need to make sure the Report.Filter property is cleared when saving the report.
Use the same approach for the form.

Ms Access filter reports in runtime mode

Does anyone knows how to filter reports the right way within Access runtime mode?
The usual code with DoCmd doesn't work.
This is what I tried for the report:
Private Sub Befehl217_Click()
DoCmd.OpenReport "Tagesbericht", acViewPreview
End Sub
Private Sub Bezeichnungsfeld26_Click()
DoCmd.GoToControl "DateFilter"
DoCmd.RunCommand acCmdFilterMenu
End Sub
This didn't work. Access complained that "FilterMenu isn't available".
I tried to create a context menu but this only displayed me cut, copy and paste.
You confirmed your report includes a control named Bezeichnungsfeld26 and when the user clicks in that control, you want to bring up the filter menu for that control.
When the user clicks in that control, it has the focus, so there is no need for GoToControl. And you don't want to go to a different control if you want the user to filter on Bezeichnungsfeld26.
Disable the GoToControl line ...
Private Sub Bezeichnungsfeld26_Click()
'DoCmd.GoToControl "DateFilter"
DoCmd.RunCommand acCmdFilterMenu
End Sub
You can use the Filter property:
Me.Filter = "[YourField] = " & somevalue & ""
Me.FilterOn = True
or, to expand on your current method:
DoCmd.OpenReport "Tagesbericht", acViewPreview, , "[YourField] = " & somevalue & ""
If you filter for a date you must pass a properly formatted string expression for the date:
Dim FilterDate As Date
FilterDate = Date
DoCmd.OpenReport "Tagesbericht", acViewPreview, , "[DateFilter] = #" & Format(FilterDate, "yyyy\/mm\/dd") & "#"

Why do I get run-time error '2759' when saving the record? access 2010

Using Access 2010, I have a form for Purchase_Orders where the status changes depending on the whether the Items in the sub form have been delivered or not, and, it is influenced by the date as well.
Private Sub Form_AfterUpdate()
Dim rs As Recordset
Dim db As Database
Dim var_Delivered As String
var_Delivered = "SELECT Count(*) AS d_Count" & _
" FROM Items" & _
" WHERE PO_ID =" & Me.PO_ID.Value & _
" AND Supplier_Dnote_ID IS Null" & _
" AND Delivered_Without_Dnote =0;"
Set db = CurrentDb
Set rs = db.OpenRecordset(var_Delivered, dbOpenDynaset)
'MsgBox rs!d_Count
If rs!d_Count > 0 Then
If Me.Supply_date < Date Then
Me.Status = "Overdue"
Else
Me.Status = "Submitted"
End If
Else
Me.Status = "Delivered"
End If
db.Close
Set db = Nothing
Set rs = Nothing
End Sub
This runs after_update of the Purchase_Orders. I have a save_close button that uses the following code and doesn't return an error:
If Me.Dirty = True Then
DoCmd.Close acForm, "Purchase_Orders", acSaveYes
Else
DoCmd.Close acForm, "Purchase_Orders", acSaveNo
End If
However, I also have a Save button that doesn't close the form. This is where I get run-time error 2759 : The method you tried to invoke on an object failed. Debug Highlights the saverecord line.
Private Sub SaveOnlyBtn_Click()
If Me.Dirty = True Then
docmd.RunCommand acCmdSaveRecord
End If
End Sub
If I comment the status code out and use the save button, the record saves fine without any errors. Why do I get this error? I'm completely stumped and searching the error online hasn't helped me either.
So I found that the error did not occur when I put the code in the "on dirty" event, which then made me realise that I don't need necessarily have to run the code after the form updates, only when specific fields change. So I changed my code to a public code and called it when supply date, delivered_without_dnote, or supplier_Invoice_ID changed.
the public code is :
Public Sub delivered_status()
On Error GoTo errTrap1
If Forms!Purchase_Orders_Ex.Form!Status = "Cancelled" Then
Exit Sub
Else
DoCmd.RunCommand acCmdSaveRecord
Dim rs As Recordset
Dim db As Database
Dim var_Delivered As String
var_Delivered = "SELECT Count(*) AS d_Count" & _
" FROM Items" & _
" WHERE PO_ID =" & Forms!Purchase_Orders_Ex.Form!PO_ID.Value & _
" AND Supplier_Dnote_ID IS Null" & _
" AND Delivered_Without_Dnote =0;"
Set db = CurrentDb
Set rs = db.OpenRecordset(var_Delivered, dbOpenDynaset)
'MsgBox "Outstanding Items: " & rs!d_Count
If rs!d_Count > 0 Then
If Forms!Purchase_Orders_Ex.Form!Supply_date < Date Then
Forms!Purchase_Orders_Ex.Form!Status = "Overdue"
Else
Forms!Purchase_Orders_Ex.Form!Status = "Submitted"
End If
Else
Forms!Purchase_Orders_Ex.Form!Status = "Delivered"
End If
rs.Close
Set db = Nothing
Set rs = Nothing
End If
errTrap1:
Select Case Err.Number
Case 3314 'form not complete and other required fields are empty
Exit Sub
Case Else
If Err.Number > 0 Then
MsgBox Err.Number & ": " & Err.Description
End If
End Select
End Sub
Now, when I use either the save_close or Save_Only I do not get error 2759. I do not completely understand which part of my original method caused the error but it no longer occurs with this approach.
I've just encountered this issue and moving code out of Form_AfterUpdate fixed it for me too.
What's (vaguely) interesting is that the code in question worked fine locally, but did not work when deployed to the client. I tried importing just the amended form instead of replacing the whole access app, but I still got the same issue. I also copied the back-end database back from the server to my development machine, but still didn't get the issue locally. On top of that I did endless compact/repair and decompile/compile.
My conclusion at the end of all of that was that this was yet another weird issue emanating from the Access black-box, rather than an issue with the particular code.