create new tables from query on another table.

Richard U

Active Member
Joined
Feb 14, 2006
Messages
406
Office Version
  1. 365
  2. 2016
  3. 2010
  4. 2007
Platform
  1. Windows
I have a table that contains multiple entries for each country in the world.

I need to break this up to one table for each country. I'm very rusty at this. I know what I have to do, just can't remember how.

I know that I need to have a query to return the country codes...

Code:
Select distinct Country_Code 
from Country_Table
which will give me the list of countries I need.

I know I need to wrap a statement around it...

Code:
Select into NewTable....

But that's where I get lost. I know I need to iterate down the results of the country codes query, but I've forgotten how to do that.
 
I just used the following code to export the records for each country in a table called 'country' to separate Excel files.
Code:
Sub ExportCountries()
Dim db As DAO.Database
Dim rstCountry As DAO.Recordset
Dim qdfExport As DAO.QueryDef
Dim strSQL As String

    Set db = CurrentDb

    Set rstCountry = db.OpenRecordset("SELECT DISTINCT CountryName FROM Country ORDER BY CountryName")

    rstCountry.MoveFirst

    While Not rstCountry.EOF
        strSQL = "SELECT * FROM Country WHERE CountryName = '" & rstCountry.Fields(0).Value & "'"

        Set qdfExport = db.CreateQueryDef("qryExport", strSQL)

        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryExport", "C:\Test\" & rstCountry.Fields(0).Value, True
        db.QueryDefs.Delete "qryExport"
        rstCountry.MoveNext
    Wend
    
    rstCountry.Close
    
    Set rstCountry = Nothing
    
    Set db = Nothing
    
End Sub
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I was on my way out the door earlier, so didn't get a chance to give more detail at the time, but it looks like Norie came to the rescue, so hopefully you have what you need now.
 
Upvote 0

Forum statistics

Threads
1,221,798
Messages
6,162,027
Members
451,737
Latest member
MRASHLEY

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