1

I'm trying to use the GO statement in a query passed to SQL Server 2017 from VBA code in MS Access 2019. The execution results in runtime error:

80040e14 "Incorrect syntax near GO".

Exactly the same query executed in SSMS works fine.

What am I doing wrong? It works if I remove the GO line.

In Access Module:

Public SQLDB As Object
Public ADOcom As Object
Public Const adCmdText as Long = 1

Sub CreateDB()
  Dim sql As String, connectionstring As String

  connectionstring = "Driver={SQL Server Native Client 11.0};" & _
                     "Server=MySQLServer;" & _
                     "Trusted_Connection=yes;" & _
                     "Provider=SQLNCLI11;" & _
                     "DataTypeCompatibility=80;" & _
                     "MARS Connection=True;"

  Set SQLDB = CreateObject("ADODB.Connection")
  Set ADOcom = CreateObject("ADODB.Command")
  SQLDB.Open connectionstring
  Set ADOcom.ActiveConnection = SQLDB
  ADOcom.CommandType = adCmdText

  sql = "USE Master;" & vbCrLf & _
        "GO" & vbCrLf & _
        "CREATE DATABASE MyTest"

  ADOcom.CommandText = sql
  ADOcom.Execute
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
andarvi
  • 110
  • 1
  • 9
  • Possible duplicate of [Incorrect syntax near 'GO'](https://stackoverflow.com/questions/25680812/incorrect-syntax-near-go) – Erik A Oct 10 '19 at 15:31

1 Answers1

2

GO is not a Transact-SQL statement, instead it is a command recognized by the Sql Server Management Studio (i.e. SSMS), SQLCMD and OSQL utilities.

Reference

You don't need to use GO statement in commandText

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72