Microsoft KB Archive/110752

= How to Use SQL SELECT Statement Without Field Syntax Error =

Article ID: 110752

Article Last Modified on 10/29/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q110752



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.



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
 Start a new project in Visual Basic. Form1 is created by default.  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  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 query words: 3.00

Keywords: KB110752

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.