Anda di halaman 1dari 8

Using MySQL, Visual Basic 6, and Crystal Reports 9

1. Introduction
. !nato"y #$ ! Report
%. Creating &'e (ield )e$initions
*. +lacing (ields, ,rouping, and Su""ari-ing
.. !dding (or"ulas
6. +reparing )ata
/. +reparing and )isplaying t'e Report
0. Conclusion
9. Sa"ple Code 11
12. Sa"ple Code 1
Introduction
So, you 'a3e a 'uge a"ount o$ data in your MySQL4 data5ase, and your users can "o3e data in and out at 6ill 6it' t'e VB
application t'at you 6rote, 5ut no6 t'ey 6ant t'e data in a "ore "eaning$ul $or". In t'e $irst Visual Basic application I 6rote, I tried
to gi3e t'e" su""aries and reports 5y creating $or"s 6it' dyna"ically allocated arrays o$ la5els, 5uilding t'e $or" as I 6or7ed "y
6ay t'roug' t'e data.
8'ile suc' a "et'od 6or7ed, and t'e $or" loo7ed $airly nice, "a7ing c'anges 6as di$$icult. Mo3ing ele"ents around on t'e $or"
re9uired "odi$ication o$ 3arious constants t'at 6ere used to control place"ent and spacing, and c'anges re9uired a re:co"pile
;granted, I could 'a3e loaded t'e constants $ro" a te<t $ile, 5ut I 6as ne6=. Later, I disco3ered Crystal Reports>, a po6er$ul tool $or
report generation. 8it' Crystal Reports, I 6as a5le to re5uild "y code:5ased report in a "atter o$ "inutes, and "odi$y it at 6ill.
8'en a c'ange 6as "ade, I si"ply 'ad to replace t'e report $ile, and Visual Basic 6ould s'o6 t'e ne6 report t'e ne<t ti"e it 6as
3ie6ed. &'is really 'elped "e sa3e ti"e on data reporting, and I 6as a5le to c'urn out ne6 reports any ti"e a user 6ould re9uest
one.
#ne o$ t'e c'allenges I 'ad 6'en trying to 6or7 6it' Crystal Reports 6as getting t'e report to s'o6 in "y VB application 6it' data
generated using an !)# recordset and MySQL. In $act, 6it' t'e 3ersion o$ Crystal Reports t'at co"es included 6it' VB6 ;3ersion
*.6 o$ Crystal Reports=, t'is $eature 6as not really supported. I personally pre$er to de3elop so$t6are 6it'out using a )S?, 5ut in t'is
case I 'ad to "a7e an e<ception and set up a )S? on "y target syste" so t'at Crystal Reports could access li3e data, 6it' t'e VB
application "odi$ying t'e selection $or"ula o$ t'e report in order to allo6 t'e user control o3er 6'at data 6as displayed in t'e report.
(ortunately, 6'en I $ound "ysel$ a de3eloper position in a co"pany t'at is a reseller $or Crystal Reports, and I $ound "ysel$ a5le to
6or7 6it' Crystal Reports 9. Mo3ing $ro" Crystal Reports *.6 to Crystal Reports 9 6as an i"pro3e"ent si"ilar to "o3ing $ro"
'ard:coding reports to using Crystal Reports. Version 6 o$ Crystal Reports introduced !cti3e )ata, 6'ic' allo6s us to create reports
5ased on !)# pro3ided data. Version 0 o$ Crystal Reports introduced t'e Report )esigner Co"ponent, 6'ic' greatly i"pro3ed t'e
6ay reports are loaded and 3ie6ed in VB. In t'is article I 6ill co3er designing and displaying reports using Visual Basic 6, Crystal
Reports 9, and MySQL. In a $uture article I 6ill also co3er displaying data 6it' Crystal Reports 3ersions 6 and *.6.
&'is article 6ill assu"e t'at t'e reader is using t'e MySQL R)BMS, 5ut s'ould apply to de3elopers using ot'er data5ase
"anage"ent syste"s. (or an o3er3ie6 o$ 6'y MySQL is a good c'oice $or Visual Basic de3elopers, see t'e 8'y VB@MySQL article
on t'is site. (urt'er in$or"ation on MySQL, along 6it' $ull do6nloads, can 5e o5tained $ro" t'e MySQL 6e5 site. &'is sa"ple 6ill
re9uire t'e latest 3ersion o$ Connector @ #)BC ;$or"erly My#)BC=, a3aila5le $or do6nload 'ere ;currently %..1.26=. !s I stated,
t'is article 6ill 5e 5ased on Crystal Reports 9 ;e3aluation copy a3aila5le $or do6nload 'ere=. I reco""end installing "aintenance
pac7 1 and all "ont'ly 'ot $i<es, a3aila5le t'roug' support.crystaldecisions.co".
#n t'e Visual Basic side, I reco""end you 'a3e ser3ice pac7 . installed $or VB, 6'ic' you can do6nload 'ere. !dditionally, t'e
latest 3ersion o$ M)!C ;Microso$t )ata !ccess Co"ponents= s'ould 5e installed, and can 5e $ound 'ere ;currently ./ S+1=. (inally,
t'is article applies to !)# ./ and VB6. It is not applica5le to !)#.?A& under VB.?A& ;Unless you re$erence !)#)B in your
proBect and use it $or data5ase access=.
Creating The Report
Anatomy Of A Report
! crystal report is co"posed o$ se3eral sectionsC
Report Deader
+age Deader
,roup Deader
)etail Line
,roup (ooter
+age (ooter
Report (ooter
!nyt'ing placed in t'e report 'eader and $ooter 6ill 5e displayed once in t'e report. Ite"s in t'e page 'eader and $ooter 6ill 5e
displayed on e3ery page. ,roup 'eaders and $ooters 6ill 5e s'o6n once per group o$ detail lines, and t'ere 6ill 5e a detail line $or
e3ery ro6 o$ data. !ll sections can 5e eit'er 'idden or suppressed so t'at t'ey do not appear in t'e reportC 'idden sections can 5e
displayed 5y Edrilling:do6nE t'roug' dou5le clic7ing on a related section t'at is displayed, suppressed sections are not displayed at
all.
Various in$or"ation can 5e placed in any o$ t'e report sections includingC
&e<t La5els
)ata5ase (ields
I"ages
,rap's
Su""aries
(or"ulas
Most o$ t'ese are sel$:e<planatory. Su""aries include totals, counts, a3erages and ot'er aggregate 3alues 5ased on di$$erent $ields.
(or"ulas allo6 t'e report designer to add co"ple< logic and $or"atting to di$$erent $ields.
Creating The Field Definitions
Creating a typical report is 3ery si"ple. (irst 6e de$ine t'e $ields t'at 6e 6ill 5e reporting on, t'en arrange t'ose $ields on t'e $or",
grouping and su""ari-ing as needed. ! $ield de$inition $ile allo6s us to de$ine t'e data 6e 6ill 5e accessing in our report. Lets start
6it' t'e ta5le 6e 6ill 5e reporting $ro"C
CRA!&A &!BLA report;
transactionid I?& U?SI,?A) ?#& ?ULL !U&#FI?CRAMA?& +RIM!RG HAG,
sales"an CD!R;2= ?#& ?ULL,
region CD!R;2= ?#& ?ULL,
a"ount (L#!&
=I
&o create a $ield de$inition, 6e 5ring up t'e Crystal Reports data5ase e<pert. &'e data5ase e<pert can 5e accessed 5y opening a 5lan7
report, or 5y clic7ing t'eDatabase Expert "enu option under t'e Database drop:do6n "enu. &'e a3aila5le connections tree3ie6
6ill 'a3e a Create New Connection 5ranc' t'at 6'en e<panded 6ill re3eal t'e Field Definitions Only option. By clic7ing Create
File... 5utton on t'e $ile 5ro6ser dialog, a tool $or creating $ield de$inition $iles 6ill 5e displayed.
(ield de$inition $iles are ta5 separated 6it' .tt< e<tensions 6it' $our 3aluesC
$ield na"e
$ield type
$ield lengt' ;i$ string=
sa"ple data
(ield na"e is ta7en directly $ro" t'e na"e o$ t'e $ield in your recordset ;t'is could 5e t'e $ield na"e $ro" your underlying ta5le, or
t'e alias you assigned to it using t'e !S 7ey6ord in your 9uery=, $ield type is t'e datatype o$ t'e $ield, $ield lengt' speci$ies t'e 6idt'
o$ a string, and sa"ple data is a static 3alue used to assist in laying out t'e $ields.
! $ield de$inition $ile $or t'e ta5le de$ined a5o3e "ig't loo7 li7e t'e $ollo6ingC
transactionid uLong 1
sales"an String 2 ,eorge
region String 2 Calgary
a"ount Currency %2.22
#nce 6e 'a3e created our $ield de$inition $ile, 6e can use it to design our report 5y dragging it $ro" t'e E!3aila5le )ata SourcesE 5o<
to t'e ESelected &a5lesE 5o< in t'e )ata5ase A<pert and clic7ing t'e #H 5utton. Be$ore 6e go any $urt'er, it s'ould 5e noted t'at a .tt<
$ile can also 5e created auto"atically $ro" a recordset. &'is can 5e use$ul $or deter"ining 6'ic' report datatypes to use, But I $ind
you still need to go in a$ter and gi3e "eaning$ul sa"ple 3alues. Dere is a 9uic7 sa"ple o$ 'o6 to auto"atically generate a .tt< $ile,
create a ne6 proBect 6it' a re$erence to !)# ./ and place t'e $ollo6ing code in t'e code section o$ $or"1.C
#ption A<plicit
Eps"on.dll MUS& BA +RASA?& I? SGS&AM )IRAC&#RG
EI& C!? BA (#U?) I? &DA J&##LSJ)AVAL#+ARS SUB(#L)AR
E#( G#UR CR9 )IRAC&#RG
+ri3ate )eclare (unction Create(ield)e$(ile Li5 Kps"on.dllK ;lpUn7 !s #5Bect, F
ByVal $ield)e$(ile !s String, ByVal 5#3er8rite(ile !s Long= !s Long
Private Sub Form_Load()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim result As Long
Set conn !e" ADODB.Connection
conn.CursorLocation ad#seClient
conn.ConnectionString $DR%&'R()*S+L ODBC ,.-. Driver/0$ _
1 $S'R&'R.2,.3-4.567..8.0$ _
1 $DA9ABAS'test0$ _
1 $#%Dm*user0$ _
1 $P:Dm*;ass0$ _
1 $OP9%O!$ 1 . < 2 < 6 < ,2 < 2836 < .4,63
conn.O;en Set rs !e" ADODB.Recordset
rs.O;en $S'L'C9 = FRO) re;ort$> conn> adO;enStatic> adLoc?ReadOnl*
@PASS R'CORDS'9> PA9A 9O B'!'RA9'D F%L'> 9R#'CFALS' 9O O&'R:R%9'
result CreateFieldDeDFile(rs> A;;.PatE 1 $Fre;ort.ttG$> 9rue)
%D result 9Een
)sgBoG $File $ 1 A;;.PatE 1 $Fre;ort.ttG created successDull*$>
vb%nDormation> $SuccessH$
'lse
)sgBoG $Failed to create ttG DileH$> vbCritical
'nd %D
'nd Sub
Placing Fields, Grouping, and Summariing
By c'oosing t'e (ield A<plorer under t'e Vie6 "enu, 6e can use t'e $ield e<plorer to c'oose $ields and place t'e" onto t'e $or".
!$ter dragging eac' o$ t'e $ields onto t'e $or", 6e can group data to "a7e it "ore "eaning$ul.
Under t'e Report "enu, t'e ,roup A<pert option 6ill 5ring up t'e ,rouping A<pert. 8it' t'e grouping e<pert 6e can c'oose 6'ic'
$ields 6e 6ill 5e grouping 5y ;in our case region, t'en sales"an=.
#nce grouping is speci$ied, 6e 6ill see t'e t6o groups added to our report, 6it' t'e group na"es no6 s'o6n as part o$ t'e "ain
report in t'e group 'eader sections. By rig't:clic7ing on t'ese t6o section 'eaders, 6e can c'oose t'e suppress option to pre3ent
t'ese group 'eaders $ro" 5eing displayed.
&o su""ari-e data, 6e clic7 on t'e $ield 6e 6is' to su""ari-e, and c'oose Su""ary $ro" t'e Insert "enu. &'e Insert Su""ary
dialog gi3es us options to c'oose 6'ic' $ield to su""ari-e ;in our case t'e a"ount $ield=, 6'at su""ary to per$or" ;6e 6ill
calculate t'e Su"=, and 6'ere t'e resulting 3alue is to 5e placed ;6ill 6ill place one in t'e $ooter $or eac' group and in t'e report
$ooter=. &'e su""aries are t'en a3aila5le on t'e report $or us to $or"at and place.
Adding Formulas
#nce our data is placed on t'e report, 6e can create calculated $ields 5y creating $or"ulas. (or"ulas can 5e 6ritten in eit'er t'e
Crystal Reports synta<, or in CR9, 6e can use Visual Basic synta<. Lets add a si"ple $or"ula to display co""ission $or our salesI in
t'e $ield e<plorer, rig't clic7 on t'e (or"ula (ields 'eader and c'oose ?e6, t'is 6ill display t'e $or"ula editor a$ter pro"pting you
$or a $or"ula na"e ;co""ission 6ill su$$ice $or a na"e=. In t'e top rig't you 6ill $ind a drop:do6n 5o< displaying Crystal Synta<,
c'ange it to say Basic Synta< so 6e can 6or7 6it' Visual Basic type synta<. ?e<t, in t'e $or"ula 6indo6 type t'e $ollo6ingC
$or"ula L Marticle.a"ountN O 2.2.
?o6 you can clic7 sa3e and close t'e $or"ula 6indo6. &'e ne6 $or"ula is no6 a3aila5le in t'e $ield 5ro6ser and can 5e placed on
t'e report in t'e detail section. 8e can add su""aries o$ t'e $or"ula i$ 6e 6is'.
8ell, t'at gi3es us a 5asic report, you can press t'e (. 7ey to see a pre3ie6 o$ t'e report using t'e sa"ple data you entered in your
.tt< $ile. Gou ne<t step 6ill 5e to s'o6 t'e report to your users in your VB application using data retrie3ed $ro" MySQL.
Displaying The Data
8it' Crystal Reports 0 and 'ig'er, t'e #CP Crystal Vie6er is done a6ay 6it' to 5e replaced 6it' t'e Report )esigner Co"ponent.
&'is approac' allo6s $or a "ore o5Bect:oriented approac' to displaying reports. #ne 5ene$it is t'at t'e report 3ie6er is not placed
directly on t'e $or", allo6ing $or t'e report to appear in a speci$ic region o$ t'e $or" 6it'out spa6ning a separate $or".
&'e process $or displaying t'e report in VB is $airly si"pleC
instantiate !)#)B Connection and Recordset o5Bects
populate recordset o5Bect 6it' 9uery
instantiate Crystal Reports !pplication and Report o5Bects
open report
5ind report to recordset
5ind report to 3ie6er
s'o6 report
&o display a report in your V5 application you 6ill need t'e $ollo6ingC
Re$erencesC
Microso$t !cti3eP )ata #5Bects ./ Li5rary
Crystal Reports 9 !cti3eP )esigner Run &i"e Li5rary
Co"ponentsC
Crystal Reports Vie6er Control 9
Gou 6ill also need a Crystal Reports Vie6er Control placed on your $or" na"ed CRVie6er.
Preparing Data
(irst 6e are going to need t'e data t'at 6e 6ill 5e reporting on. &'is is done using !)#C
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn !e" ADODB.Connection
conn.CursorLocation ad#seClient @S'R&'RIS%D' !O9 R'CCO)'!D'D
conn.ConnectionString $DR%&'R()*S+L ODBC ,.-. Driver/0$ _
1 $S'R&'R.2,.3-4.567..8.0$ _
1 $DA9ABAS'test0$ _
1 $#%Dm*username0$ _
1 $P:Dm*;ass0$ _
1 $OP9%O!$ 1 . < 2 < 6 < ,2 < 2836 < .4,63. @S'9 ALL PARA)'9'RS
conn.O;en @9A'S' OP9%O! &AL#'S AR' B'S9 FOR &B

conn.'Gecute $DROP 9ABL' %F 'J%S9S re;ort$> > ad'Gecute!oRecords

@AD'J'C#9'!OR'CORDS %S #S'D FOR

@+#'R%'S 9AA9 R'9#R! !O DA9A

@(CR'A9'> %!S'R9> D'L'9'> '9C.)
conn.'Gecute $CR'A9' 9ABL' re;ort($ _
1 $transactionid %!9 #!S%B!'D !O9 !#LL A#9O_%!CR')'!9 PR%)ARK L'K>$ _
1 $salesman CAAR(28) !O9 !#LL>$ _
1 $region CAAR(28) !O9 !#LL>$ _
1 $amount Float)$> > ad'Gecute!oRecords @9A' 9ABL' :' :%LL R'POR9 OFF
OF

@P#9 SO)' DA9A %! 9A'
9ABL'
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Beorge@>@Calgar*@>,8.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Beorge@>@Calgar*@>.-.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Beorge@>@San Mose@>.8.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Merr*@>@Calgar*@>,8.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Merr*@>@San Mose@>.88.88)$> > ad'Gecute!oRecords

Set rs !e" ADODB.Recordset
rs.O;en $S'L'C9 = FRO) re;ort$> conn> adO;enStatic> adLoc?ReadOnl*
Preparing and Displaying the Report
?o6 t'at 6e 'a3e a recordset, 6e Bust need to load t'e report, lin7 it to t'e recordset and t'e 3ie6er, and display itC
Dim cr*stal As CRAJDR9.A;;lication @LOADS R'POR9 FRO) F%L'
Dim re;ort As CRAJDR9.re;ort @AOLDS R'POR9

CR&ie"er.Dis;la*Border False @)AL'S R'POR9 F%LL '!9%R' FOR)
CR&ie"er.Dis;la*9abs False @9A%S R'POR9 DO'S !O9 DR%LL
DO:!> !O9 !''D'D
CR&ie"er.'nableDrillDo"n False @R'POR9 DO'S !O9 S#PPOR9 DR%LLIDO:!
CR&ie"er.'nableReDresEButton False @ADO R'CORDS'9 :%LL !O9 CAA!B'>
!O9 !''D'D
Set cr*stal !e" CRAJDR9.A;;lication @)A!AB'S R'POR9S

Set re;ort cr*stal.O;enRe;ort(A;;.PatE 1 $Fre;ort..r;t$) @OP'! O#R
R'POR9

re;ort.DiscardSavedData @CL'ARS R'POR9 SO :' :ORL FRO)
R'CORDS'9
re;ort.Database.SetDataSource rs @L%!L R'POR9 9O R'CORDS'9

CR&ie"er.Re;ortSource re;ort @L%!L &%':'R 9O R'POR9
CR&ie"er.&ie"Re;ort @SAO: R'POR9

Do :Eile CR&ie"er.%sBus* @NOO) )'9AOD DO'S !O9 :ORL
:A%L'
Do'vents @R'POR9 %S
LOAD%!B> SO :' )#S9 PA#S'
Loo; @:A%L'
R'POR9 LOADS.

CR&ie"er.Noom 73
Once tEe re;ort is dis;la*ed> "e can tEen cleanu; out obOectsP
rs.Close @ALL B'LO: A'R' %S CL'A!#P
Set rs !otEing

conn.Close
Set conn !otEing
Set cr*stal !otEing
Set re;ort !otEing
Finall*> "e can add some code to tEe Form_ResiQe event to ensure tEat our re;ort occu;ies
tEe entire DormP
Private Sub Form_ResiQe() @)AL' S#R' R'POR9 F%LLS FOR)
CR&ie"er.9o; 8 @:A'! FOR) %S R'S%N'D
CR&ie"er.LeDt 8
CR&ie"er.AeigEt ScaleAeigEt
CR&ie"er.:idtE Scale:idtE
'nd Sub
Conclusion
!nd t'ere you 'a3e it. I$ all goes 6ell, you no6 'a3e a report displaying on your VB $or", generated $ro" MySQL data. (ro" 'ere
you can loo7 at creating "ore co"ple< $or"ulas, pulling data $ro" "ultiple recordsets, and reporting on 9ueries "ore co"ple< t'an
your typical SALAC& O 9uery.
Crystal Reports is a registered trade"ar7 o$ Crystal )ecisionsC 666.crystaldecisions.co"
Sample Code !"
)o6nload !ttac'ed Sa"plecode
Displaying a Crystal Reports 9 Report with MyS! and "#$.
Private Sub Form_Load()
@9A%S COD' %S #S'D 9O D%SPLAK A CRKS9AL R'POR9S 7 R'POR9 #S%!B
@DA9A P#LL'D FRO) A )KS+L DA9ABAS' #S%!B ADO A!D &%S#AL BAS%C 4
@KO# !''D R'F'R'!C'S 9OP )icrosoDt ActiveJ Data ObOects 2.5 Librar*
@ Cr*stal Re;orts 7 ActiveJ Designer Run 9ime Librar*
@KO# !''D 9A' CO)PO!'!9P Cr*stal Re;orts &ie"er Control 7
@ADD 9A' CR&%':'R7. CO)PO!'!9 9O KO#R FOR) A!D !A)' %9 CR&ie"er.

Dim conn As ADODB.Connection @CO!!'C9%O! 9O BROL'R +#'R%'S
Dim rs As ADODB.Recordset @AOLDS ALL DA9A R'9#R!'D FRO) +#'RK
Dim cr*stal As CRAJDR9.A;;lication @LOADS R'POR9 FRO) F%L'
Dim re;ort As CRAJDR9.re;ort @AOLDS R'POR9

CR&ie"er.Dis;la*Border False @)AL'S R'POR9 F%LL '!9%R' FOR)
CR&ie"er.Dis;la*9abs False @9A%S R'POR9 DO'S !O9 DR%LL DO:!> !O9
!''D'D
CR&ie"er.'nableDrillDo"n False @R'POR9 DO'S !O9 S#PPOR9 DR%LLIDO:!
CR&ie"er.'nableReDresEButton False @ADO R'CORDS'9 :%LL !O9 CAA!B'> !O9
!''D'D

Set conn !e" ADODB.Connection
conn.CursorLocation ad#seClient @S'R&'RIS%D' !O9 R'CCO)'!D'D
conn.ConnectionString $DR%&'R()*S+L ODBC ,.-. Driver/0$ _
1 $S'R&'R.25.8.8..0$ _
1 $DA9ABAS'test0$ _
1 $#%Dtestuser0$ _
1 $P:D.2,3-0$ _
1 $OP9%O!$ 1 . < 2 < 6 < ,2 < 2836 < .4,63 @S'9 ALL PARA)'9'RS
conn.O;en @9A'S' OP9%O! &AL#'S AR' B'S9 FOR
&B

conn.'Gecute $DROP 9ABL' %F 'J%S9S re;ort$> > ad'Gecute!oRecords
@AD'J'C#9'!OR'CORDS %S #S'D FOR
@+#'R%'S 9AA9 R'9#R! !O DA9A
@(CR'A9'> %!S'R9> D'L'9'> '9C.)
conn.'Gecute $CR'A9' 9ABL' re;ort($ _
1 $transactionid %!9 #!S%B!'D !O9 !#LL A#9O_%!CR')'!9 PR%)ARK L'K>$ _
1 $salesman CAAR(28) !O9 !#LL>$ _
1 $region CAAR(28) !O9 !#LL>$ _
1 $amount Float)$> > ad'Gecute!oRecords @9A' 9ABL' :' :%LL R'POR9
OFF OF

@P#9 SO)' DA9A %! 9A' 9ABL'
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Beorge@>@Calgar*@>,8.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Beorge@>@Calgar*@>.-.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Beorge@>@San Mose@>.8.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount)
&AL#'S(@Merr*@>@Calgar*@>,8.88)$> > ad'Gecute!oRecords
conn.'Gecute $%!S'R9 %!9O re;ort(salesman> region> amount) &AL#'S(@Merr*@>@San
Mose@>.88.88)$> > ad'Gecute!oRecords

Set rs !e" ADODB.Recordset
rs.O;en $S'L'C9 = FRO) re;ort$> conn> adO;enStatic> adLoc?ReadOnl*

Set cr*stal !e" CRAJDR9.A;;lication @)A!AB'S R'POR9S

Set re;ort cr*stal.O;enRe;ort(A;;.PatE 1 $Fre;ort..r;t$) @OP'! O#R R'POR9

re;ort.DiscardSavedData @CL'ARS R'POR9 SO :' :ORL FRO)
R'CORDS'9
re;ort.Database.SetDataSource rs @L%!L R'POR9 9O R'CORDS'9

CR&ie"er.Re;ortSource re;ort @L%!L &%':'R 9O R'POR9
CR&ie"er.&ie"Re;ort @SAO: R'POR9

Do :Eile CR&ie"er.%sBus* @NOO) )'9AOD DO'S !O9 :ORL :A%L'
Do'vents @R'POR9 %S LOAD%!B> SO :' )#S9 PA#S'
Loo; @:A%L' R'POR9 LOADS.

CR&ie"er.Noom 73

rs.Close @ALL B'LO: A'R' %S CL'A!#P
Set rs !otEing

conn.Close
Set conn !otEing

Set cr*stal !otEing
Set re;ort !otEing
'nd Sub
Private Sub Form_ResiQe() @)AL' S#R' R'POR9 F%LLS FOR)
CR&ie"er.9o; 8 @:A'! FOR) %S R'S%N'D
CR&ie"er.LeDt 8
CR&ie"er.AeigEt ScaleAeigEt
CR&ie"er.:idtE Scale:idtE
'nd Sub
Sample Code !#
)o6nload !ttac'ed Sa"plecode
Creating a .ttx field definition file fro% an &DO recordset
O;tion 'G;licit
@;2smon.dll )#S9 B' PR'S'!9 %! SKS9') D%R'C9ORK
@%9 CA! B' FO#!D %! 9A' F9OOLSFD'&'LOP'RS S#BFOLD'R
@OF KO#R CR7 D%R'C9ORK
Private Declare Function CreateFieldDeDFile Lib $;2smon.dll$ (l;#n? As ObOect> _
B*&al DieldDeDFile As String> B*&al bOver:riteFile As Long) As Long
Private Sub Form_Load()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim result As Long
Set conn !e" ADODB.Connection
conn.CursorLocation ad#seClient
conn.ConnectionString $DR%&'R()*S+L ODBC ,.-. Driver/0$ _
1 $S'R&'R.2,.3-4.567..8.0$ _
1 $DA9ABAS'test0$ _
1 $#%Dm*user0$ _
1 $P:Dm*;ass0$ _
1 $OP9%O!$ 1 . < 2 < 6 < ,2 < 2836 < .4,63
conn.O;en
Set rs !e" ADODB.Recordset
rs.O;en $S'L'C9 = FRO) re;ort$> conn> adO;enStatic> adLoc?ReadOnl*
@PASS R'CORDS'9> PA9A 9O B'!'RA9'D F%L'> 9R#'CFALS' 9O O&'R:R%9'
result CreateFieldDeDFile(rs> A;;.PatE 1 $Fre;ort.ttG$> 9rue)
%D result 9Een
)sgBoG $File $ 1 A;;.PatE 1 $Fre;ort.ttG created successDull*$>
vb%nDormation> $SuccessH$
'lse
)sgBoG $Failed to create ttG DileH$> vbCritical
'nd %D
'nd Sub