473,386 Members | 1,812 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,386 software developers and data experts.

Print values of recordset in report

I know this has to be a pretty basic function, and I have a goofy error somewhere that I am hoping someone here can point out. I am simply trying to loop through a recordset and display the values contained in a field "TAG". Code is

Expand|Select|Wrap|Line Numbers
  1. Private Sub Print_Report_Click()
  2. Dim db As DAO.Database
  3. Dim rpt As Report
  4. Dim rst As DAO.Recordset
  5. Dim fld As DAO.Field
  6. Dim sSQL As String
  7. Dim txtbx As Access.TextBox
  8. Dim lblnew As Access.Label
  9. Dim lngleft As Long
  10. Dim lngtop As Long
  11.  
  12.  
  13. sSQL = "SELECT TAG FROM ESGBKR"
  14.  
  15. Set db = CurrentDb
  16. Set rst = db.OpenRecordset(sSQL, dbOpenDynaset)
  17. Set rpt = CreateReport
  18. rpt.RecordSource = sSQL
  19.  
  20. 'Set columns
  21. Set lblnew = CreateReportControl(rpt.Name, acLabel, acDetail, , "TAG")
  22.  
  23. lngtop = lngtop + 200
  24. If Not (rst.EOF And rst.BOF) Then
  25.     rst.MoveFirst
  26. Do Until rst.EOF = True
  27.     For Each fld In rst.Fields
  28.    Set txtbx = CreateReportControl(rpt.Name, acTextBox, acDetail, fld.name, lngleft, lngtop)
  29.     txtbx.SizeToFit
  30.     lngtop = lngtop + 25 + txtbx.Height
  31.     Next fld
  32.  
  33.     rst.MoveNext
  34. Loop
  35. Else
  36. MsgBox "No Records Available"
  37. End If
  38.  
  39. DoCmd.OpenReport rpt.Name, acViewPreview
  40.  
  41. rst.Close
  42. Set rst = Nothing
  43. Set db = Nothing
  44. Set rpt = Nothing
  45.  
  46. End Sub
It seems to loop through my table, and give me the right amount of records (i.e., 5), but each record has the same value, the first one. So the table looks like this
Tag
X4-2A
X4-2B
X4-2C
X4-2D
X4-2E

I get a report that shows a column title "TAG" and then five values below that, each of which is "X4-2A". Any ideas what I have coded incorrectly to not get the correct values?

Thanks much
Nov 14 '13 #1
3 5267
zmbd
5,501 Expert Mod 4TB
Line 13:
Take copy that SQL and paste it into a normal query (open in the query designer, switch to SQL view, copy-n-paste). Run this query and see what records you are actually pulling.

This is also a fairly unusual means of creating a report.
Normally, the fields in the underlying record source would have all of this setup and then the report would do all of the pretty layout and formatting.

I would move the lines 27 to 31 above 26.
Eliminate the do-loop starting at line 26

See what you get then.


(here's a working example from MS of what I think OP is trying to accomplish and upon which I based my coding suggestions: Creating Access Reports Office 2000
One thing to Note here: ADODB is used as this is in ACC2000 and there was much gnashing of teeth about what type of record set to use back then; however, in ACC2007/2010 ADODB reference may not be set as the move appears to be back to DAO as a default; thus, instead of the ADODB, use DAO or go into the references and set the ADODB library.)
Nov 14 '13 #2
ADezii
8,834 Expert 8TB
  1. Aside from being a very unorthodox approach, you are missing a couple of key steps.
    1. You are looping thru the Recordset but the Height of the Detail Section does not allow you to see all five Values. The Height of the Detail Section needs to be adjusted (Code Line# 19).
    2. The Text Boxes are created but you have not defined their Control Source (Code Line# 31).
    3. The Text Boxes and associated Labels are bunched together. Either move the Label to the Form Header or set the width properly so they do not overlap.
  2. I made some revisions to hopefully point you in the right direction, but I still do not agree with your basic approach.
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim rpt As Report
  3. Dim rst As DAO.Recordset
  4. Dim fld As DAO.Field
  5. Dim sSQL As String
  6. Dim txtbx As Access.TextBox
  7. Dim lblnew As Access.Label
  8. Dim lngleft As Long
  9. Dim lngtop As Long
  10.  
  11. sSQL = "SELECT TAG FROM ESGBKR"
  12.  
  13. Set db = CurrentDb
  14. Set rst = db.OpenRecordset(sSQL, dbOpenDynaset)
  15. Set rpt = CreateReport
  16.  
  17. With rpt
  18.   .RecordSource = sSQL
  19.   .Section(acDetail).Height = 400
  20. End With
  21.  
  22. 'Set columns
  23. Set lblnew = CreateReportControl(rpt.Name, acLabel, acDetail, , "TAG")
  24.  
  25. lngtop = lngtop + 200
  26.  
  27. If Not (rst.EOF And rst.BOF) Then
  28.   Set txtbx = CreateReportControl(rpt.Name, acTextBox, acDetail, rst.Fields(0).Name, lngleft, lngtop)
  29.     With txtbx
  30.       .SizeToFit
  31.       .ControlSource = "TAG"
  32.     End With
  33.     lngtop = lngtop + 25 + txtbx.Height
  34.  
  35.     rst.MoveNext
  36. Else
  37.   MsgBox "No Records Available"
  38. End If
  39.  
  40. DoCmd.OpenReport rpt.Name, acViewPreview
  41.  
  42. rst.Close
  43. Set rst = Nothing
  44. Set db = Nothing
  45. Set rpt = Nothing
Nov 16 '13 #3
zmbd
5,501 Expert Mod 4TB
ADezii
Aside from being a very unorthodox approach, you are missing a couple of key steps
That means alot coming from ADezii!
I'm glad he took the time to review the code too... I missed quite a few things there!
Nov 18 '13 #4

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

Similar topics

2
by: ashtonn | last post by:
Hello, How do i print values returned by Py_BuildValue in Linux? PyObject *obj = Py_BuildValue("{s:i}", "Status", status); I need to print the Status value here -Thanks, Ashton
2
by: G | last post by:
I am trying to duplicate the printing of a shortcut-print of an access report in my NT Scheduled Tasks. I can now print the report manually very easily by right-clicking on a shortcut to the access...
0
by: JC Mugs | last post by:
I have an Access XP project that is to print checks. There are a couple of lines of fields that should not print when the values are 0. Such as Doz2 =0 then Item should not print, It is...
1
by: Richard Hollenbeck | last post by:
I noticed I can't push a value into a text box by saying something like, "txtThisTextBox = intSomeVariable * 0.5" because I get an run-time error saying I can't assign a value to this object....
3
by: Simone | last post by:
Hi, I'm a DB newbie and I've been having issues trying to print the record I am viewing in a form to a report. I have looked in groups all over and they all say to try basically the same thing...
1
by: Ray | last post by:
Hi all, Is it possible to print the crystal report directly in vb.net without previewing it? Thanks a lot, Ray
1
by: jj | last post by:
I created a dynamic report and the fields is displayed base on the query. The report contains about 34 fields so if the query has 34 fields, all 34 fields in the report shows up. But if the query...
7
by: itm | last post by:
I have a mail out to send to a group of owners with multiple accounts. I want to limit the number of accounts that print on the first page to 20. I want remaining accounts to print on a second...
4
by: wassimdaccache | last post by:
Dear experts, I would like to print a record set or fill all data on it on a specific report that I made Suppose that my record set is RS, I do have RS! and RS! Report Name : Customer...
9
by: Chinde | last post by:
I've googled this and I've come up with nothing. I'm using the code below to produce a recordset for a report. all seems to work just fine except when i try to print I get the error: "A custom...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.