473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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.  
Apr 19 '07 #1
1 2485
nico5038
3,080 Expert 2GB
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)
Apr 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
9
by: Susan Bricker | last post by:
Greetings. I am having trouble populating text data that represents data in my table. Here's the setup: There is a People Table (name, address, phone, ...) peopleID = autonumber key There...
12
by: Wadim Grasza | last post by:
I want to store and display (on a form or a report) multiple pictures per record in an access database. The pictures are not stored within the database. They are stored as files and the database...
1
by: kkrizl | last post by:
I have a form that displays general information about an alarm permit location. There's a subform that shows detailed information about burglar alarms that have gone off at the location. When a...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
2
by: Bill | last post by:
I have a 200 record database that includes a date/time field, AnnivDate, for a wedding anniversary. AnnivDate has nulls and some incorrect year data. I have been creating the Access database...
14
by: optimum | last post by:
hi i am kind of new to programming, i have managed to create a simple payroll database in C++, but now i am trying to convert in into C but i am keep getting error, can anyone help me convert this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.