Connecting Tech Pros Worldwide Forums | Help | Site Map

Need Help..Set record source, controls and display data in a report using VBA Code.

Newbie
 
Join Date: Mar 2007
Location: Perl of the Indian Ocean
Posts: 10
#1: Apr 19 '07
I have created a report using sample codes taken by the forum. one problem is that the report displays the field/column names in the table in columnar format(field names display in rows.) but i want to be able to display filed names and its relevant data in tabular format in the repor but the code itself just displays only the fields in rows and another problem is that, to set the record source. the way i set the record source property of the report gives an error saying that "Object variable or With block variable not set". so here's the sample code of my dynamic report. can u guys help me out to sove this small problem. thanx.



Expand|Select|Wrap|Line Numbers
  1. ------------------------------------------------------
  2. Public Sub PreviewReport_Click()
  3.  
  4. createtNewReport
  5.  
  6. End Sub
  7. ------------------------------------------------------
  8.  
  9. Public Sub createtNewReport()
  10.  
  11. Dim txtNew As TextBox
  12. Dim lblNew As Label
  13.  
  14. Dim rpt As Report
  15. Dim sRptName As String
  16. Dim fldData As Field
  17. Dim lngTop As Long
  18. Dim lngLeft As Long
  19. Dim dbCurr As Database
  20. Dim rsRecordset As Recordset
  21.  
  22. lngTop = 0
  23. lngLeft = 0
  24.  
  25.  
  26. -----------------------
  27. 'set report's record source propery 
  28. 'rpt.RecordSource = "X"
  29. 'it gives an error saying that"Object variable or With block variable not set" 
  30. 'with out this part the report's being created. 
  31. ----------------
  32.  
  33. Set dbCurr = DBEngine.Workspaces(0).Databases(0)
  34. Set rsRecordset = dbCurr.OpenRecordset("X")
  35.  
  36.  
  37. sRptName = "ICTA_PMIS_REPORT"
  38.  
  39. DoCmd.OpenReport sRptName, acViewDesign
  40.  
  41. For Each fldData In rsRecordset.Fields
  42.  
  43. 'create controls
  44. Set txtNew = CreateReportControl(sRptName, acTextBox, acDetail, , fldData.Name, lngLeft + 1500, lngTop)
  45. txtNew.SizeToFit
  46.  
  47.  
  48. Set lblNew = CreateReportControl(sRptName, acLabel, acDetail, txtNew.Name, fldData.Name, lngLeft, lngTop, 1400, txtNew.Height)
  49.       lblNew.SizeToFit
  50.  
  51.  
  52. 'Increment top calue for next control
  53. lngTop = lngTop + txtNew.Height + 25
  54.  
  55. Next
  56.  
  57. DoCmd.Close acReport, sRptName, acSaveYes
  58.  
  59.  
  60. '------------------------------------------------
  61. exit_createtNewReport:
  62. Exit Sub
  63.  
  64. err_createtNewReport:
  65. MsgBox Err.Description
  66. Resume exit_createtNewReport
  67.  
  68. End Sub
  69.  
  70.  

nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#2: Apr 23 '07

re: Need Help..Set record source, controls and display data in a report using VBA Code.


For dynamic reports based on a crosstable query with variable fields I use the following preparation and VBA code.
To start, doing this you need to place the fields "coded" in the report.
The column headings should be called "lblCol1", "lblCol2", "lblCol3", etc.
The "detail" fields should be called "Col1", "Col2", "Col3", etc.

The report query has two rowheader columns and a Total column, therefor the first field is effectively column 4 (count starts at 0 so I used intI=3) but this could differ for you.

Make sure that the number of Columns is not bigger as the number placed. The programcode has no protection against that !

The OpenReport code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Report_Open(Cancel As Integer)
  3. Dim intI As Integer
  4.  
  5. Dim rs As Recordset
  6.  
  7. Set rs = CurrentDb.OpenRecordset(Me.RecordSource)
  8.  
  9. 'Place headers
  10. For intI = 3 To rs.Fields.Count - 1
  11. Me("lblCol" & intI - 1).Caption = rs.Fields(intI).Name
  12. Next intI
  13.  
  14. 'Place correct controlsource
  15. For intI = 3 To rs.Fields.Count - 1
  16. Me("Col" & intI - 1).ControlSource = rs.Fields(intI).Name
  17. Next intI
  18.  
  19. 'Place Total field
  20. Me.ColTotal.ControlSource = "=SUM([" & rs.Fields(2).Name & "])"
  21.  
  22. End Sub
  23.  
The report query has two rowheader columns and a Total column, therefor the first field is effectively column 4 (count starts at 0 so I used intI=3) but it could differ for you.

Nic;o)
Reply