来自 Access 数据库的 Vb.Net 预览报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21086304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:26:04  来源:igfitidea点击:

Vb.Net preview report from access database

vb.netms-access

提问by Hilal Al-Rajhi

I am using visual studio 2010 to develop my desktop application and ms access database. I have already designed reports in access database and they are working perfectly. Now my concern is that is it possible to use these access reports in my Vb.Net application?

我正在使用 Visual Studio 2010 来开发我的桌面应用程序和 ms access 数据库。我已经在访问数据库中设计了报告,并且它们运行良好。现在我担心的是可以在我的 Vb.Net 应用程序中使用这些访问报告吗?

I need this because it would be much easier to distribute the software instead of using crystal report where I will need to install a run time machine for crystal reports.

我需要这个,因为它会更容易分发软件而不是使用水晶报告,我需要为水晶报告安装运行时机器。

回答by ?s??? ????

It's possible to do but it's a horrible implementation. You basically have to launch Access, open the database file in Access, then navigate down and launch the report. There is no way to just "view" the report inside your app. It will only show inside Access.

这是可能的,但这是一个可怕的实现。您基本上必须启动 Access,在 Access 中打开数据库文件,然后向下导航并启动报告。无法仅在您的应用程序内“查看”报告。它只会显示在 Access 中。

I think your best option is to use ReportViewercontrol if you don't want to use Crystal Reports.

如果您不想使用 Crystal Reports,我认为最好的选择是使用ReportViewer控件。

回答by user3850146

I found this: https://support.microsoft.com/kb/317113?wa=wsignin1.0

我发现了这个:https: //support.microsoft.com/kb/317113?wa=wsignin1.0

To preview or to print an Access report, you call the OpenReport method of the DoCmd object. When you call OpenReport, one of the arguments that you pass determines whether the report is previewed on the screen, or whether it is sent to the printer:

若要预览或打印 Access 报表,请调用 DoCmd 对象的 OpenReport 方法。当您调用 OpenReport 时,您传递的参数之一决定了报告是在屏幕上预览还是发送到打印机:

' Preview a report named Sales:
oAccess.DoCmd.OpenReport(ReportName:="Sales", View:=Access.AcView.acViewPreview)
' Print a report named Sales:
oAccess.DoCmd.OpenReport(ReportName:="Sales", View:=Access.AcView.acViewNormal)

But I haven't been able to find much info/help regarding OpenReport method or DoCmd object.

但是我找不到关于 OpenReport 方法或 DoCmd 对象的太多信息/帮助。

回答by MICHAEL_AML

I am not a proffesional developer, but after long research it's worked for my project.

我不是专业的开发人员,但经过长时间的研究,它对我的​​项目有用。

I would like to open directly from MS Access, my existing reports and then to open these from not default installation folder path (different for each one pc). The third requirement was to open more than one report (Not all together).

我想直接从 MS Access 打开我现有的报告,然后从非默认安装文件夹路径(每台电脑不同)打开这些报告。第三个要求是打开多个报告(不是全部一起)。

Design: Form8.vb [Design]

设计: Form8.vb [设计]

vb.Net Code:

vb.Net 代码:

Imports System.ComponentModel
Imports Microsoft.Office.Interop.Access

Public Class Form8

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim oAccess As New Microsoft.Office.Interop.Access.Application()
    Dim CurDir As String = System.AppDomain.CurrentDomain.BaseDirectory
    Dim FPath As String = CurDir & "\YourDatabase.accdb"

    oAccess.Visible = True
    oAccess.OpenCurrentDatabase(FPath)

If ComboBox1.Text = "Your text" Then
        oAccess.DoCmd.OpenReport(ReportName:="Your Report Name", View:=AcView.acViewPreview)
ElseIf ComboBox1.Text = "Your text2" Then
        oAccess.DoCmd.OpenReport(ReportName:="Your Report Name", View:=AcView.acViewPreview)
ElseIf ComboBox1.Text = "Your text3" Then
        oAccess.DoCmd.OpenReport(ReportName:="Your Report Name", View:=AcView.acViewPreview)
End If

End Sub
End Class

Replace YourDatabase with YourDatabaseName, Replace Your text,Your text1,Your text2,Your text3 with YourText, Replace Your Report Name with the Name of your report Name, As needed.

将 YourDatabase 替换为 YourDatabaseName,将您的文本、您的文本 1、您的文本 2、您的文本 3 替换为您的文本,将您的报告名称替换为您的报告名称,根据需要。

Finally, in order for this solution to work, you need:

最后,为了使此解决方案起作用,您需要:

1) Your (Conn.open) connection to look like this:

1) 您的 (Conn.open) 连接如下所示:

Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = |DataDirectory|\Your DB.accdb"

2) In Solution Explorer, Your database to include in your project or to your setup project, to be installed with your project. like this:

2) 在解决方案资源管理器中,您的数据库要包含在您的项目或安装项目中,与您的项目一起安装。像这样:

Solution Explorer [image]

解决方案资源管理器 [图片]

SetUp Project link

设置项目链接

3) At server explorer, Data Connection the (Sourse) Database file name, must be with the full path name e.g like this:

3)在服务器资源管理器中,数据连接(源)数据库文件名必须使用完整路径名,例如:

C:\Users\User\Desktop\YourDatabase.accdb, and the connection String at the Properties like this:

C:\Users\User\Desktop\YourDatabase.accdb,以及属性中的连接字符串,如下所示:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source = "C:\Users\User\Desktop\YourDatabase.accdb".

Provider=Microsoft.ACE.OLEDB.12.0;Data Source = "C:\Users\User\Desktop\YourDatabase.accdb"。

I hope this helps!!!

我希望这有帮助!!!

回答by Doug

The following code:

以下代码:

Preview a report named Sales:
oAccess.DoCmd.OpenReport(ReportName:="Sales", View:=Access.AcView.acViewPreview)
Print a report named Sales:
oAccess.DoCmd.OpenReport(ReportName:="Sales", View:=Access.AcView.acViewNormal)

But I haven't been able to find much info/help regarding OpenReport method or DoCmd object.

但是我找不到关于 OpenReport 方法或 DoCmd 对象的太多信息/帮助。

This will only work if you are using VB.Net to automate the Access application. This will open the Access application and allow you to view or print the reports just as if you were running Access directly. However, you need to have the Access application (not just the .mdb or .accdb file) on the computer that is running the VB.Net application.

这仅在您使用 VB.Net 自动化 Access 应用程序时才有效。这将打开 Access 应用程序并允许您查看或打印报告,就像您直接运行 Access 一样。但是,您需要在运行 VB.Net 应用程序的计算机上安装 Access 应用程序(不仅仅是 .mdb 或 .accdb 文件)。

I have not found any way to utilize the Access reports without having Access on the computers running the VB.Net application. I'm looking into using Crystal Reports for the application I'm working on now.

我还没有找到在运行 VB.Net 应用程序的计算机上没有 Access 的情况下使用 Access 报告的任何方法。我正在考虑将 Crystal Reports 用于我现在正在处理的应用程序。