Hi, I am using vb2005 with access database and I have a crystal report which I want to preview and print only the last saved record. How can I do it?
Can any one help me with sample code

Recommended Answers

All 4 Replies

I'm not all that familiar with Crystal Reports, but I'm guessing that you still need to query the database.
Here's how:
SELECT TOP 1 * FROM (SELECT * FROM <table> ORDER BY id ASC)

This syntax is necessary in an access database in order for the query to be successful.

I know this syntax will work with query in database. But in the case of Crystal report how it will be used? I need breif explanation.
Thanks for trying to reply me.

Assuming that you're using the CrystalReportViewer.

Imports System.Data.OleDb
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data

Public Class Reports
    Private objRpt As New CrystalReport1

    Public Sub ReportLastRecord()
        Dim con As OleDbConnection
        Dim connectionString As String = "<your connectionstring>"
        Dim sql As String
        sql = "SELECT TOP 1 * FROM (SELECT * FROM <table> ORDER BY id ASC)"

        con = New OleDbConnection(connectionString)
        con.Open()

        Dim da As New OleDbDataAdapter(sql, con)
        Dim dt As New DataTable("LastRecord")
        da.Fill(dt)
        con.Close()

        objRpt.SetDataSource(dt)
        CrystalReportViewer1.ReportSource = objRpt
        CrystalReportViewer1.Refresh()
    End Sub
End Class

Thanks I will try and let you know.

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.