Attempting CREATE VIEW in Access gives "Syntax error in CREATE TABLE statement"
Asked Answered
T

2

3

I typed this code to create a view in a pre created database:

CREATE VIEW NHTrips AS
SELECT TripID, TripName, StartLocation, State, Distance, MaxGrpSize, Type, Season
FROM Trip
WHERE State = 'NH' 
;

When I try to run Access(2007) responds with a an error message: "Syntax error in CREATE TABLE statement."

Why?

Terzas answered 24/9, 2015 at 20:28 Comment(1)
The Microsoft Access database engine does not support the use of CREATE VIEW, or any of the DDL statements, with non-Microsoft Access database engine databases.msdn.microsoft.com/en-us/library/bb177895(v=office.12).aspxSquire
A
4

Access supports CREATE VIEW when you execute it from ADO/OleDb. This code snippet works because CurrentProject.Connection is an ADO object ...

Dim strSql As String
strSql = "CREATE VIEW NHTrips AS" & vbCrLf & _
    "SELECT TripID, TripName, StartLocation, State, Distance, MaxGrpSize, Type, Season" & vbCrLf & _
    "FROM Trip" & vbCrLf & _
    "WHERE State = 'NH';"
CurrentProject.Connection.Execute strSql

However attempting to execute the same statement from DAO triggers error #3290 "Syntax error in CREATE TABLE statement." ...

CurrentDb.Execute strSql ' CurrentDb refers to a DAO Database object

That means you will get the same error if you attempt to execute that statement from the query designer because it uses DAO.

If you can use something other than CREATE VIEW, consider using the CreateQueryDef method to create your query with the SQL SELECT statement ...

strSql = "SELECT TripID, TripName, StartLocation, State, Distance, MaxGrpSize, Type, Season" & vbCrLf & _
    "FROM Trip" & vbCrLf & _
    "WHERE State = 'NH';"
CurrentDb.CreateQueryDef "NHTrips", strSql
Awry answered 25/9, 2015 at 0:8 Comment(0)
A
1

Not much info given about which database you are trying to create a view in. If you are trying to create a view in access, then you should think of all the queries that are in the navigation bar of access as VIEWS.

These can be created using VBA by manipulating the QueryDefs object see here and here

Allophone answered 24/9, 2015 at 20:39 Comment(1)
Did you find the above useful? If so please flag as an answer.Allophone

© 2022 - 2024 — McMap. All rights reserved.