MS Access Crashes on “SELECT ?” Query

I found another way to force MS Access to crash to desktop, and it’s shockingly simple.

Sub SQLTest()
    Dim Query As ADODB.Command
    Set Query = New ADODB.Command
    With Query
        .ActiveConnection = CurrentProject.Connection
        .CommandText = "SELECT ?"
        .Parameters.Append .CreateParameter(, adChar, , 1, "a")
        .Execute
    End With
End Sub

In addition to the reliable crash, Microsoft provided some poor documentation stating, “The minimum syntax for a SELECT statement is: SELECT fields FROM table .” Well there’s no problem at all running a statement like SELECT 'Hello' and I was surprised I couldn’t parameterize it for input testing.

Workaround

The same query will succeed (not crash) if you specify a name for the result column. There is no logical reason for it to work one way and not the other, but it looks like this:

Query.CommandText = "SELECT ? AS 'MyColumn'"

Ironically, the name doesn’t have to be a valid string. I was able to get the same result using this nonsense:

Query.CommandText = "SELECT?AS'"

MS Word

Outside of the Access environment, one way to reproduce this bug is to connect to an existing accdb file. Here are the steps:

  1. Existing file named C:\example.accdb If you want to download a file, see the previous post.
  2. Open Word and click the New and Blank document buttons.
  3. Press Alt+F11 to open the VBA window.
  4. Go to the Insert menu and click Module.
  5. Go to the Tools menu and click References.
  6. Select the Microsoft ActiveX Data Objects 6.1 Library.
  7. Click OK.
  8. Paste the following code:
Sub SQLTest()
    Dim Conn As ADODB.Connection
    Dim Query As ADODB.Command
    Set Conn = New ADODB.Connection
    Set Query = New ADODB.Command
    With Query
        Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\example.accdb"
        .ActiveConnection = Conn
        .CommandText = "SELECT ?"
        .Parameters.Append .CreateParameter(, adChar, , 1, "a")
        .Execute
    End With
End Sub

After that, just press F5 to crash the program. Keep in mind you will lose all unsaved work in the program.

Leave a Reply

Your email address will not be published.