How to show field/row serial number on detail data report without use data environment?

simply view like this


|Number|WO Number|WO DATE|ERROR DESCRIPTION|LOCATION|EXECUTOR NAME|
___________________________________________________________________
| 1 |N0.000001|10-01-1|
| 2 |N0.000002|10-01-2|
| 3 |N0.000003|10-01-3|
| 4 |N0.000004|10-01-4|
| 5 |N0.000005|10-01-5|
___________________________________________________________________

Dani AI

Generated

Three practical ways to get a printed serial (row) number on a VB6 Data Report without the Data Environment — tying together points from , , and .

A. Server-side (best for stable ordering / large sets)

SELECT ROW_NUMBER() OVER (ORDER BY [WO Number]) AS Serial,
       [WO Number], [WO DATE], [ERROR DESCRIPTION], [LOCATION], [EXECUTOR NAME]
FROM   WorkOrders
ORDER  BY [WO Number];

Uses SQL Server 2005+; the report just binds to the query and the Serial column prints correctly even when paging or re-sorting. For MS Access, create a temp table with an AutoNumber or use a DCount expression (works but is slow on large sets).

B. Runtime ADO copy (works when no DB-side change is possible)
Create a temporary/disconnected ADO recordset that has the original fields plus a Serial field, copy rows from the source recordset while incrementing a counter, then set DataReport.DataSource to that new recordset. This is the runtime approach demonstrated by . Important notes: initialize the counter before the loop, preserve the same sort used on the report, make field names/types match the report controls, and call Refresh/Show on the DataReport. If numbers look wrong after sorting, generate them server-side instead.

C. Use a reporting tool or control
’s ListView is fine for on-screen lists (printing requires extra code). For production-ready printable reports consider Crystal/ActiveReports which support running totals/row numbers without manual copying (as suggested).

Choose server-side numbering for correctness with paging/sorting; use the ADO-copy trick for quick, code-only solutions.

Recommended Answers

All 6 Replies

You do not have to use Data Report, you can use something like the Listview Control etc. The Listview can sort data in columns etc.

i see
but I want to view the report, and it can be printed.
and I usually use Data Deport.

but yours is okay. I will be learn.

If you use the VB6 Data Report, you have to use the Data Environment, because the report needs to be connected to your database, which is only available through the data environment.

You can use third party tools as well like Crystal Reports etc. The easiest in your situation would be the Listview, which you can still view AND print as in a normal data report. Just google Listview in VB6, there is plenty of tutorials on the Listview with code samples.

Good luck.

Let Me try, so thanks v much :)

This will do the trick :)

dim rsReport as ADODB.Recordset
dim n as integer

'create temporary recordset
Set rsReport = New ADODB.Recordset
With rsReport
Set .ActiveConnection = Nothing
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
    
.Fields.Append "Serial", adBigInt 'show your serial
.Fields.Append "Column1", adVarChar, 50
.Fields.Append "Column2", adVarChar, 50

.Open
End With

'copy your recordset, assume rs is your active recordset
Do While Not rs.EOF
rsReport.AddNew
rsReport.Fields("Serial") = n
rsReport.Fields("Column1") = rs.Fields(0)
rsReport.Fields("Column2") = rs.Fields(1)

'generate your serial here
n=n+1
rs.MoveNext
loop

'finally, bound to your datareport
With DataReport1
.Hide
Set .DataSource = rsReport

'bound your report on detail section here
With .Sections("Section3")
  
end with

.Refresh
.Show
end with

cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.