简体   繁体   English

从访问中导出一个字段到Excel中的特定单元格

[英]Export one field from access to specific cell in excel

I want to place a name from a table/query to a specific cell from access.我想将表/查询中的名称放置到访问中的特定单元格。 I have a check sheet with lots of data in other cells that at the top has the persons name at the top.我有一个检查表,在其他单元格中有很多数据,顶部有人名。 I would like a button that can be clicked that will open THAT file and insert the desired name into it via VBA.我想要一个可以单击的按钮,该按钮将打开该文件并通过 VBA 将所需的名称插入其中。 I'm aware on how to do this with VBA to a word doc with bookmarks, but now sure how to define the 'bookmark' for a cell in excel.我知道如何使用 VBA 对带有书签的 word 文档执行此操作,但现在确定如何为 Excel 中的单元格定义“书签”。

This is what I have so far.这是我到目前为止。

Private Sub Cmdopentarget1_Click()
Dim xl As Excel.Application
Dim wbTarget1 As Workbook
Dim qdfquerytest As QueryDef
Dim rsquerytest As Recordset
Set qdfquerytest = CurrentDb.QueryDefs("query1")
Set qdfsecondquery = CurrentDb.QueryDefs("query2")
Set rsquerytest = qdfquerytest.OpenRecordset()
Set rssecondquery = qdfsecondquery.OpenRecordset()
Set xl = CreateObject("Excel.application")
xl.Visible = True
Set wbTarget1 = xl.Workbooks.Open("C:\Users\Owner\Dropbox\Target1.xlsx")
'Set wbTarget1 = xl.Workbooks.Open("C:\Users\Dennis\Dropbox\Target1.xlsx")
'wbTarget1.Worksheets("Sheet1").Cells(8, 1).CopyFromRecordset rsquerytest
wbTarget1.Worksheets("Sheet1").Range("A1").CopyFromRecordset rsquerytest
wbTarget1.Worksheets("Sheet1").Range("C5").CopyFromRecordset rssecondquery

It works, but the query I have returns TWO fields, Fullname and the position, I need to only have the FullName in the excel cell.它有效,但我的查询返回两个字段,全名和位置,我只需要在 excel 单元格中有全名。

You can use你可以使用

[variable or object] = rsquerytest(0).Value

to pull the value from a record.从记录中提取值。 The recordset starts at position 0. If you wanted the second column, you would use rsquerytest(1).Value .记录集从位置 0 开始。如果您想要第二列,您可以使用rsquerytest(1).Value

source 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM