database 如何附加和查看pdf文档以访问数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19730993/
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-08 09:06:58  来源:igfitidea点击:

How to attach and view pdf documents to access database

databasevbams-accesspdf

提问by MiniMite

I have a very simple database in access, but for each record i need to attach a scanned in document (probably pdf). What is the best way to do this, the database should not just link to a file on the pc, but should copy and keep the file with it, meaning if the original file goes missing the database is moved or copied, the file should still be accessable from within the Database. Is This possible? and what is the easiest way of doing it? If is should i can write a macro, i just dont know where to start. and also when i display a report of the table, i would like to just see thumbnails of the documents. Thank you.

我有一个非常简单的数据库可供访问,但是对于每条记录,我都需要附上扫描的文档(可能是 pdf)。最好的方法是什么,数据库不应该只是链接到电脑上的文件,而是应该复制并保留文件,这意味着如果原始文件丢失,数据库被移动或复制,文件应该仍然可以从数据库内访问。这可能吗?最简单的方法是什么?如果是我可以写一个宏,我只是不知道从哪里开始。而且当我显示表格的报告时,我只想查看文档的缩略图。谢谢你。

回答by Chris Rolliston

As the other answerers have noted, storing file data inside a database table can be a questionable practice. That said, I wouldn't personally rule it out, though if you are going to take that option, I'd strongly suggest splitting out the file data into its own table in its own backend file. For example:

正如其他回答者所指出的,将文件数据存储在数据库表中可能是一种有问题的做法。也就是说,我个人不会排除它,但如果您打算采用该选项,我强烈建议将文件数据拆分到其自己的后端文件中的自己的表中。例如:

  • Create a new database file called Scanned files.mdb(or Scanned files.accdb).
  • Add a single table called Scanswith fields such as FileID(AutoNumber, primary key), MainTableID(matches whatever is the primary key of the main table in the main database file), FileName(Text), FileExt(Text) and FileData('OLE object', really just a BLOB - don't actually use OLE Objects because they will bloat the database horribly).
  • Back in the frontend, add a reference to Scansas a linked table.
  • Use a bit of VBA to upload and extract files from the Scanstable (if you're interested in the mechanics of this, post a separate question).
  • Use the VBA Shellroutine (if you must) or ShellExecutefrom the Windows API (= the better option IMO) to open extracted data.
  • 创建一个名为Scanned files.mdb(或Scanned files.accdb)的新数据库文件。
  • 添加一个Scans带有字段FileID(自动编号,主键),MainTableID(匹配主数据库文件中主表的主键),FileName(文本),FileExt(文本)和FileData('OLE 对象',实际上只是BLOB - 实际上不要使用 OLE 对象,因为它们会使数据库膨胀得可怕)。
  • 回到前端,添加一个引用Scans作为链接表。
  • 使用一点 VBA 从Scans表中上传和提取文件(如果您对此机制感兴趣,请发布一个单独的问题)。
  • 使用 VBAShell例程(如果必须)或ShellExecute从 Windows API(= 更好的选项 IMO)打开提取的数据。

If you are using the newer ACCDB format, then you have the 'attachment' field type available as smk081 suggests. This basically does most of the above steps for you, however doing things 'by hand' gives you greater flexibilty - for example, it allows giving each file a 'DateScanned' or 'DateEffective' field.

如果您使用的是较新的 ACCDB 格式,那么您可以使用 smk081 建议的“附件”字段类型。这基本上为您完成了上述大部分步骤,但是“手动”操作可以为您提供更大的灵活性 - 例如,它允许为每个文件提供一个“DateScanned”或“DateEffective”字段。

That said, your requirement for thumbnails will require explicit coding whatever option you take. It mightbe possible to leverage the Windows file previewing API, though I'd be certain thumbnails are a definite requirement before investigating this - Access VBA is powerful enough to encourage attempts at complex solutions, but frequently not clean and modern enough to allow fulfilling them in a particularly maintainable fashion.

也就是说,您对缩略图的要求将需要对您采取的任何选项进行显式编码。这也许可以利用Windows的文件预览API,但我可以肯定的缩略图是一个明确的要求,调查在此之前-访问VBA是足够强大,以鼓励在复杂的解决方案的尝试,但往往不干净,现代化,足以让他们满足以特别易于维护的方式。

回答by smk081

There is an Attachment type under Data Type when you go into Design View of your table. You can add an attachment field here. When you go into the Datasheet view of the table you can select this field for a particular row and a window will open for you to specify the attachment. This will cause your database to quickly grow in size if you add a lot of large attachments.

当您进入表格的设计视图时,数据类型下有一个附件类型。您可以在此处添加附件字段。当您进入表格的数据表视图时,您可以为特定行选择此字段,然后将打开一个窗口供您指定附件。如果您添加大量大附件,这将导致您的数据库快速增长。

回答by Johnny Bones

You can use an OLE field in a table, but I would really suggest you not use this approach. The database is going to be HUGE in no time, and you're going to regret it.

您可以在表中使用 OLE 字段,但我真的建议您不要使用这种方法。数据库很快就会变得巨大,你会后悔的。

Instead, you should consider adding a field that stores the path to the file, and keep the files in one folder on your network. Then you can use a SHELL() command to open the file. What's the difference between restoring an Access database and restoring PDF files if something goes wrong? This will keep your database at a manageable size and reduce the possibility of corruption.

相反,您应该考虑添加一个字段来存储文件的路径,并将文件保存在网络上的一个文件夹中。然后您可以使用 SHELL() 命令打开该文件。如果出现问题,还原 Access 数据库和还原 PDF 文件有什么区别?这将使您的数据库保持在可管理的大小并减少损坏的可能性。