简体   繁体   English

带循环的ms访问查询以创建多个表

[英]ms access query with loop to create multiple tables

I have one table in oracle database - which will have two columns (project name, view name). 我在oracle数据库中有一张表-它将有两列(项目名称,视图名称)。 In that table when you filter project name, we will get all view names related to that project, based on those view names again we need to write query like select * from projectname$viewaname; 在该表中,当您过滤项目名称时,我们将获得与该项目相关的所有视图名称,再次基于这些视图名称,我们需要编写查询,例如select * from projectname$viewaname; to fetch that view related data. 来获取与该视图相关的数据。

Doing this manually is taking long time for each project. 对于每个项目,手动执行此操作会花费很长时间。 So my idea is to create MS ACCESS database to create tables for selected project and export them as excel files to C:\\temp folder . 因此,我的想法是创建MS ACCESS数据库以为所选项目创建表,并将其作为excel文件导出到C:\\temp folder

I need your help to create multiple tables in one go (using query/passthrough query or any other option) in MS Access fetching data from oracle database. 我需要您的帮助,以便在MS Access中一次性创建多个表(使用查询/直通查询或任何其他选项),以便从oracle数据库获取数据。

For that I have created MS access file, created one linked table (in which i have project and view names). 为此,我创建了MS Access文件,创建了一个链接表(其中有项目和视图名称)。

After that I have created one form, using project field as combo box from linked table and updated settings like, this form should be opened at start-up. 之后,我创建了一个表单,使用项目字段作为链接表中的组合框并更新了设置,该表单应在启动时打开。

When I open access file, automatically this form is opening and asking me to enter oracle database user id and password - after entering credentials, combo box is updating and I can select my project in that list. 当我打开访问文件时,此表格会自动打开并要求我输入oracle数据库用户ID和密码-输入凭据后,组合框将更新,我可以在该列表中选择我的项目。

After that, I have created one query using main table and applied filter condition based on the selection in the form. 之后,我使用主表创建了一个查询,并根据表单中的选择应用了过滤条件。 Now I got results like project and view name for the end user selected project. 现在,我得到了最终用户选择的项目的结果,例如项目和视图名称。

I need your help like, 我需要你的帮助

now we have data in table like below. 现在我们在下表中有数据。

Project | Viewname
A       | A1  
A       | A2
A       | A3
A       | A4
A       | A5

SQL query to see individual view data is : SQL查询中查看单个视图的数据是:

select * from projectname$view_name;

ex: select * from A$A1; 例如: select * from A$A1;

project name, view name and no of rows(views), columns in views are dynamic - will change based on project. 项目名称,视图名称和行(视图)数,视图中的列是动态的-将根据项目而变化。

I need your help to create multiple tables(one per one view) dynamically - Please suggest me the best option. 我需要您的帮助来动态创建多个表(每个视图一个)-请建议我最好的选择。

Regards, Murali 问候,穆拉利

You have asked multiple questions, so the answer is structured correspondingly: 您已经问了多个问题,所以答案的结构是相应的:

In order to create MS Access Table using VBA refer to the following sample code snippet: 为了使用VBA创建MS Access Table,请参考以下示例代码片段:

Public Sub CreateTableVBA() 
    Dim SQL As String 
    SQL = "CREATE TABLE ThisTable " & "(FirstName CHAR, LastName CHAR);" 
    DoCmd.RunSQL SQL 
End Sub

In order to create multiple Tables you should have an array of Table names and corresponding array of SQL statements, like the one shown above. 为了创建多个表,您应该有一个表名数组和相应的SQL语句数组,就像上面显示的那样。 Then you can loop through the array using VBA For-Next code block, running DoCmd.RunSQL command. 然后,您可以使用VBA For-Next代码块循环运行该数组,并运行DoCmd.RunSQL命令。

Alternatively, instead of DoCmd.RunSQL you may use Execute() function on VBA Database object, like shown below: 另外,也可以在VBA Database对象上使用Execute()函数代替DoCmd.RunSQL ,如下所示:

Sub CreateTableXSQL() 

    Dim dbs As Database 

    ' include the path to MyDb.mdb on your computer. 
    Set dbs = OpenDatabase([Path to MyDb.mdb]) 

    ' create a table SQL with two text fields. 
    dbs.Execute "CREATE TABLE ThisTable " & "(FirstName CHAR, LastName CHAR);" 

    dbs.Close 

End Sub

Hope this may help. 希望这可能有所帮助。

Consider iteratively looping through the project/view name query in a recordset and creating the pass-through query that then exports to an Excel spreadsheet. 考虑迭代遍历记录集中的项目/视图名称查询,并创建传递查询,然后将其导出到Excel电子表格。

Public Sub ImportOracleProjectViews()
    Dim db As Database, rst As Recordset, qdef As QueryDef
    Dim constr As String, strSQL As String, mkTBL As String

    Set db = CurrentDb
    ' ENTER QUERY HOLDING PROJECT AND VIEW NAMES '
    Set rst = db.OpenRecordset("QUERYNAME")

    ' DELETE TEMP QUERYDEF IF EXISTS '
    For Each qdef In db.QueryDefs
        If qdef.Name = "temp" Then
            db.QueryDefs.Delete ("temp")
        End If
    Next qdef

    If rst.RecordCount = 0 Then Exit Sub
    rst.MoveLast: rst.MoveFirst

    ' LOOP THROUGH EACH RECORD OF RECORDSET ' 
    Do While Not rst.EOF

        ' SQL STATEMENTS '
        strSQL = "SELECT * FROM " & rst!projectname & "$" & rst!viewname

        ' ORACLE CONNECTION STRING '
        constr = "ODBC;Driver={Microsoft ODBC for Oracle};Server=myServerAddress;" _
                              & "Uid=myUsername;Pwd=myPassword;"

        ' PASS THRU QUERY '
        Set qdef = db.CreateQueryDef("temp")
        qdef.Connect = constr
        qdef.SQL = strSQL
        qdef.Close

        ' EXPORT TO MS EXCEL SPREADSHEET '
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, temp, _
           "C:\Path\To\Output\Folder\" & rst!projectname & "_" & rst!viewname & ".xlsx", _
           True

        rst.MoveNext
    Loop

    rst.Close

    Set rst = Nothing
    Set qdef = Nothing
    Set db = Nothing

    MsgBox "Successfully imported views to local tables and " _
                    & "Excel spreadsheets!", vbInformation

End Sub

Resources 资源

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

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