jchance
asked on
Ad Hoc Crystal Reporting
Is there a way to create a form to gather information that is contained in a ODBC datasource and then
feed a dynamically generated SQL statement or a collection of records into a crystal report template
that can be viewed on the web. I'm trying to come up with a way to allow for ad-hoc reporting within
a web based management tool where the reports will be not only easy to view but to print as well.
feed a dynamically generated SQL statement or a collection of records into a crystal report template
that can be viewed on the web. I'm trying to come up with a way to allow for ad-hoc reporting within
a web based management tool where the reports will be not only easy to view but to print as well.
1. Get the field collection
2. Display it on a web form
3. Build the SQL after asking the user what he wants
4. Use this sql as the data source of the crystal report by using sqlquery property, by using asp
5. Trigger the report from form.
2. Display it on a web form
3. Build the SQL after asking the user what he wants
4. Use this sql as the data source of the crystal report by using sqlquery property, by using asp
5. Trigger the report from form.
ASKER
mlmcc-- I really need a sample that will work on a web based application. I've seen a VB example but I still was unable to transition the code into a working format.
raj nbr ---It sounds just like what I'm looking for, however, I have not done much work using asp. Do you have a code example on how you would call up a crystal report by passing the sqlquery property a value.
raj nbr ---It sounds just like what I'm looking for, however, I have not done much work using asp. Do you have a code example on how you would call up a crystal report by passing the sqlquery property a value.
I've recently done this two different ways using ASP (and although we have ASP experts here, I am an ASP Novice). The first way creates a Crystal Report entirely on the fly, and does not require an .rpt file to be set up. My test ASP is listed below. You will probably have to play with it a little, as I used a SQL Server 7.0 Stored Procedure to get my data. However, the principles are pretty straight forward: Create an ADO Recordset with the data on which you want to report, create the Crystal Application and Report Objects, then create and insert the elements you require. I found that the difficult part is gathering the necessary information from the user in a easy and comprehensive manner. There is some decent documentation and samples on the Crystal Website: http://www.crystaldecisions.com (let me know if you have trouble finding them). I added some comments in the below code where you will need to make changes. I will post a second comment with the other option. This example assumes that you have the necessary Crystal Reports 8.0 components installed, and uses the Crystal ActiveX Smart Viewer. This report is for a top 10, 20, etc., report - you would change your data accordingly.
ASP SCRIPT:
<%@ Language=VBScript %>
<%
dim vInstructionTopXXX
dim vHeaderInstruction
dim vCityInstruction
dim vRollYearInstruction
vInstructionTopXXX="Input Desired Number"
vHeaderInstruction="Enter Report Title"
vCityInstruction="Enter City Name"
vRollYearInstruction="Ente r Roll Year"
bGo = cbool("0" & Request.QueryString("go"))
strTopXX = Request.Form("TopXX")
' strReportHeader = Request.Form("RptHeader")
' strCity = Request.Form("RptCity")
strRollYear = Request.Form("RollYear")
%>
<!-- #include virtual = "/includes/adovbs.inc" -->
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM method=post action="dab2.asp?go=1">
<P>Top XXX Report - <%=vInstructionTopXXX%></P >
<P><INPUT type="Text" Name="TopXX"></P>
<P><%=vRollYearInstruction %></P>
<P><INPUT type="Text" Name="RollYear"></P>
<P><INPUT style="WIDTH: 129px; HEIGHT: 24px" type=submit size=43 value="Show Report"></P>
<P>
</FORM>
<%
if bGo then
set cn = server.CreateObject("ADODB .Connectio n")
set rs = server.CreateObject("ADODB .Recordset ")
set cmd = server.CreateObject("ADODB .Command")
' SQL = "select * from _PTX.PTX_USER"
cn.ConnectionString="Provi der=SQLOLE DB.1;persi st security info=true;Data Source=YourServerName;init ial catalog=YourDatabase;user id=YourUserID;password=You rPassword"
cn.Open
if cn.State <> 1 Then
%>
<P>Connection Could Not Be Established</P>
<%
end if
'rs.Open SQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
set cmd.ActiveConnection = cn
'Set the command Type to Stored Procedure - 4
'************************* ********** ********** ******
'This is where you would change your code to retrieve
'the data into an ADO recordset any way you choose.
'************************* ********** ********** ******
cmd.CommandType=4
cmd.CommandText="StoredPro cName"
cmd.Parameters.Refresh
with cmd
.Parameters("@iTopCount"). Value = strTopXX
.Parameters("@CityCode").V alue = "005"
.Parameters("@iTaxYear").V alue = CInt(strRollYear)
.Parameters("@iOption").Va lue = 1
end with
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
set rs.ActiveConnection = cn
rs.CursorLocation = 3
'rs.CursorType = adUseClient
set rs.Source = cmd
rs.Open
'Set rs = cmd.Execute
'************************* ********** ********** ********** ********** *****
'Create Crystal Reports Objects and Display the Report.
'************************* ********** ********** ********** ********** *****
reportname = server.MapPath(".\dab_test 1.rpt")
' reportname = "e:\PTX_TopXXX_Test.rpt"
' reportname = "c:\My Documents\Projects\GIS_PTX \PTX_TopXX X_Test.rpt "
' reportname = "g:\webprojects\devroot\PT X_TopXXX_T est.rpt"
'Create the Application Object
If Not IsObject(session("oApp")) Then
Set session("oApp") = Server.CreateObject("Cryst alRuntime. Applicatio n")
End If
'Close existing report object, if any, and open the report.
If IsObject(session("oRpt")) then
Set session("oRpt") = Nothing
End If
Set session("oRpt") = session("oApp").NewReport
'Set session("oRpt") = session("oApp").OpenReport (reportnam e, 1)
session("oRpt").MorePrintE ngineError Messages = False
session("oRpt").EnablePara meterPromp ting = False
session("oRpt").DiscardSav edData
'Create Report Parameter Objects and pass the Parameter Values to
'the Crystal Report Objects.
''set session("ParamCollection") = session("oRpt").Parameterf ields
''set ParamNumHeader = session("ParamCollection") .Item(1)
''set ParamRollYear = session("ParamCollection") .Item(2)
'Parameter 12 in SetCurrentValue tags parameter as a String
''Call ParamNumHeader.SetCurrentV alue (CStr(strTopXX), 12)
''Call ParamRollYear.SetCurrentVa lue (CStr(strRollYear), 12)
set Database = session("oRpt").Database
set Tables = Database.Tables
set session("oRs") = Server.CreateObject("ADODB .Recordset ")
' Set session.Contents("oRs") = rs
Set session("oRs") = rs
Tables.Add "",,session("oRs"),,"p2smo n.dll"
''set Table1 = DatabaseTables.Item(1)
''Table1.SetPrivateData 3, session("oRs")
'Create the Report Detail Section and Populate with Values
Set crSection = session("oRpt").Sections.I tem("D")
Set rhSection = session("oRpt").Sections.I tem("RH")
Set phSection = session("oRpt").Sections.I tem("PH")
Set crFieldObject = crSection.AddFieldObject(" {ado.busin ess_name}" ,720,0)
Set crFieldObject1 = crSection.AddFieldObject(" {ado.prope rty_count} ",5500,0)
'Create the Page Header Object and Column Titles
Set phTextObject = phSection.AddTextObject("T est Col 1", 600, 0)
'Format the Text Object
With phTextObject
.Height = 350
.Width = 4000
.HorAlignment = 1
.Font.Size = 10
.Font.Underline = TRUE
.Font.Name = "Times New Roman"
.TextColor = "&hFF0000"
End With
Set phTextObject = phSection.AddTextObject("T est Col 2", 5700, 0)
'Format the Text Object
With phTextObject
.Height = 350
.Width = 4000
.HorAlignment = 1
.Font.Size = 10
.Font.Underline = TRUE
.Font.Name = "Times New Roman"
.TextColor = "&hFF0000"
End With
'Create the Report Header Text Object
Set rhTextObject = rhSection.AddTextObject("M y Custom Header", 2500, 0)
'Format the Text Object
With rhTextObject
.Height = 350
.Width = 4000
.HorAlignment = 2
.Font.Size = 16
.Font.Bold = TRUE
.Font.Name = "Times New Roman"
.TextColor = "&hFF"
End With
session("oRpt").Save reportname
'Retrieve the Records and Create the Page On Demand Engine Object
On error Resume Next
session("oRpt").ReadRecord s
If Err.number <> 0 Then
Response.Write err.Source & " " & err.Number & " " & err.Description
Else
If IsObject(session("oPageEng ine")) Then
set session("oPageEngine") = Nothing
End If
set session("oPageEngine") = session("oRpt").PageEngine
End If
rs.Close
cn.Close
set rs = nothing
set cn = nothing
set cmd = nothing
%>
<!-- #include file="SmartViewerActiveX.a sp" -->
<%
end if
%> </P>
</BODY>
</HTML>
Good Luck!
ASP SCRIPT:
<%@ Language=VBScript %>
<%
dim vInstructionTopXXX
dim vHeaderInstruction
dim vCityInstruction
dim vRollYearInstruction
vInstructionTopXXX="Input Desired Number"
vHeaderInstruction="Enter Report Title"
vCityInstruction="Enter City Name"
vRollYearInstruction="Ente
bGo = cbool("0" & Request.QueryString("go"))
strTopXX = Request.Form("TopXX")
' strReportHeader = Request.Form("RptHeader")
' strCity = Request.Form("RptCity")
strRollYear = Request.Form("RollYear")
%>
<!-- #include virtual = "/includes/adovbs.inc" -->
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM method=post action="dab2.asp?go=1">
<P>Top XXX Report - <%=vInstructionTopXXX%></P
<P><INPUT type="Text" Name="TopXX"></P>
<P><%=vRollYearInstruction
<P><INPUT type="Text" Name="RollYear"></P>
<P><INPUT style="WIDTH: 129px; HEIGHT: 24px" type=submit size=43 value="Show Report"></P>
<P>
</FORM>
<%
if bGo then
set cn = server.CreateObject("ADODB
set rs = server.CreateObject("ADODB
set cmd = server.CreateObject("ADODB
' SQL = "select * from _PTX.PTX_USER"
cn.ConnectionString="Provi
cn.Open
if cn.State <> 1 Then
%>
<P>Connection Could Not Be Established</P>
<%
end if
'rs.Open SQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
set cmd.ActiveConnection = cn
'Set the command Type to Stored Procedure - 4
'*************************
'This is where you would change your code to retrieve
'the data into an ADO recordset any way you choose.
'*************************
cmd.CommandType=4
cmd.CommandText="StoredPro
cmd.Parameters.Refresh
with cmd
.Parameters("@iTopCount").
.Parameters("@CityCode").V
.Parameters("@iTaxYear").V
.Parameters("@iOption").Va
end with
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
set rs.ActiveConnection = cn
rs.CursorLocation = 3
'rs.CursorType = adUseClient
set rs.Source = cmd
rs.Open
'Set rs = cmd.Execute
'*************************
'Create Crystal Reports Objects and Display the Report.
'*************************
reportname = server.MapPath(".\dab_test
' reportname = "e:\PTX_TopXXX_Test.rpt"
' reportname = "c:\My Documents\Projects\GIS_PTX
' reportname = "g:\webprojects\devroot\PT
'Create the Application Object
If Not IsObject(session("oApp")) Then
Set session("oApp") = Server.CreateObject("Cryst
End If
'Close existing report object, if any, and open the report.
If IsObject(session("oRpt")) then
Set session("oRpt") = Nothing
End If
Set session("oRpt") = session("oApp").NewReport
'Set session("oRpt") = session("oApp").OpenReport
session("oRpt").MorePrintE
session("oRpt").EnablePara
session("oRpt").DiscardSav
'Create Report Parameter Objects and pass the Parameter Values to
'the Crystal Report Objects.
''set session("ParamCollection")
''set ParamNumHeader = session("ParamCollection")
''set ParamRollYear = session("ParamCollection")
'Parameter 12 in SetCurrentValue tags parameter as a String
''Call ParamNumHeader.SetCurrentV
''Call ParamRollYear.SetCurrentVa
set Database = session("oRpt").Database
set Tables = Database.Tables
set session("oRs") = Server.CreateObject("ADODB
' Set session.Contents("oRs") = rs
Set session("oRs") = rs
Tables.Add "",,session("oRs"),,"p2smo
''set Table1 = DatabaseTables.Item(1)
''Table1.SetPrivateData 3, session("oRs")
'Create the Report Detail Section and Populate with Values
Set crSection = session("oRpt").Sections.I
Set rhSection = session("oRpt").Sections.I
Set phSection = session("oRpt").Sections.I
Set crFieldObject = crSection.AddFieldObject("
Set crFieldObject1 = crSection.AddFieldObject("
'Create the Page Header Object and Column Titles
Set phTextObject = phSection.AddTextObject("T
'Format the Text Object
With phTextObject
.Height = 350
.Width = 4000
.HorAlignment = 1
.Font.Size = 10
.Font.Underline = TRUE
.Font.Name = "Times New Roman"
.TextColor = "&hFF0000"
End With
Set phTextObject = phSection.AddTextObject("T
'Format the Text Object
With phTextObject
.Height = 350
.Width = 4000
.HorAlignment = 1
.Font.Size = 10
.Font.Underline = TRUE
.Font.Name = "Times New Roman"
.TextColor = "&hFF0000"
End With
'Create the Report Header Text Object
Set rhTextObject = rhSection.AddTextObject("M
'Format the Text Object
With rhTextObject
.Height = 350
.Width = 4000
.HorAlignment = 2
.Font.Size = 16
.Font.Bold = TRUE
.Font.Name = "Times New Roman"
.TextColor = "&hFF"
End With
session("oRpt").Save reportname
'Retrieve the Records and Create the Page On Demand Engine Object
On error Resume Next
session("oRpt").ReadRecord
If Err.number <> 0 Then
Response.Write err.Source & " " & err.Number & " " & err.Description
Else
If IsObject(session("oPageEng
set session("oPageEngine") = Nothing
End If
set session("oPageEngine") = session("oRpt").PageEngine
End If
rs.Close
cn.Close
set rs = nothing
set cn = nothing
set cmd = nothing
%>
<!-- #include file="SmartViewerActiveX.a
<%
end if
%> </P>
</BODY>
</HTML>
Good Luck!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If this sounds like something you would like to see I can provide it to you.
good luck
mlmcc