如何附加文件从一个MS Access数据库的内容复制到一个VBA变量? [英] How to copy the contents of an attached file from an MS Access DB into a VBA variable?

查看:187
本文介绍了如何附加文件从一个MS Access数据库的内容复制到一个VBA变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景资料: 我不是很聪明的用VBA,或为此事访问,但我有一个创建一个文件VBA脚本(一个KML是具体的,但是这没有多大关系的,我的问题),在用户计算机上并写入它使用数据库链接到记录变量。因此:

Background Information: I am not very savvy with VBA, or Access for that matter, but I have a VBA script that creates a file (a KML to be specific, but this won't matter much for my question) on the users computer and writes to it using variables that link to records in the database. As such:

Dim MyDB As Database
Dim MyRS As Recordset
Dim QryOrTblDef As String
Dim TestFile As Integer

    QryOrTblDef = "Table1" 
    Set MyDB = CurrentDb
    Set MyRS = MyDB.OpenRecordset(QryOrTblDef)
    TestFile = FreeFile

    Open "C:\Testing.txt"
    Print #TestFile, "Generic Stuff"
    Print #TestFile, MyRS.Fields(0)

等。

我的情况: 我有一个非常大的字符串,我要添加到可变要打印到另一个文件(与多边形顶点的一个大的坐标列表的文本文件)(KML文件,在上述例子中说明)。我希望能添加包含坐标作为附件数据类型的Access数据库这个文本文件,并将其内容复制到一个变量,在上面的脚本中使用。

My Situation: I have a very large string(a text document with a large list of polygon vertex coordinates) that I want to add to a variable to be printed to another file (a KML file, noted in the above example). I was hoping to add this text file containing coordinates as an attachment datatype to the Access database and copy its contents into a variable to be used in the above script.

我的问题: 有没有一种方法可以让我从一个附加的文本文件(附于MS Access数据库的一个领域内的附件数据类型)来访问和复制数据到一个变量,这样我可以在VBA脚本中使用它?

My Question: Is there a way I can access and copy the data from an attached text file (attached as an attachment data type within a field of an MS Access database) into a variable so that I can use it in a VBA script?

我发现: 我有麻烦finidng对这个话题我想主要是因为我没有什么关键字来进行搜索的知识信息,但我能找到某人code在一个论坛上,ozgrid,这似乎是接近我想要做的。尽管它只是从文本文件拉动盘而不是一个连接到数据库。

What I have found: I am having trouble finidng information on this topic I think mainly because I do not have the knowledge of what keywords to be searching for, but I was able to find someones code on a forum, "ozgrid", that seems to be close to what I want to do. Though it is just pulling from a text file on disk rather than one attached to the database.

code,在一个文本文件中创建一个函数来访问数据:

Code from above mentioned forum that creates a function to access data in a text file:

Sub Test() 

    Dim strText As String 

    strText = GetFileContent("C:\temp\x.txt") 
    MsgBox strText 

End Sub 

Function GetFileContent(Name As String) As String 
    Dim intUnit As Integer 

    On Error Goto ErrGetFileContent 
    intUnit = FreeFile 
    Open Name For Input As intUnit 
    GetFileContent = Input(LOF(intUnit), intUnit) 
ErrGetFileContent: 
    Close intUnit 
    Exit Function 
End Function 

任何帮助是AP preciated。谢谢。

Any help here is appreciated. Thanks.

推荐答案

我有些疑惑,为什么一个备忘录数据类型不适合,如果你存储纯文本,甚至是有组织的文字表。话虽这么说,一种方法是输出到磁盘,并读入一个字符串。

I am a little puzzled as to why a memo data type does not suit if you are storing pure text, or even a table for organized text. That being said, one way is to output to disk and read into a string.

''Ref: Windows Script Host Object Model
Dim fs As New FileSystemObject
Dim ts As TextStream
Dim rs As DAO.Recordset, rsA As DAO.Recordset
Dim sFilePath As String
Dim sFileText As String

    sFilePath = "z:\docs\"

    Set rs = CurrentDb.OpenRecordset("maintable")
    Set rsA = rs.Fields("aAttachment").Value

    ''File exists
    If Not fs.FileExists(sFilePath & rsA.Fields("FileName").Value) Then
        ''It will save with the existing FileName, but you can assign a new name
        rsA.Fields("FileData").SaveToFile sFilePath
    End If

    Set ts = fs.OpenTextFile(sFilePath _
           & rsA.Fields("FileName").Value, ForReading)

    sFileText = ts.ReadAll

另请参阅: http://msdn.microsoft.com/en -us /图书馆/办公室/ ff835669.aspx

这篇关于如何附加文件从一个MS Access数据库的内容复制到一个VBA变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆