从Excel

时间:2015-08-05 18:21:18

标签: excel-vba ms-access vba excel

我有一个包含所有相同字段的80个表的数据库。我正在尝试做的是在整个数据库中搜索特定数据。这是我以前用来获取数据的代码

strTable = "Table3"
user = frm1.Label2.Caption

Set con = CreateObject("ADODB.connection")
If Err.Number <> 0 Then
    MsgBox "Connection was not created!", vbCritical, "Connection Error"
    Exit Sub
End If
On Error GoTo 0
 'Open the connection.
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile

SQL = "SELECT* FROM " & strTable & " WHERE [JCI_Con] = '" & user & "' AND Time_out is null"

这是我正在使用的完整实际代码

Option Explicit

 Sub RunQuery14()
 Application.DisplayAlerts = False
Dim con         As Object
Dim rs          As Object
Dim AccessFile  As String
Dim strTable    As String
Dim SQL         As String
Dim i           As Integer
Dim user        As String

On Error GoTo Errhandler

Application.ScreenUpdating = False
Sheets("DATA").Cells.Clear
AccessFile = "H:\APPLICATIONS\SEAT AUDIT\DATABASE\trial.accdb"

strTable = "Table1"
user = "????"
'Create the ADODB connection object.
Set con = CreateObject("ADODB.connection")
'Check if the object was created.
If Err.Number <> 0 Then
    MsgBox "Connection was not created!", vbCritical, "Connection Error"
    Exit Sub
End If
On Error GoTo 0
 'Open the connection.
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
'Create the SQL statement to retrieve the data from table.
'Get the necessary information (first name etc.) for all the Canadian customers.
SQL = ""
SQL = SQL & " SELECT * FROM "
SQL = SQL & " ( "
SQL = SQL & " SELECT 'Zone1' AS MyTableName, * FROM Zone1 WHERE [Auditor_Name] = '" & user & "'"
SQL = SQL & " UNION ALL"
SQL = SQL & " SELECT 'Zone2' AS MyTableName, * FROM Zone2 WHERE [Auditor_Name] = '" & user & "'"
SQL = SQL & " ) AS First2Tables"

    On Error Resume Next
   'Create the ADODB recordset object.
    Set rs = CreateObject("ADODB.Recordset")
    'Check if the object was created.
If Err.Number <> 0 Then
    'Error! Release the objects and exit.
    Set rs = Nothing
    Set con = Nothing
    'Display an error message to the user.
    Exit Sub
End If
On Error GoTo 0
'Set thee cursor location.
rs.CursorLocation = 3 'adUseClient on early  binding
rs.CursorType = 1 'adOpenKeyset on early  binding

'Open the recordset.
rs.Open SQL, con

'Check if the recordet is empty.
If rs.EOF And rs.BOF Then
    'Close the recordet and the connection.
    rs.Close
    con.Close
    'Release the objects.
    Set rs = Nothing
    Set con = Nothing
    'Enable the screen.
    Application.ScreenUpdating = True
    'In case of an empty recordset display an error.
    Exit Sub
End If

'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
    Sheets("DATA").Cells(1, i + 1) = rs.Fields(i).Name

Next i

'Write the query values in the sheet.
Sheets("DATA").Range("A2").CopyFromRecordset rs

'Close the recordet and the connection.
rs.Close
con.Close

'Release the objects.
Set rs = Nothing
Set con = Nothing

'Adjust the columns' width.
Sheets("DATA").Columns("A:Z").AutoFit

'Enable the screen.
Application.ScreenUpdating = True
Exit Sub
Errhandler:


End Sub

1 个答案:

答案 0 :(得分:1)

更改SQL以使用UNION查询,该查询将获取所有表中的所有数据

SQL = ""
SQL = SQL & " SELECT 'Table1' AS MyTableName, * FROM Table1 WHERE .....
SQL = SQL & " UNION ALL"
SQL = SQL & " SELECT 'Table2' AS MyTableName, * FROM Table2 WHERE .....
SQL = SQL & " UNION ALL"
etc....

为什么在众神名称中你有80个具有相同字段的表格。

我原本期望一个公共表包含所有公共字段和一个&#34;行类型&#34;字段(可以存储80个不同的类型值),然后从80个其他表中链接到该表,每个表具有每行类型的额外列。

(这有它的好处,但有正当理由按照你的方式去做)

无论如何,上面的查询将起作用但是你需要在每个查询中替换*以仅获取公共字段。 (联合查询中的字段必须在每列中具有相同的数据类型 - 我认为)

如果您想添加订单,请使用列位置编号来编写,例如

  ORDER BY 4,10,12,1

如果超过允许的UNIONS数限制,请尝试:

SQL = " SELECT * FROM ("
SQL = SQL & " SELECT 'Table1' AS MyTableName, * FROM Table1 WHERE .....
SQL = SQL & " UNION ALL"
SQL = SQL & " SELECT 'Table2' AS MyTableName, * FROM Table2 WHERE .....
SQL = SQL & " UNION ALL"
etc....
SQL = SQL & " ) AS First10Tables"

SQL = SQL & " UNION ALL"

SQL = SQL & " SELECT * FROM ("
SQL = SQL & " SELECT 'Table1' AS MyTableName, * FROM Table1 WHERE .....
SQL = SQL & " UNION ALL"
SQL = SQL & " SELECT 'Table2' AS MyTableName, * FROM Table2 WHERE .....
SQL = SQL & " UNION ALL"
etc....
SQL = SQL & " ) AS Next10Tables"

如果这是一个问题:

您可以使用UNION代替UNION ALL。

重构数据库,或使用命名查询或临时表(即一次从10个表格中将行插入表中)

哈维