How to Use SQL SELECT Statement Without Field Syntax Error

Last reviewed: June 21, 1995
Article ID: Q110752
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SYMPTOMS

The SQL command "SELECT FROM AuthorsTable" gives the following error message:

   Syntax error in Select statement

CAUSE

The SELECT statement must be followed immediately by a valid field name, or by an asterisk (*) to indicate all fields in the table. This requirement applies to all forms of the SELECT statement, including the SELECT INTO, SELECT ALL, SELECT DISTINCT, and SELECT DISTINCTROW statements.

RESOLUTION

Indicate the fields you want immediately after the SELECT statement, for example:

   SELECT * FROM AuthorsTable

STATUS

This behavior is by design.

MORE INFORMATION

SQL SELECT Statement Syntax

The SQL SELECT statement specifies which fields you want to retrieve. You use the FROM clause to indicate which tables contain those fields. You use the WHERE clause to indicate which records are to be retrieved.

SELECT is usually the first word in an SQL statement. If you include more than one field, separate the field names with commas. List the fields in the order you want them to be retrieved. If a field name appears in more than one table listed in the FROM clause, precede the field name with the table name and the . (dot) operator. In the following example, the AU_ID field is in both the Authors table and the Titles table. The SQL statement selects the Title field from the Titles table and the Author field from the Authors table:

   SELECT Titles.Title.Dept, Author
   FROM Titles, Authors
   WHERE Titles.AU_ID = Authors.AU_ID

Visual Basic requires the SQL command string (such as the one above) to be concatenated into one, single line in a string variable or quoted string.

You can use an asterisk (*) to select all fields in a table. The following example selects all of the fields in the Publishers table:

   SELECT Publishers.* FROM Publishers

You can use the AS reserved word to create an alias for a field name. The following example uses the Year for the field name:

   SELECT [Year Published] AS Year
   FROM Titles

When you use a field name that contains a space or punctuation, surround the name with brackets:

   SELECT [Year Published], Title
   FROM Titles

For more information on SQL syntax, see the SQL topic in Visual Basic's Help menu.

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add the following to the Form Load event code:

       Sub Form_Load ()
          Dim db As database
          Dim ds As dynaset
          Set db = OpenDatabase("C:\VB3\BIBLIO.MDB")
          ' The following line gives "Syntax error in SELECT statement":
          Set ds = db.CreateDynaset("SELECT FROM Publishers")
    
          ' Replace the above line as follows to correct the syntax error:
          ' Set ds = db.CreateDynaset("SELECT * FROM Publishers")
    
          form1.Show
          Print ds![Company Name]   'Prints Company Name field from record 1.
    
       End Sub
    
    

  3. Start the program, or press the F5 key. The "Syntax error in SELECT statement" message displays. From Visual Basic's Run menu, choose End to clear that error and end Visual Basic's break mode.

To correct this syntax error, change the SELECT so that it is followed by a valid field name, or by an asterisk (*) to select all fields. For example, the SQL command "SELECT * FROM Publishers" correctly selects all fields in the Publishers table.


Additional reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgDataOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.