从拉Acce​​ss数据库中的数据,以Word文档 [英] Pull data from Access database to Word document

查看:298
本文介绍了从拉Acce​​ss数据库中的数据,以Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据从Access数据库转移到一个Word文档。我已经执行使用Excel类似的程序,我c。使用访问代替Excel它需要$ C $。

I am attempting to transfer data from an Access database into a Word document. I have performed a similar procedure using Excel and I need to code it using Access instead of Excel.

Excel的code(对于一个比喻是这样的)

The Excel code (for an analogy is this)

Dim myworkbook As Excel.Workbook
Set myworkbook = GetObject("C:\Users\jn\Desktop\trial.xlsm")

然后

Dim excelstr As String
Excelstr = Application.Range("A1:A100").Find("aword").Offset(0,1).Value

我想不通,我拉的数据通过查找表中的字符串,并使用某种抵消程序,找到旁边被发现的字符串的字符串如何与Access做到这一点。

I can't figure out how to do this with Access where I pull in data by finding a string within a table and using some sort of offset procedure to find a string next to the string that was found.

推荐答案

我不知道什么是在访问你的表结构。所以我会假设是这样的:

I don't know what is your table structure in Access... So I will assume like this:

表将被命名为表1 ... 2个领域COL1和col2 ...和价值的,你将搜索(您的例子AWORD)是col1和相应的结果将在col2的..

The table will be named Table1... with 2 fields Col1 and Col2... and one of the value that you will search (your example "aword") is in Col1 and the corresponding result will be in Col2...

下面是code:

Public Function GetAccess(strData As String)
   Dim db As DAO.Database
   Dim rst As DAO.Recordset
   Dim strDB As String
   Dim strSQL As String
   Dim strResult As String

   'Change Table1, Col1, Col2 to your liking correspondingly
   strSQL = "Select Col2 from Table1 where Col1 = """ & strData & """"
   strDB = "C:\\Users\\jn\\Documents\\Trial.accdb"  'Change it to your database name

   Set db = OpenDatabase(strDB)
   Set rst = db.OpenRecordset(strSQL)
   If rst.RecordCount > 0 Then
      strResult = rst.Fields("Col2") 'Remember to change Col2 to your own Column name
   Else
      strResult = ""
   End If
   rst.Close
   db.Close

   Set rst = Nothing
   Set db = Nothing

   GetAccess = strResult
End Function

所以,如果你想找到的结果,这里是code调用上面的函数:

So if you want to find the result, here is the code to call the above function:

Dim strResult As String

strResult = GetAccess("aword")

这篇关于从拉Acce​​ss数据库中的数据,以Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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