MS Access - 如何通过记录计数将一个大表拆分为较小的表

时间:2013-08-14 20:25:09

标签: ms-access access-vba ms-access-2010

我在MS Access中有一个表有+ 17K的记录。我试图将该表分解为每个500条记录的小表。使用以下代码,我可以创建临时表,但我无法重置ID列的数量。原始表上的ID列是自动编号。我正在尝试重置临时表上的ID字段,以便我可以从1开始进行记录搜索,然后转到500.

我拥有的alter SQL不会将临时表的ID列更新/重置为1.有什么想法吗?

Function SplitTables_Actual()
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.Connection
Set cn = CurrentProject.Connection
Dim rowcount As Long
Dim tblcount As Integer
Dim i As Integer
SQL = "SELECT * INTO tmp_Flush_Actual FROM BIG_Table"
DoCmd.RunSQL SQL
SQL = "ALTER TABLE tmp_Flush_Actual ALTER COLUMN ID COUNTER(1,1)"
DoCmd.RunSQL SQL
SQL = "SELECT count(*) as rowcount from BIG_Table"
rs.Open SQL, cn
rowcount = rs!rowcount
rs.Close
tblcount = rowcount / 500 + 1
For i = 1 To tblcount
SQL = "SELECT * into tmp_flush_Actual" & i & " FROM tmp_Flush_Actual" & _
" WHERE ID <= 500*" & i
DoCmd.RunSQL SQL
SQL = "DELETE * FROM tmp_Flush_Actual" & _
" WHERE ID<= 500*" & i
DoCmd.RunSQL SQL
Next i

End Function

1 个答案:

答案 0 :(得分:0)

在初始查询的底线上,不要选择ID(自动编号)列。在初始临时表中选择所需的列,然后更改表以添加新的计数器列。我使用了COUNTER(1,1),这样每次创建临时表时,第一条记录都是1。

我添加了一个小块,将损坏的文件保存到文件夹中。我注释掉了错误处理,但取消注释以确保您的保存目录正常工作。

    Function SplitTables_RTPA_Actual()
    Dim rs As New ADODB.Recordset
    Dim cn As New ADODB.Connection
    Set cn = CurrentProject.Connection
    Dim rowcount As Long
    Dim tblcount As Integer
    Dim i As Integer
        'Just don't select the ID column
        SQL = "SELECT Company, Incurred_By, Transaction_Type, Format(Transaction_Date, 'mm/dd/yyyy'), Investment_ID, " & _
        "Task_ID, Charge_Code, Resource_ID, Role, Notes, Quantity INTO tmp_Flush_Tran_Actual FROM Actual_Transaction_Data"
    DoCmd.RunSQL SQL
        SQL = "ALTER TABLE tmp_Flush_Tran_Actual ADD COLUMN ID COUNTER(1,1)"
    DoCmd.RunSQL SQL
        SQL = "SELECT count(*) as rowcount from Actual_Transaction_Data"
    rs.Open SQL, cn
    rowcount = rs!rowcount
    rs.Close
    tblcount = rowcount / 100 + 1

    For i = 1 To tblcount
            'Create Temp Flush File
            SQL = "SELECT * into tmp_Flush_Tran_Actual" & i & " FROM tmp_Flush_Tran_Actual" & _
            " WHERE ID <=100*" & i
        DoCmd.RunSQL SQL
            SQL = "ALTER TABLE tmp_Flush_Tran_Actual" & i _
            & " DROP COLUMN ID;"
        DoCmd.RunSQL SQL

        'Delete 500 from Temp Flush File
            SQL = "DELETE * FROM tmp_Flush_Tran_Actual" & _
            " WHERE ID <=100*" & i
        DoCmd.RunSQL SQL

        'On Error GoTo ErrorHandler

        Dim strTable As String
        Dim strWorksheetPath As String

        'Location where you want to save the broken out files
        strWorksheetPath = "C:\YOUR TEMP FOLDER\TEST\"
        strWorksheetPath = strWorksheetPath & "Actual_Transactions" & i & ".xls"
        strTable = "tmp_Flush_Tran_Actual" & i

        DoCmd.TransferSpreadsheet transfertype:=acExport, spreadsheettype:=acSpreadsheetTypeExcel9, TableName:=strTable, FileName:=strWorksheetPath, hasfieldnames:=True

        'ErrorHandlerExit:
        '    Exit Function
        '    'Next i
        '
        'ErrorHandler:
        '    MsgBox "Error No: " & Err.Number _
        '    & "; Description: " & Err.Description
        '    Resume ErrorHandlerExit

    Next i

End Function