make table from query

Access Beginner

Active Member
Joined
Nov 8, 2010
Messages
311
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I found this bit of code when I did a search here for VBA to make table from a query
Code:
Private Sub doit()

DoCmd.SetWarnings False
DoCmd.OpenQuery "test"
DoCmd.SetWarnings True

End Sub
But when I run this it just opens the query, should there be something else to turn the query into a table.

Please forgive my ignorance, I'm still on a steep learning curve.

Using Access 2007
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi

That will only make a table if your query "test" is a make-table query. Is that the case? You specify a make-table query by going into query design view and selecting the "Make Table" icon.

HTH
DK
 
Upvote 0
Hi

Thanks for your reply.

No the query "test"is not a make-table query.

What I was hoping to find was VBA, that would turn a query into a table for me. This query is based on another query and by making it a table, I hope to speed the linking to an Excel spreadsheet up.

In way of explaining. I have 20 files ( some have over 2 million records) and I create a number of queries based on these tables. These files are weekly. So I was hoping to have this query "test" as table, this way the query would only be run once, and I can then link my Excel spreadsheets to this newly created table. Once new data arrives, I can than run this VBA again and start the process over again.

The query "test"is also the base of a few other queries as well, so this will help me greatly.

I hope I have been clear.

Cheers
 
Upvote 0
This code will create a table based on the specified query and will delete the table if it already exists. Therefore, please use a copy of your database to test this

Code:
Sub CreateTableFromQuery()

    Dim sQueryName As String
    Dim sNewTableName As String
    Dim sSQL As String

    On Error GoTo ErrHandler

    'Change these to suit your requirements
    sNewTableName = "MY_NEW_TABLE"
    sQueryName = "TEST"


    On Error Resume Next
    sSQL = "DROP TABLE " & sNewTableName
    CurrentDb.Execute sSQL
    
    If Err.Number <> 0 Then
        MsgBox "Could not delete table " & sNewTableName & vbCr & Err.Description
    Else
        On Error GoTo ErrHandler

        'Create the table based on records from sQueryName
        sSQL = "SELECT * INTO " & sNewTableName & " FROM " & sQueryName

        CurrentDb.Execute sSQL
    End If

    Exit Sub

ErrHandler:
    MsgBox Err.Description, vbExclamation, "Error"

End Sub

Does that do what you want?

HTH
DK
 
Last edited:
Upvote 0
Thanks for your help.

I will test this tomorrow at work and let you know. I dont have any actual files at home to test it with.

Thanks again, your assistance is much appreciated.

Cheers
 
Upvote 0

Forum statistics

Threads
1,224,509
Messages
6,179,192
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top