locked
How to Quote Square Brackets in a Column Name for Use in the ADO Recordset's Filter Property RRS feed

  • Question

  • I am attempting to specify a filter condition on an ADO Recordset where one of the column names contains both left and right square brackets as part of the name. The resulting condition string is being assigned to the Recordset's Filter property so as to subset the data returned from the Recordset.

    If the field name is "First Name" then simply enclosing the field name in square brackets works fine. For example:

    recSet.Filter = "[First Name] = 'Tony'";
    

    However, for a field name such as "Height [cm]" I have not been able to determine how to quote the square brackets within the field name. After searching the internet, things I have tried are:

    recSet.Filter = "[Height [[]cm[]]] > 156"; 
    recSet.Filter = "[Height [cm[]]] > 156"; 
    recSet.Filter = "[Height [cm]]] > 156";
    recSet.Filter = "[Height `[`cm`]`] > 156";

    But none of these work.

    Any suggestions on the correct way to handle such names?

    Thanks, David.

    BTW, I have no control over the names in the data tables being accessed as they come from the likes of Microsoft's Analysis Services OLE DB Provider where the column names are similar to [$Customer].[Postal Code].

    • Moved by Fred Bao Friday, March 6, 2015 7:10 AM ADO Related
    Wednesday, February 25, 2015 1:04 PM

Answers

  • Hi David,

    The recordset is something complete different from a dataset and for most current Visual Studio programmers something from the old Greek. 

    Maybe they can help you in the VBA forum, because in that it is also used (your bracket is inside the string, outside it, it would have a different meaning)

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=issvba


    Success
    Cor

    • Proposed as answer by Fred Bao Friday, March 6, 2015 7:11 AM
    • Marked as answer by Just Karl Wednesday, March 25, 2015 5:25 PM
    Friday, February 27, 2015 10:59 AM

All replies

  • Hi HexTheKiwi,

    In the Sql.

    recSet.Filter = "[Height [cm]]] > 156";

    This one should be OK. I suggest you rechecking this one.

    If it is still not working. Don't use the name like this fomat. Just rename the field name without square blanket.

    Regards.

    Thursday, February 26, 2015 9:32 AM
  • Thanks the reply _Omar.

    Unfortunately, the Recordset's Filter property does not take standard SQL syntax. When I use the syntax:

    recSet.Filter = "[Height [in]]]";

    I still get a COMException thrown with an ErrorCode of 0x800A0BB9 (ADODB_E_INVALIDARGUMENT).

    And as I said earlier, I can't change the names of columns coming from some sources, such as the Microsoft Analysis Services OLE DB Provider. So I'm sort of stuck at trying to find the correct way to quote the column names.

    David.

    Thursday, February 26, 2015 12:28 PM
  • Hi David,

    The recordset is something complete different from a dataset and for most current Visual Studio programmers something from the old Greek. 

    Maybe they can help you in the VBA forum, because in that it is also used (your bracket is inside the string, outside it, it would have a different meaning)

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=issvba


    Success
    Cor

    • Proposed as answer by Fred Bao Friday, March 6, 2015 7:11 AM
    • Marked as answer by Just Karl Wednesday, March 25, 2015 5:25 PM
    Friday, February 27, 2015 10:59 AM