简体   繁体   中英

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). 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; 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 .

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.

For that I have created MS access file, created one linked table (in which i have project and view names).

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.

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 :

select * from projectname$view_name;

ex: 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:

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. Then you can loop through the array using VBA For-Next code block, running DoCmd.RunSQL command.

Alternatively, instead of DoCmd.RunSQL you may use Execute() function on VBA Database object, like shown below:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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