Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2006, 12:21 AM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Spliting a data table

Hi Guys

I hope one of you can help me; I need to split a data table by using my site id variable. I can go and write a normal query and output to excel, but the problem is then I need to write 300 different queries! Does anyone of you maybe have a VB code that I can use? I would greatly appreciate this.

Thanks
Hennie


 
Old July 19th, 2006, 12:24 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

I am sure we could find something. Coul you give an example of the query that you would like to run, and what parts of it would change?


Mike
EchoVue.com
 
Old July 19th, 2006, 02:16 AM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike

I've got a table with 37 columns and +- 2000 records. The variables that I want use is the following: Country, Site_ID (This will be the part that will change) disc_num qc_num etc.

In this table I've got about 300 different site numbers and would like to sent an excel file to each site with it's own data in the excel file.
I'm not so clued up with VB but I think the code will probably run in a loop and the filename will have to be concatenated with the site number.

Thanks a lot for helping me! Please let me know if you need any more info.

Kind regards,
Hennie

 
Old July 19th, 2006, 03:17 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Hennie -

Sorry, it's 2am over here, and I need to crash. I'll see what I can put together for you in the morning.

Mike

Mike
EchoVue.com
 
Old July 19th, 2006, 03:24 AM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No problem thanks!!

 
Old July 19th, 2006, 10:48 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Hennie,

I think I have it...

Here is the code - an explanation is below...

Let me know if you get stuck on any part of it.

Code:
Private Sub CommandExport_Click()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim strSQL As String
    Dim path As String

    Dim str1Sql As QueryDef
    Dim strCrt As String
    Dim strDt As String

    Set cn = Application.CurrentProject.Connection
    Set rs = New ADODB.Recordset
    Set dbs = CurrentDb
    path = "C:\DataExport\"

    strSQL = "SELECT DISTINCT Site_ID FROM MyTable"
    rs.Open strSQL, cn

    If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst
        Do While Not rs.EOF
            strCrt = rs.Fields(0)
            strSQL = "SELECT * FROM MyTable WHERE Site_ID=" & strCrt

            Set qdf = dbs.CreateQueryDef("qryExport", strSQL)
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryExport", path & "SiteData-" & strCrt & ".xls", True
            DoCmd.DeleteObject acQuery, "qryExport"
            Set qdf = Nothing
            rs.MoveNext
        Loop
    End If
End Sub
OK - Explanation...

What you want to do, is create a button on your form, and then add add a Click Event. Select 'Code Builder' and then past the code inside of the procedure. View the code below on the Wrox site to see the comments.

Private Sub CommandExport_Click()
    This part sets up all the variables and objects that you need
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim strSQL As String
    Dim path As String

    Dim str1Sql As QueryDef
    Dim strCrt As String
    Dim strDt As String

    Set cn = Application.CurrentProject.Connection
    Set rs = New ADODB.Recordset
    Set dbs = CurrentDb
    Set your export path here
    path = "C:\DataExport\"

    This creates a set of all the Site_IDs in the table. Change MyTable to your table name
    strSQL = "SELECT DISTINCT Site_ID FROM MyTable"
    rs.Open strSQL, cn

    If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst
        For each unique id, the code below creates a query in Access called 'qryExport'. This query holds all the records for the specific ID. It then exports that query to Excel, and deletes the query, before moving onto the next Site_ID. You will need to change MyTable to your table Name, and make adjustments to the FileName if needed.
        Do While Not rs.EOF
            strCrt = rs.Fields(0)
            strSQL = "SELECT * FROM MyTable WHERE Site_ID=" & strCrt

            Set qdf = dbs.CreateQueryDef("qryExport", strSQL)
            Right now it will export to 'SiteData-55.xls' for Site_ID = 55
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryExport", path & "SiteData-" & strCrt & ".xls", True
            DoCmd.DeleteObject acQuery, "qryExport"
            Set qdf = Nothing
            rs.MoveNext
        Loop
    End If
End Sub

All the best, and Welcome to the forum.

Mike

Mike
EchoVue.com
 
Old July 20th, 2006, 01:52 AM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike

Thanks a lot for the code I will try the code and let you know.

 
Old July 21st, 2006, 06:45 AM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike

This code that you send me works like a dream! Thanks a million!

Kind regards,
Hennie






Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating one table with data from another table dirtdog22 Access VBA 1 January 21st, 2008 04:41 PM
spliting database in record level dhua SQL Server 2000 5 January 30th, 2007 07:32 AM
Spliting a record Tachyophan Access VBA 3 October 25th, 2006 03:10 AM
How to Update one table with other table data? ramk_1978 SQL Language 2 May 26th, 2006 12:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.