Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CR 8.5 RDC VFP7 'Server has not yet been opened' error
Message
General information
Forum:
Visual FoxPro
Category:
Crystal Reports
Miscellaneous
Thread ID:
00665637
Message ID:
00665972
Views:
83
Thanks for your effort! I will look at your article again and, perhaps, start from scratch using that as my base. I had created a class to handle Crystal (based on Mark Miller's February, 2002 FoxPro Advisor article). I have included the code from my .VCX (printed by using Rick Schummer's PrintCX utility). I do not expect you to go through my code (I don't think that you have that kind of time). Rather, I might post this as an example for others to see the good, the bad and the ugly of "my" VFP7/CR85 RDC code.

Thank you Craig Berntson!
Thank you Mark Miller!
Thank you Rick Schummer!



RAS PrintCX Source code for: i:\autorep\classes\crystalobjects.vcx
Independent Objects
* For COMMENT platform, TimeStamp: Not built into App, Rec No: 1,
Unique Id: Class (VERSION = 3.00)
* Object: doreport BaseClass: custom [SubClassed from: (custom)]
* For WINDOWS platform, TimeStamp: 06/06/02 14:42:26 , Rec No: 5,
Unique Id: _0J817SO2I (Class)
* Properties *
Width = 20
ncounter = 0
nattachmentposition = 0
Name = "doreport"
* Methods *
1. PROCEDURE setformulavalues
2. Lparameters cFormulaName, xValue
3. *
4. * Purpose: Set the specified value of a Crystal Report formula to the specified value
5. * This could be as simple as setting a report title. It could also be used to
6. * tell the report to only look at a certain subset of records in the dataset.
7. * See the Customer.rpt for a sample.
8. *
9. * cFormulaName Name of formula we want to set
10. * xValue Value to set - could be any datatype supported by CR8.5
11. *
12. * ASSUMES:
13. * crReport Valid Crystal Reports Report Object
14. *
15. * RETURN VALUES:
16. * .T. Formula value set successfully
17. * .F. Formula value not set - most likely cause is misspelled formula name
18. *
19. * Is the formula name specified with a leading '@' sign?
20. *
21. Local x, lFound, cValue
22.
23. If Left(cFormulaName, 1) # '@'
24. cFormulaName = '@' + cFormulaName
25. Endif
26. cFormulaName = '{' + cFormulaName + '}'
27.
28. If Pcount() < 2
29. =MessageBox('Must supply two parameters!', 0, 'SetFormulaValues Error')
30. Return .F.
31. Endif
32.
33. lFound = .F.
34. cValue = ''
35. With This.crReport.FormulaFields
36. For x = 1 to .Count
37. If .Item(x).Name = cFormulaName
38. lFound = .T.
39. Exit For
40. Endif
41. EndFor
42. If lFound
43. *
44. * Massage the value based on type
45. *
46. Do Case
47. Case VarType(xValue) = 'C'
48. cValue = "'" + xValue + "'"
49. Case VarType(xValue) = 'D'
50. cValue = "#" + Dtoc(xValue) + "#"
51. Case VarType(xValue) = 'T'
52. cValue = "'" + Ttoc(xValue, 1) + "'"
53. Case VarType(xValue) = 'N'
54. cValue = xValue
55. Case VarType(xValue) = 'Y'
56. cValue = "'" + Str(Mton(xValue), 15, 4) + "'"
57. Case VarType(xValue) = 'L'
58. cValue = Iif(xValue, 'True', 'False')
59. Otherwise
60. =MessageBox('Unrecognized type: ' + Type('xValue'), 0,'SetFormulaValues Error')
61. lFound = .F.
62. Endcase
63. .Item(x).Text = cValue
64. Else
65. =MessageBox('FormulaName ' + cFormulaName + ' not found in this report', 0, 'SetFormulaValues Error')
66. Endif
67. EndWith
68.
69. Return lFound
70.
71. ENDPROC
72.
73. PROCEDURE printreport
74. Lparameters lGetPrinter, nCopies, lCollated, nStartPage, nStopPage
75. *
76. * lGetPrinter .F. does not prompt for printer information, .T. does
77. * nCopies Number of copies to print - default is 1
78. * lCollated .T. collates, .F. does not
79. * nStartPage Only included if specified
80. * nStopPage Only included if specified
81. *
82. Local cPrinter
83. cPrinter = ''
84.
85. If VarType(nCopies) = 'L'
86. nCopies = 1
87. Endif
88. If VarType(nStopPage) = 'N' and VarType(nStartPage) = 'L'
89. nStartPage = 1
90. Endif
91. *
92. * Choose a printer if indicated
93. *
94. *!* If lGetPrinter
95. *!* cPrinter = GetPrinter()
96. *!* Endif
97.
98. *!* With This
99. *!* If !Empty(cPrinter)
100. *!* .crReport.SelectPrinter('', cPrinter, '')
101. *!* Endif
102. *!* .crReport.ReadRecords()
103. *!* .crreport.PaperOrientation = this.crpaperorientation
104. *!*
105. *!* Do Case
106. *!* Case VarType(nStartPage) = 'N' and VarType(nStopPage) = 'L'
107. *!* .crReport.PrintOut(lGetPrinter, nCopies, lCollated, nStartPage)
108. *!* Case VarType(nStartPage) = 'N' and VarType(nStopPage) = 'N'
109. *!* .crReport.PrintOut(lGetPrinter, nCopies, lCollated, nStartPage, nStopPage)
110. *!* Otherwise
111. *!* .crReport.PrintOut(lGetPrinter, nCopies, lCollated)
112. *!* Endcase
113. *!* Endwith
114.
115. With This
116.
117. .crreport.PrinterSetup(0)
118. .crReport.ReadRecords()
119. .crreport.PaperOrientation = this.crpaperorientation
120.
121. *!* Do Case
122. *!* Case VarType(nStartPage) = 'N' and VarType(nStopPage) = 'L'
123. *!* .crReport.PrintOut(lGetPrinter, nCopies, lCollated, nStartPage)
124. *!* Case VarType(nStartPage) = 'N' and VarType(nStopPage) = 'N'
125. *!* .crReport.PrintOut(lGetPrinter, nCopies, lCollated, nStartPage, nStopPage)
126. *!* Otherwise
127. *!* .crReport.PrintOut(lGetPrinter, nCopies, lCollated)
128. *!* ENDCASE
129.
130. Do Case
131. Case VarType(nStartPage) = 'N' and VarType(nStopPage) = 'L'
132. .crReport.PrintOut(.T., nCopies, lCollated, nStartPage)
133. Case VarType(nStartPage) = 'N' and VarType(nStopPage) = 'N'
134. .crReport.PrintOut(.T., nCopies, lCollated, nStartPage, nStopPage)
135. Otherwise
136. .crReport.PrintOut(.T., nCopies, lCollated)
137. Endcase
138.
139. Endwith
140.
141.
142.
143. ENDPROC
144.
145. PROCEDURE open
146. *
147. * ReportFileName needs to be fully qualified!
148. *
149.
150. If File(This.ReportFileName)
151. This.crReport = This.crApplication.OpenReport(This.ReportFileName, 1)
152. Else
153. =messagebox('Report ' + This.ReportFileName + ' not found',0,'Not Found')
154. Return .F.
155. ENDIF
156.
157.
158. ENDPROC
159.
160. PROCEDURE previewreport
161. Lparameters cCaption
162. *
163. * cCaption Caption to display at top of preview window
164. *
165.
166. If Empty(cCaption)
167. cCaption = 'Previewing ' + This.ReportFileName
168. Endif
169.
170. This.oPreview = CreateObject('Preview')
171. If Type('This.oPreview') # 'O'
172. =MessageBox('Error initializing Preview form', 0, 'Crystal Objects Error')
173. Return .F.
174. Endif
175.
176. With This
177. .crReport.ReadRecords()
178. .oPreview.Caption = cCaption
179. .oPreview.Report = .crReport && Pass the report object
180. .oPreview.DisplayReport() && Show the report
181. .oPreview.Show(1) && Show the form as modal
182. Endwith
183.
184. This.oPreview = Null
185.
186. ENDPROC
187.
188. PROCEDURE startoutlook
189. Local lOk
190. lOk = .T.
191.
192. This.oOutlook = CreateObject("Outlook.Application") && Wake up Outlook
193. If Type('This.oOutlook') # 'O'
194. =MessageBox('Outlook 2000 not found!', 0, 'OutLook 2000 Error')
195. lOk = .F.
196. Endif
197. This.oSpace = This.oOutlook.GetNameSpace("MAPI") && Set up the NameSpace
198. If Type('This.oSpace') # 'O'
199. =MessageBox('Outlook 2000 MAPI error!', 0, 'OutLook 2000 Error')
200. lOk = .F.
201. Endif
202. This.oDrafts = This.oSpace.GetDefaultFolder(16) && Get the Drafts folder object
203. If Type('This.oDrafts') # 'O'
204. =MessageBox('Outlook 2000: Cannot find your Drafts folder!', 0,'OutLook 2000 Error')
205. lOk = .F.
206. Endif
207.
208. Return lOk
209.
210. ENDPROC
211.
212. PROCEDURE buildemail
213. Lparameters cFormat, cAddress, cSubject
214. *
215. * Purpose: Export the report and then add it to a email stored in the Drafts folder of Outlook 2000.
216. * Placing the email in the Drafts folder allows us to attach multiple reports to the same
217. * message without worrying about Outlook sending the message before we are ready.
218. *
219. *
220. * cFormat Format type based on following table:
221. * A Adobe Acrobat
222. * C Comma Separated Value
223. * E Microsoft Excel
224. * L Lotus 1-2-3
225. * R Crystal Reports 8.X
226. * T Paginated Text
227. * W Microsoft Word
228. * X Rich Text
229. * cAddress Address of the recipient - separate multiple address with semi-colons.
230. * cSubject Subject line of the message
231. *
232. * Default format to Excel if not otherwise specified
233. *
234.
235. If VarType(cFormat) # 'C'
236. cFormat = 'E'
237. Endif
238. If VarType(cAddress) # 'C'
239. =MessageBox('Address not specified - email not built', 0, 'Crystal Objects Error')
240. Return .F.
241. Endif
242. If VarType(cSubject) # 'C'
243. cSubject = 'A Crystal Report for you!'
244. Endif
245. If !(INLIST(cFormat,'A','C','E','L','R','T','W','X'))
246. cFormat = 'E'
247. Endif
248.
249. *
250. * Build the email and attach the report to the email address supplied
251. *
252. Local cFileName, oMsg
253. *
254. * Export the report first
255. *
256. cFileName = This.ExportReport(cFormat)
257. *
258. * Add new message to Outlook (if not there for this email address already)
259. *
260. oMsg = This.oDrafts.Items.Find("[To] = '" + Alltrim(cAddress) +
261. If IsNull(oMsg)
262. oMsg = This.oDrafts.Items.Add() && Create a new message
263. If VarType(oMsg) # 'O'
264. =MessageBox('Outlook 2000: Cannot create email message!', 0, 'Crystal Objects Error')
265. Return .F.
266. Endif
267. oMsg.Subject = cSubject
268. oMsg.To = cAddress
269. Endif
270. *
271. * Attach the report to either the new message or the existing message
272. * Parameters to call are: File name, Type (1=copy the file to message), Position in message, Display text
273. *
274. This.nAttachmentPosition = This.nAttachmentPosition + 10
275. oMsg.Attachments.Add(cFileName, 1, This.nAttachmentPosition, cFileName)
276. oMsg.Save()
277.
278. Return .T.
279. ENDPROC
280.
281. PROCEDURE exportreport
282. Lparameters cFormat,cName
283. *
284. * cFormat Format type based on following table:
285. * A Adobe Acrobat
286. * C Comma Separated Value
287. * E Microsoft Excel
288. * L Lotus 1-2-3
289. * R Crystal Reports 8.X
290. * T Paginated Text
291. * W Microsoft Word
292. * X Rich Text
293. *
294. * Default format to Excel if not otherwise specified
295. *
296.
297. If VarType(cFormat) # 'C'
298. cFormat = 'E'
299. Endif
300. If !(INLIST(cFormat,'A','C','E','L','R','T','W','X'))
301. cFormat = 'E'
302. Endif
303.
304. Local oExportOptions, cExtension, cFileName
305. *
306. * Write the report out as a document of their choosing
307. *
308. With This
309. oExportOptions = .crReport.ExportOptions
310. .nCounter = .nCounter + 1
311.
312. IF PARAMETERS() = 2
313. cFilename = cName
314. ELSE
315. cFileName = Left(.ReportFileName, At(".", .ReportFileName) - 1) + ;
316. Alltrim(Str(.nCounter))
317. cFileName = Proper(cFileName)
318. ENDIF PARAMETERS() = 2
319. ENDWITH
320.
321. With oExportOptions
322. .DestinationType = 1 && Disk file
323. *
324. * Set the format type based on passed parameter
325. *
326. Do Case
327. Case cFormat = 'A' && Acrobat PDF
328. .FormatType = 31
329. .PDFExportAllPages = .T.
330. cExtension = 'pdf'
331.
332. CASE cFormat = 'C' && CSV
333. .FormatType = 5
334. cExtension = 'csv'
335.
336. Case cFormat = 'E' && Excel 5
337. .FormatType = 21
338. .ExcelTabHasColumnHeadings = .T.
339. .ExcelUseWorksheetFunctions = .T. && Use functions for subtotals
340. cExtension = 'xls'
341.
342. Case cFormat = 'L' && Lotus 1-2-3
343. .FormatType = 11
344. cExtension = 'wks'
345.
346. Case cFormat = 'R' && Crystal Report
347. .FormatType = 1
348. cExtension = 'rpt'
349.
350. Case cFormat = 'T' && Text
351. .FormatType = 10
352. .NumberOfLinesPerPage = 60
353. cExtension = 'txt'
354.
355. Case cFormat = 'W' && Microsoft Word
356. .FormatType = 14
357. cExtension = 'doc'
358.
359. Case cFormat = 'X' && RTF
360. .FormatType = 35
361. cExtension = 'rtf'
362. ENDCASE
363.
364. IF PARAMETERS() = 2
365. .DiskFileName = ALLTRIM(cFileName)+"."+cExtension
366. ELSE
367. .DiskFileName = PUTFILE("Export As",cFileName,cExtension)
368. ENDIF PARAMETERS() = 2
369.
370. *!* .DiskFileName = cFileName + cExtension
371. EndWith
372. *
373. * Do not prompt the user for information...
374. *
375.
376. This.crReport.Export(.F.)
377.
378. Return oExportOptions.DiskFileName
379. ENDPROC
380.
381. PROCEDURE sendemail
382. Lparameters cFileBody
383. *
384. * Purpose: Send emails previously built with the BuildEmail method
385. *
386. * cFileBody Full path and filespec of a TEXT file to read into the body of the message
387. * Example: 'c:\messagebody.txt'
388. *
389. * Loop through the Drafts folder and send all the messages stored there, after adding the body if specified.
390. *
391. Local oMsg, crlf, cBody, hFile
392.
393. If VarType(cFileBody) # 'C'
394. cFileBody = ''
395. Else
396. If !File(cFileBody)
397. cFileBody = ''
398. Endif
399. Endif
400. With This
401. Do While .T.
402. If .oDrafts.Items.Count = 0
403. Exit
404. Endif
405. oMsg = .oDrafts.Items(1)
406. If !Empty(cFileBody)
407. crlf = Chr(13) + Chr(10)
408. cBody = ''
409. If File(cFileBody)
410. hFile = Fopen(cFileBody)
411. If hFile # -1
412. .nAttachmentPosition = .nAttachmentPosition + 10
413. cBody = Space(.nAttachmentPosition)
414. Do While !Feof(hFile)
415. cBody = cBody + crlf + Fgets(hFile)
416. Enddo
417. =Fclose(hFile)
418. Endif
419. Endif
420. cBody = cBody + crlf
421. *
422. * Load the body of the email
423. *
424. oMsg.Body = cBody
425. Endif
426. oMsg.Save()
427. oMsg.Send()
428. Enddo
429. Endwith
430. ENDPROC
431.
432. PROCEDURE changedatasource
433. Lparameters cNewODBC, cTableName, cNewTableName
434.
435. * cNewODBC ODBC DSN to use for this run
436. * cTableName Name of the table this is to be applied to
437. * cNewTableName New name of table if appropriate
438. *
439. Local nItems, lSuccess
440.
441. lSuccess = .F.
442. If VarType(cNewODBC) # 'C' or VarType(cTableName) # 'C'
443. Return lSuccess
444. Endif
445. If Empty(cNewTableName)
446. cNewTableName = cTableName
447. Endif
448. *
449. * Ensure '.dsn' is not included - also remember the ODBC name is CaSe SeNsItIvE
450. *
451. cNewODBC = StrTran(cNewODBC, '.dsn', '')
452.
453. nItems = This.crReport.Database.Tables.Count
454.
455. For nX = 1 to nItems
456. If This.crReport.Database.Tables.Item(nX).Location = cTableName
457. This.crReport.Database.Tables.Item(1).SetLogOnInfo(cNewODBC,"", "", "")
458. This.crReport.Database.Tables.Item(1).Location = cNewTableName
459. lSuccess = .T.
460. Exit For
461. Endif
462. Endfor
463. Return lSuccess
464. ENDPROC
465.
466. PROCEDURE changetablename
467. Lparameters cReportData, cReplaceWith
468. *
469. * cReportData Table name as specified in the report file.
470. * cReplaceWith Table name to use during this run of the report.
471. *
472. Local nItems
473.
474. nItems = This.crReport.Database.Tables.Count
475. IF nItems = 1
476. This.crReport.Database.Tables.Item(1).Location = cReplaceWith
477. ELSE && nItems != 1
478. For nX = 1 to nItems
479. If This.crReport.Database.Tables.Item(nX).Location = cReportData
480. This.crReport.Database.Tables.Item(nX).Location = cReplaceWith
481. Exit For
482. Endif
483. ENDFOR
484. ENDIF nItems = 1
485.
486. ENDPROC
487.
488. PROCEDURE Init
489. *
490. * Copyright (c) 2001 Midwest Software Development Corporation
491. * All Rights Reserved.
492. *
493. * Guarantee: It works for me - it should work for you. If it doesn't, fix it and then tell me what
494. * you did so I can improve mine. Thank you.
495. *
496. * Direct all inquiries, comments and/or suggestions to me via the VPM NG (not priority) or reach me
497. * at mark@msdcweb.com
498. *
499.
500. If !('CRYSTALOBJECTS' $ Set('CLASSLIB'))
501. Set Classlib to CrystalObjects additive
502. Endif
503. This.crApplication = CreateObject('CrystalRuntime.Application')
504. If Type('This.crApplication') # 'O'
505. =MessageBox('Error initializing Crystal Reports Runtime Application', 0, 'Crystal Objects Error')
506. Return .F.
507. Endif
508.
509. This.crReport = CreateObject('CrystalRuntime.Report')
510. If Type('This.crReport') # 'O'
511. =MessageBox('Error initializing Crystal Reports Runtime', 0,'Crystal Objects Error')
512. This.crApplication = .F.
513. Return .F.
514. Endif
515.
516. ENDPROC
* Number of SCX/VCX records for class: 1
* Property Descriptions *
1. reportfilename -- Name of current Crystal Report.
2. crreport -- Crystal Report Object.
3. crapplication -- Crystal Report Runtime Application Object.
4. opreview -- Object reference to the preview form.
5. ooutlook -- Outlook 2000 application object.
6. ospace -- Outlook 2000 Namespace object.
7. odrafts -- Outlook 2000 Drafts folder.
8. ncounter -- Used to separate multiple reports of the same name when exporting.
9. nattachmentposition -- Used to separate attachments in the body of the email so the email appears correctly to the recipient.
10. crarea -- Crystal Report Area Object
11. crprintwindowoptions -- The various print window options.
12. -- rpaperorientation
13. *setformulavalues -- Pass data from VFP into a Crystal Report Formula Object.
14. *printreport -- Print the current report.
15. *open -- Open a report.
16. *previewreport -- Preview the report in the preview form.
17. *startoutlook -- Instantiates Outlook 2000.
18. *buildemail -- Build email for the current report.
19. *exportreport -- Export a Crystal Report in a variety of formats.
20. *sendemail -- Send emails previously built with BuildEmail()
21. *changedatasource -- Change the data source of a report at runtime.
22. *changetablename -- Change the name of a table used within a report at runtime.
* Scale Mode: Pixels
* Object: preview BaseClass: form [SubClassed from: (form)]
* Report Previewer
* For WINDOWS platform, TimeStamp: 03/15/02 13:05:10 , Rec No: 2,
Unique Id: _0J90SC8JF (Class)
* Properties *
DataSession = 2
Top = 11
Left = 18
DoCreate = .T.
Caption = "Report Preview"
WindowType = 1
Name = "preview"

* Methods *
1. PROCEDURE displayreport
2. * Set the report source
3. If VarType(ThisForm.Report) # 'O'
4. =MessageBox('Report Preview Error: No Report Specified', 64, ThisForm.Caption)
5. Return
6. Endif
7.
8. With ThisForm
9. .crViewer.Visible = .T.
10. If .Left # 0 or .WindowState # 2
11. .Left = 0
12. .WindowState = 2 && Maximize the window
13. .Resize()
14. Endif
15. EndWith
16.
17. ThisForm.crViewer.ReportSource = ThisForm.Report
18.
19.
20. * Set the viewer to view the report
21. ThisForm.crViewer.ViewReport()
22. * Set the zoom level to fit the page
23. * to the width of the viewer window
24. ThisForm.crViewer.Zoom(1)
25.
26. ENDPROC
27.
28. PROCEDURE Resize
29. *!* ThisForm.Mwresize1.Resize(This)
30. With ThisForm.crViewer
31. .Width = ThisForm.Width
32. .Height = ThisForm.Height - 14
33. .Left = 0
34. EndWith
35.
36. ENDPROC
37.
38. PROCEDURE Init
39. *
40. * Copyright (c) 2001 Midwest Software Development Corporation
41. * All Rights Reserved.
42. *
43. * Direct all inquiries, comments and/or suggestions to me at mark@msdcweb.com
44. *
45. * Turn on dual interface support
46. *
47. =Sys(2333, 1)
48. *
49. * The viewer object must be added at runtime otherwise you will get errors
50. *
51.
52. ThisForm.AddObject("crViewer", "olecontrol", "CRViewer.CRViewer.8.0")
53. With ThisForm.crViewer
54. .Left = 0
55. .Height = ThisForm.Height - 14
56. .Top = 0
57. .Width = ThisForm.Width
58. .EnableCloseButton= .T.
59. .EnableDrillDown= .T.
60. .EnableExportButton= .T.
61. .EnableGroupTree= .T.
62. .EnableNavigationControls= .T.
63. .EnablePopupMenu= .T.
64. .EnablePrintButton= .T.
65. .EnableProgressControl= .T.
66. .EnableRefreshButton= .T.
67. .EnableSearchControl= .T.
68. .EnableSearchExpertButton= .T.
69. .EnableSelectExpertButton= .T.
70. .EnableStopButton= .T.
71. .EnableToolbar= .T.
72. .EnableZoomControl= .T.
73. EndWith
74.
75. ENDPROC
* Number of SCX/VCX records for class: 2
* Property Descriptions *
1. report -- Crystal Reports Report Object
2. *displayreport -- Show the report in the viewer control

* Scale Mode: Pixels

* Properties *
Arial, 0, 9, 5, 15, 12, 32, 3, 0
Objects contained in preview
* Object: Mwresize1 BaseClass: image [SubClassed from:
mwresize.vcx(mwresize)]
* For WINDOWS platform, TimeStamp: 03/15/02 13:05:10 , Rec No: 3,
Unique Id: _0J90SC8JO
* Properties *
Picture = ..\resize.bmp
Height = 16
Left = 432
Top = 300
Width = 16
Name = "Mwresize1"
* No Method code for object
Previous
Reply
Map
View

Click here to load this message in the networking platform