指定包含 SELECT 陳述式所列示之欄位中的資料表或查詢。

SELECT fieldlist FROM tableexpression [IN externaldatabase ]

包含 FROM 子句的 SELECT 陳述式有以下部分:

fieldlist 要與任何欄位名稱別名、 SQL 彙總函數 、選取述詞 ( ALL、DISTINCT、DISTINCTROW 或 TOP ) 或其他 SELECT 陳述式選項一起擷取之一個或多個欄位的名稱。 tableexpression 識別一個或多個可從中擷取資料之資料表的陳述式。 陳述式可以是單一資料表的名稱、已儲存的查詢名稱或 INNER JOIN LEFT JOIN RIGHT JOIN 的複合結果。 externaldatabase 包含 tableexpression 中所有資料表的外部資料庫完整路徑。

必須要有 FROM 並接在任何 SELECT 陳述式之後。

tableexpression 中資料表的名稱順序並不重要。

為了改善效能和促進使用上的便利,建議您使用連結的資料表,而非使用 IN 子句來從外部資料庫擷取資料。

下列範例顯示如何從 Employees 資料表中擷取資料:

SELECT LastName, FirstName 
FROM Employees;

下列部分範例會假設 [Employees] 資料表中有一個 [Salary] 欄位。 請注意,在 Northwind 資料庫的 [Employees] 資料表中,這個欄位實際上並不存在。

This example creates a dynaset-type Recordset based on an SQL statement that selects the LastName and FirstName fields of all records in the Employees table. It calls the EnumFields procedure, which prints the contents of a Recordset object to the Debug window.

Sub SelectX1() 
    Dim dbs As Database, rst As Recordset 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
    ' Select the last name and first name values of all  
    ' records in the Employees table. 
    Set rst = dbs.OpenRecordset("SELECT LastName, " _ 
        & "FirstName FROM Employees;") 
    ' Populate the recordset. 
    rst.MoveLast 
    ' Call EnumFields to print the contents of the 
    ' Recordset. 
    EnumFields rst,12 
    dbs.Close 
End Sub

本範例會計算位於 PostalCode 欄位中並命名傳回欄位 Tally 之項目的記錄總數。

Sub SelectX2() 
    Dim dbs As Database, rst As Recordset 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
    ' Count the number of records with a PostalCode  
    ' value and return the total in the Tally field. 
    Set rst = dbs.OpenRecordset("SELECT Count " _ 
        & "(PostalCode) AS Tally FROM Customers;") 
    ' Populate the Recordset. 
    rst.MoveLast 
    ' Call EnumFields to print the contents of  
    ' the Recordset. Specify field width = 12. 
    EnumFields rst, 12 
    dbs.Close 
End Sub

本範例會顯示員工人數及平均和最高的薪資。

Sub SelectX3() 
    Dim dbs As Database, rst As Recordset 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
    ' Count the number of employees, calculate the  
    ' average salary, and return the highest salary. 
    Set rst = dbs.OpenRecordset("SELECT Count (*) " _ 
        & "AS TotalEmployees, Avg(Salary) " _ 
        & "AS AverageSalary, Max(Salary) " _ 
        & "AS MaximumSalary FROM Employees;") 
    ' Populate the Recordset. 
    rst.MoveLast 
    ' Call EnumFields to print the contents of 
    ' the Recordset. Pass the Recordset object and 
    ' desired field width. 
    EnumFields rst, 17 
    dbs.Close 
End Sub

The Sub procedure EnumFields is passed a Recordset object from the calling procedure. The procedure then formats and prints the fields of the Recordset to the Debug window. 變數是所需的列印欄位寬度。 某些欄位可能會被截斷。

Sub EnumFields(rst As Recordset, intFldLen As Integer) 
    Dim lngRecords As Long, lngFields As Long 
    Dim lngRecCount As Long, lngFldCount As Long 
    Dim strTitle As String, strTemp As String 
    ' Set the lngRecords variable to the number of 
    ' records in the Recordset. 
    lngRecords = rst.RecordCount 
    ' Set the lngFields variable to the number of 
    ' fields in the Recordset. 
    lngFields = rst.Fields.Count 
    Debug.Print "There are " & lngRecords _ 
        & " records containing " & lngFields _ 
        & " fields in the recordset." 
    Debug.Print 
    ' Form a string to print the column heading. 
    strTitle = "Record  " 
    For lngFldCount = 0 To lngFields - 1 
        strTitle = strTitle _ 
        & Left(rst.Fields(lngFldCount).Name _ 
        & Space(intFldLen), intFldLen) 
    Next lngFldCount     
    ' Print the column heading. 
    Debug.Print strTitle 
    Debug.Print 
    ' Loop through the Recordset; print the record 
    ' number and field values. 
    rst.MoveFirst 
    For lngRecCount = 0 To lngRecords - 1 
        Debug.Print Right(Space(6) & _ 
            Str(lngRecCount), 6) & "  "; 
        For lngFldCount = 0 To lngFields - 1 
            ' Check for Null values. 
            If IsNull(rst.Fields(lngFldCount)) Then 
                strTemp = "<null>" 
                ' Set strTemp to the field contents.  
                Select Case _ 
                    rst.Fields(lngFldCount).Type 
                    Case 11 
                        strTemp = "" 
                    Case dbText, dbMemo 
                        strTemp = _ 
                            rst.Fields(lngFldCount) 
                    Case Else 
                        strTemp = _ 
                            str(rst.Fields(lngFldCount)) 
                End Select 
            End If 
            Debug.Print Left(strTemp _  
                & Space(intFldLen), intFldLen); 
        Next lngFldCount 
        Debug.Print 
        rst.MoveNext 
    Next lngRecCount 
End Sub
  • 存取開發人員論壇
  • 在 support.office.com 上存取說明
  • 存取 UtterAccess 上的論壇
  • 存取開發人員和 VBA 程式設計說明中心 (FMS)
  • 存取 StackOverflow 上的文章
  • 支援和意見反應

    有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應