Tuesday, February 14, 2012

Data from a recordset

Hi !
I'm new to CR 8.0, so here's my question:

I have a recordset in my VB6 code and want to make a report showing that information. How do I specify in the report's design which fields to show?

If the report used data from a table, then I could select the database fields and paste them in the "Details" section, but as I use a recordset created from VB6, I don't know how to "paste" the fields...

Just in case, this is my code:

Set rstData = db.Execute ("Select * From ...")

Dim Report As New CRAXDRT.Report
Dim CRApp As New CRAXDRT.Application

Set Report = CRApp.OpenReport(gstrPathReports & "\Persons.rpt")

Report.Database.SetDataSource rstData

CRApp.LogOnServer "P2SSQL.DLL", gstrCfgServerName & "\" & gstrCfgInstanceName, _
gstrCfgDBName, "sa", "mypwd"

CRViewer.ReportSource = Report

CRViewer.ViewReportYOu can create a ttx file for your fields, then pass the recordset to Crystal and Crystal will populate the fields in the ttx file in order.

To make a ttx file, use either Method 1 or Method 2:

Method 1: Pass your recordset to this function:

Declare Function CreateFieldDefFile Lib "p2smon.dll" (lpUnk As Object, _
ByVal fileName As String, ByVal bOverWriteExistingFile As Long) As Long

'--------------------------
'LpUnk - The active data source used to create the field definition file.
' In C or C++, this is a pointer to an IUnknown derived COM interface
' relating to a DAO or ADO Recordset. In Visual Basic, this is a
' Recordset or Rowset object.
'Filename - The path and file name of the field definition file to be created.
'bOverWriteExistingFile - If a field definition file already exists with the
' specified path and file name, this flag indicates whether or not to
' overwrite that file.
'--------------------------

'rsRecordset is an ADO Recordset (I haven't tried it with DAO and it doesn't look like it would work with RDO
Call CreateFieldDefFile(rsRecordset, App.Path & "\" & strTtxFileName, True)

Method 2: The manual way

Open Notepad and type your FieldName (doesn't have to match the name of the column, but it makes it easier if it does), then hit Tab and type the DataType (if it's a string, hit Tab again, then type the Max length of the field), then (if you want, it's not necessary) hit Tab again and put in sample data (like what you see for Boolean, True would be the sample data). Sample Data has no effect on your report, I usually leave it out since it's not necessary.

Your ttx file will look like this (I have included all data types it supports):

BLOB BLOB
Boolean Boolean True
Byte Byte
Currency Currency
Date Date
Long Long
Memo Memo
Number Number
Short Short
String String 50
;Comment

If you forget to use a tab in between the FieldName and the DataType, you won't get an error, but that column won't show up in your report. Also, you can include a comment by preceding it with a semicolon.

Save the file as FileName.ttx. (Notepad will default the filename to FileName.ttx.txt, so make sure you delete the .txt from the end or you won't be able to use the file.

After your ttx file is created, open Crystal Reports, choose Add Database, then expand More Data Sources, Active Data. Browse to your ttx file that you created. Now use those fields as you normally would.

The code you have should get your recordset to Crystal.|||I'll try it out, thank you very very much !!!!

No comments:

Post a Comment