VB.NET 2010 RDLC Subreport "Data retrieval failed for the subreport" - sql-server-2008-r2

Using VB.NET 2010 and SQL Server 2008 R2.
I have inherited an application with a RDLC report. I have not used those before. The report was created and works fine but the originator left out a set of data, a subreport for the "parent" record reported on.
I have tried to add a subreport, point the input parameters of the main to the sub(they use same information). Have the sub use a Dataset that points to a Stored procedure and display the results.
Any attempt to execute (and display) sub report data fails with the message
Data retrieval failed for subreport....located at....Please check the log files for more information.
Off what/where are the log files located?
I can pass in params and as long as I add a dataset w/a stored procedure, I can see the params if I put on report. If I add the stored procedure ...Nothing. I tried a fix found on internet to create a Copy of the XSD's stored procedure call... but that didn't work either.
Main report works fine without Sub.
Thoughts?
What am I missing?
Shouldn't I be able to add a subreport, link up the parameters and have subreport display related information?
Here is my code to call main report:
Dim adapter As New SqlClient.SqlDataAdapter
Dim table As New DataTable
Try
Cursor.Current = Cursors.WaitCursor
ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Application.StartupPath & "\IndividualInterviewerDataReport.rdlc"
Dim ReportParameters(5) As Microsoft.Reporting.WinForms.ReportParameter
ReportParameters(0) = New Microsoft.Reporting.WinForms.ReportParameter("SurveyName", frmMain.SurveyName)
ReportParameters(1) = New Microsoft.Reporting.WinForms.ReportParameter("Interviewer", InterviewerId)
ReportParameters(2) = New Microsoft.Reporting.WinForms.ReportParameter("Panel", PanelMonth)
'
'
ReportParameters(3) = New Microsoft.Reporting.WinForms.ReportParameter("- Version: " + ProductVersion)
ReportParameters(4) = New Microsoft.Reporting.WinForms.ReportParameter("PanelYear", PanelYear)
ReportParameters(5) = New Microsoft.Reporting.WinForms.ReportParameter("SurveyNbr", frmMain.SurveyNum)
ReportViewer1.LocalReport.SetParameters(ReportParameters)
ReportViewer1.ShowPrintButton = True
ReportViewer1.ZoomPercent = 100
Me.spInterviewerDataByPanelTableAdapter.Connection.ConnectionString = My.Settings.DBConnection
Me.spInterviewerDataByPanelTableAdapter.Fill(Me.CodingControlDataSet.spInterviewerDataByPanel, frmMain.SurveyNum, PanelYear, PanelMonth, InterviewerId)
Me.ReportViewer1.RefreshReport()

If I'm remembering correctly, you'll need to handle the SubreportProcessing event of the LocalReport object, and set the subreport data there. You do this by setting a property of the EventArgs parameter for the event handler.
Here's some code (adapted from Microsoft documentation):
'In your report setup code'
AddHandler Me.ReportViewer1.LocalReport.SubreportProcessing, _
AddressOf DemoSubreportProcessingEventHandler
'Event hander
Public Sub DemoSubreportProcessingEventHandler(ByVal sender As Object, _
ByVal e As SubreportProcessingEventArgs)
e.DataSources.Add(New ReportDataSource("DatasetNameInReport", MyDataTable ))
End Sub

Related

MS Access VBA DoCmd.OpenForm using where clause not filtering DAO query based recordset

I,m trying to get rid of linked tables and use only VBA code to generate recordset. I found that filtering data using where clause in my DoCmd.OpenForm command doesn't work this way. Is that expected behavior? Or maybe it should work and the problem is located somewhere else... Are OpenArgs the only thing that left me to do this?
To clarify my question:
I have two ms access forms:
One (continuous form) with hyperlink and on click code behind like follows
Private Sub txtPerson_Click()
DoCmd.OpenForm "frmPersonnelDetails", , , "PersonId = " & Me.txtPersonID, acFormReadOnly, acDialog
End Sub
and second one (frmPersonnelDetails), not connected to any recordsource, with recordset created with:
Private Sub Form_Load()
strQuery = "SELECT PersonID, Abbreviation, FirstName, LastName FROM SomeTable"
Set objDaoDb = GetDAODbConn 'function that returns database connection object
Set objDaoRS = objDaoDb.OpenRecordset(strQuery, dbOpenDynaset, dbSeeChanges)
Set Me.Form.Recordset = objDaoRS
End Sub
Now, where clause doesn't work. Second form is opening always on the first record. Is it normal? What is the best way to make it open on specified record?

Create Access reports from Form and save as PDF

Im currently using an Access database to create alot of reports - the report is made using a Query as a source, in the Query i use a Form as criteria so that the open page in the Form is the data used in the report.
Then I save the report as a PDF and click in the form to run the next set of data.
That is very time consuming when i have over 500 reports to make. So is there a way to make a function, a VBA or macro to run through all pages in the form and save each report as a PDF?
My form is named NorwF, the query is NorwQ and the report is NorwRap
I hope that makes sense and that there is a faster way to make this projekt run smoothly.
If you place a button on the form, this code behind should work:
Dim rst As DAO.Recordset
Dim strReport As String
Dim strReportFile As String
strReport = "NorwRap"
'Set rst = Me.RecordsetClone
Set rst = CurrentDB.OpenRecordSet("NorwQ") 'use the query
Do While rst.EOF = False
strReportFile = rst!ID & ".pdf" ' give report name based on field name
DoCmd.OpenReport strReport,acViewPreview , , "id = " & rst!ID
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strOutFile
DoCmd.Close acReport, strReport
rst.MoveNext
Loop

Error 1004 - Vlookup in vba - Unable to get the Vlookup property of the WorksheetFunction class

I've browsed the various questions already asked with this issue other users have faced and none of the solutions seem to fix the error code coming up.
I have a form which prompts the user for a reference number - they input this into the text field and then press OK
'OK button from form1
Public Sub CommandButton1_Click()
refInput = refTextBox.Value
InputRef.Hide
ExportShipForm.Show
End Sub
Once this has been pressed, the next form appears which I would like to be populated with data based on the reference number input on the first form. I have an update button which will update the "labels" on the form to show the data - this is where I am getting an error.
The first label to update is through a Vlookup:
Below the users clicks the update button the 2nd form:
Public Sub btnUpdate_Click()
Call ICS_Caption
lbl_ICS.Caption = Label_ICS
End Sub
This calls a function below:
Public Sub ICS_Caption()
Dim ws1 As Worksheet
refInput = InputRef.refTextBox.Value
Set ws1 = Worksheets("MACRO")
dataRef = Worksheets("Shipping Data").Range("A:K")
Label_ICS = WorksheetFunction.VLookup(refInput, dataRef, 7, False)
End Sub
The error continues to come up each time - I have ran the vlookup manually in a cell outside of VBA and it works fine.
I have typed the range in the Vlookup whilst also using named ranges but each variation shows the same error.
Eventually, I would want the label on form 2 to update with the result of the Vlookup.
Any ideas?
You need to Dim dataRef as Range and then Set it.
See code Below:
Dim DataRef as Range
Set dataRef = Worksheets("Shipping Data").Range("A:K")
Just like a Workbook or Worksheet you need to Set the Range
Just as Grade 'Eh' Bacon suggest in comments its always best to Dim every reference.
The best way to do so is to put Option Explicit all the way at the top of your code. This forces you to define everything which helps it preventing mistakes/typo's etc.
Update edit:
The problem was you are looking for a Reference number in your Sheet (thus an Integer) but refInput is set as a String this conflicts and .VLookup throws an error because it can't find a match.
I have reworked your code:
Your sub is now a function which returns the .Caption String
Function ICS_Caption(refInput As Integer)
Dim dataRef As Range
Set dataRef = Worksheets("Shipping Data").Range("A:K")
ICS_Caption = WorksheetFunction.VLookup(refInput, dataRef, 7, False)
End Function
The update Button calls your Function and provides the data:
Public Sub btnUpdate_Click()
lbl_ICS.Caption = ICS_Caption(InputRef.refTextBox.Value)
End Sub
By using a Function you can provide the Integer value and get a return value back without the need of having Global Strings or Integers.
Which would have been your next obstacle as you can only transfer Variables between Modules/Userforms by using a Global Variable.
I would even advice to directly use the function in the Initialize Event of your 2nd Userform to load the data before the Userform shows this is more user friendly then needing to provide data and then still needing to push an update button.
Verify that you have no missing libraries in VBA IDE > Tools > References
Try using a worksheet cell as the place to store and retrieve refTextBox.Value, rather than refInput (which I assume is a global variable):
Public Sub CommandButton1_Click()
...
Worksheets("Shipping Data").Range($M$1).Value=refTextBox.Value
End Sub
Public Sub ICS_Caption()
Dim refInput as Long'My assumption
...
refInput=Worksheets("Shipping Data").Range($M$1).Value
...
End Sub
Make sure you have Option Explicit at the top of all of your code windows.

Passing arguments to Access Forms created with 'New'

I have a form called 'detail' which shows a detailed view of a selected record. The record is selected from a different form called 'search'. Because I want to be able to open multiple instances of 'detail', each showing details of a different record, I used the following code:
Public detailCollection As New Collection
Function openDetail(patID As Integer, pName As String)
'Purpose: Open an independent instance of form
Dim frm As Form
Debug.Print "ID: " & patID
'Open a new instance, show it, and set a caption.
Set frm = New Form_detail
frm.Visible = True
frm.Caption = pName
detailCollection.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
End Function
PatID is the Primary Key of the record I wish to show in this new instance of 'detail.' The debug print line prints out the correct PatID, so i have it available. How do I pass it to this new instance of the form?
I tried to set the OpenArgs of the new form, but I get an error stating that OpenArgs is read only. After researching, OpenArgs can only be set by DoCmd (which won't work, because then I don't get independent instances of the form). I can find no documentation on the allowable parameters when creating a Form object. Apparently, Microsoft doesn't consider a Constructor to be a Method, at least according to the docs. How should I handle this? (plz don't tell me to set it to an invisible text box or something) Thanks guys, you guys are the best on the net at answering these questions for me. I love you all!
Source Code for the multi-instance form taken from: http://allenbrowne.com/ser-35.html
Inside your Form_detail, create a custom property.
Private mItemId As Long
Property Let ItemID(value as Long)
mItemId = value
' some code to re query Me
End Property
Property Get ItemId() As Long
ItemId = mItemId
End Property
Then, in the code that creates the form, you can do this.
Set frm = New Form_detail
frm.ItemId = patId
frm.Visible = True
frm.Caption = pName
This will allow you to pass an ID to the new form instance, and ensure it gets requeried before making it visible. No need to load all of the results every time if you're always opening the form by Newing it. You let the property load the data instead of the traditional Form_Load event.
This works because Access Form modules are nothing more than glorified classes. Hope this helps.
You could try applying a filter:
frm.Filter = "[ID] = " & patID
frm.FilterOn = True
The Record Source of the Detail form will need to be set to the table to which the ID belongs.
UPDATE
As you requested, here is the code to set the RecordSource:
frm.RecordSource = "select * from TableName where [ID] = " & patID
This is probably cleaner than using a filter given that a user can remove the filter (depending on the type of form).

When viewing a report inside an application, the parameter prompt redisplays when moving to the next page

I'm pretty much a novice with crystal reports. We have another team making our reports, and I'm integrating it with the application. The problem is with paging and user defined parameters. Essentially, the document.table. setdatasource points at our query result set (set in code). The report, upon opening, will then prompt the user for the parameter, which will further reduce the displayed result to the user as the prompt parameter is part of the record selection formula.
AND
{#Age} >= 20 and
{#Age} < 30 and
{Report;1.Sector} = {?NewSector})
This will return a table of more than one page in length. However, requesting the next page will result in the user being prompted for the Sector again, and once provided, will take the user back to page 1 of the results again.
If I take out the reference to the parameter, then obviously the paging works fine.
Is there away to just take the parameter once from the user, and then reuse this value in the subsequent paging requests?
Appreciate your help...
Stupid me. I had the report refreshing every postback.
EDit
A bit more elaboration:
I have the report being created in a RefreshReports procedure, which sets up the report as follows:
Dim objConnectionInfo As CrystalDecisions.Shared.ConnectionInfo
Dim crConnectionInfo As CrystalDecisions.Shared.ConnectionInfo
Dim myconnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings(ApplicationConstants.MyDB).ConnectionString)
Dim myConnBuilder As New SqlConnectionStringBuilder(myconnection.ConnectionString)
'// Log on credentials from web.config
objConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo
objConnectionInfo.IntegratedSecurity = False
objConnectionInfo.ServerName = myconnection.DataSource
objConnectionInfo.UserID = myConnBuilder.UserID
objConnectionInfo.Password = myConnBuilder.Password
crConnectionInfo = objConnectionInfo
'// Get the report details needed
rep = Request.QueryString("Report")
crDoc.Load(Server.MapPath(rep))
crDoc.SetDatabaseLogon(myConnBuilder.UserID, myConnBuilder.Password)
Dim crDatabase As CrystalDecisions.CrystalReports.Engine.Database
Dim crTables As CrystalDecisions.CrystalReports.Engine.Tables
Dim aTable As CrystalDecisions.CrystalReports.Engine.Table
crDatabase = crDoc.Database
crTables = crDatabase.Tables
There is then a series of else ifs like the following:
ElseIf rep = "ExampleRep.rpt" Then
For Each aTable In crTables
If aTable.Name = "GetSectorRep;1" Then
Dim NewSector As String = ""
NewSector = Session("NewSector").ToString()
aTable.SetDataSource(DAL.GetSectorRepTable(NewSector))
End If
Next
In my Page_Init I grab the query strings I need and call RefreshReports. The problem I was having was that I was also requesting RefreshReports in a If Postback on page_load, which meant that every time the report posted back (eg. new page) it was requesting the parameter again, as the report was being created afresh.
So, silly me - quite an obvious mistake in the end!