简体   繁体   English

访问-使用数组数据创建SQL查询,然后导出到Excel

[英]Access - use array data to create a SQL query and then export to Excel

I am trying to get multiple usernames from a single text box, then to search a database to list all sections a username has access to, and then export that data to an Excel spreadsheet. 我试图从单个文本框中获取多个用户名,然后搜索数据库以列出用户名可以访问的所有部分,然后将该数据导出到Excel电子表格。 A single spread sheet would be nice, but multiple spread sheets would be fine. 单个电子表格会很好,但是多个电子表格就可以了。

I have managed to get the data to an array, and have the code to export to an Excel spreadsheet, but my knowledge at the moment of vba doesn't go far enough to be able to link this together, so it uses the SQL statement to search through the tables to get the information of the access each user has and then somehow either store it and dump it to a single spread sheet, or just save it to a new spreadsheet each time is searches the database. 我已经设法将数据保存到数组中,并且可以将代码导出到Excel电子表格,但是我对vba的了解还不足以将其链接在一起,因此它使用了SQL语句搜索表以获取每个用户具有的访问信息,然后以某种方式存储它并将其转储到单个电子表格中,或者每次搜索数据库时将其保存到新的电子表格中。

I have looked around but can't see how the ways I have seen can be translated into what I want. 我环顾四周,但看不到如何将我看到的方式转化为我想要的。 I guess you'd have to put the SQL results back into an Array and then do a dump to excel somehow? 我想您必须将SQL结果放回Array中,然后进行转储以某种方式表现出色?

I don't even know if this is possible to do in vba. 我什至不知道这是否可以在vba中完成。

Code that I currently have, which I know won't work in it's current state: 我当前拥有的代码,我知道在当前状态下无法使用:

Private Sub Command12_Click()
Dim strSQL As String
Dim strQry As String
Dim selecteduser As String
Dim db As DAO.Database
Dim Qdf As QueryDef

Users = Me.Text13

Dim usernames() As String

usernames() = Split(Users, ",")

strSQL = "SELECT tblPra.praNo, tblFolder.folder, tblFolder.fullTitle FROM "&_ 
     "tblPra INNER JOIN (tblFolder INNER JOIN tblRelationship ON " &_
     "tblFolder.folderID = tblRelationship.folderID) ON " &_
     "tblPra.praID = tblRelationship.praID " &_
     "WHERE (((tblPra.praNo)='" & usernames & "'));"
strQry = "tempuser"

Set db = CurrentDb
Set Qdf = db.CreateQueryDef(strQry, strSQL)

On Error Resume Next
    DoCmd.DeleteObject acQuery, "strQry"

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
   strQry, "C:\Users\name\Desktop\test.xls", True

DoCmd.DeleteObject acQuery, strQry

End Sub

Any help, advice or point in the right direction would be greatly appreciated! 任何帮助,建议或朝正确方向的方向,将不胜感激!

You could use an IN clause: 您可以使用IN子句:

usernames() = Split(Users, ",")

strSQL = "SELECT tblPra.praNo, tblFolder.folder, tblFolder.fullTitle FROM "&_ 
     "tblPra INNER JOIN (tblFolder INNER JOIN tblRelationship ON " &_
     "tblFolder.folderID = tblRelationship.folderID) ON " &_
     "tblPra.praID = tblRelationship.praID " &_
     "WHERE (((tblPra.praNo) IN ('" & Join(usernames, "','") & "')));"

For export data to existing Excel template you can use code like this: 要将数据导出到现有的Excel模板,可以使用如下代码:

Dim usernames() As String
Dim strSQL As String
Dim xlApp As Excel.Application
Dim xlWork As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rst As Recordset

usernames() = Split(Users, ",")

strSQL = "SELECT tblPra.praNo, tblFolder.folder, tblFolder.fullTitle FROM "&_
     "tblPra INNER JOIN (tblFolder INNER JOIN tblRelationship ON " &_
     "tblFolder.folderID = tblRelationship.folderID) ON " &_
     "tblPra.praID = tblRelationship.praID " &_
     "WHERE (((tblPra.praNo) IN ('" & Join(usernames, "','") & "')));"


Set rst = CurrentDb.OpenRecordset(strSQL)

Set xlApp = CreateObject("Excel.Application")
Set xlWork = xlApp.Workbooks.Open("c:\temp\MyExcelTemplate.xlsx")
Set xlSheet = xlWork.Sheets("Sheet1")

xlSheet.Range("A1").CopyFromRecordset rst

Using Excel objects xlApp , xlWork , xlSheet will allow you keep template formatting, you will be able to create new worksheets, workbooks, make multiple data transfers, copy/set formatting, validation etc - use all Excel features. 使用Excel对象xlAppxlWorkxlSheet可以保持模板格式,可以创建新的工作表,工作簿,进行多次数据传输,复制/设置格式,验证等-使用所有Excel功能。 CopyFromRecordset works very fast. CopyFromRecordset工作速度非常快。

This code requires reference to Microsoft Excel 15.0 Object Library 此代码需要引用Microsoft Excel 15.0对象库

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

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