4

as read in this question : Undefined function 'Replace' in expression , I'm getting the error "Undefined function 'Replace' in expression" because "you aren't using the Access query engine at all", but what do I use as an alternative ? Apparantly "a combination of Iif, Instr" would work, but I can't find out a way to actually replace something with these.

All I want is to remove the spaces out of a value, how would I do this?

const string strSql = "SELECT TOP 15 HOOFDGROEP.HOOFDGROEP, SUBGROEP.SUBGROEP, Artikels.*" +
                                  " FROM (Artikels LEFT JOIN HOOFDGROEP ON Artikels.HOOFDGROEPID = HOOFDGROEP.ID)" +
                                  " LEFT JOIN SUBGROEP ON Artikels.SUBGROEPID = SUBGROEP.ID WHERE REPLACE(ArtikelNaam, ' ', '') LIKE  '%' + @ArtikelNaam + '%'";

            var objCommand = new OleDbCommand(strSql, _objConnection);
            objCommand.Parameters.Add("@ArtikelNaam", OleDbType.Char).Value = naamZoeker.Replace(" ", "");
Community
  • 1
  • 1
Boyen
  • 1,429
  • 2
  • 15
  • 22
  • 3
    Please edit you question and show the command where you are getting the error. – Gordon Linoff Jul 23 '14 at 11:05
  • There is a differenz between remove spaces and replace them! So if you want to remove spaces at the begin and the end of your value you could do this in c# via trim(). Also notice that sql isn't build for string operations even if you can do most of them – WiiMaxx Jul 23 '14 at 11:09
  • @WiiMaxx I don't only want to remove them at the end or beginning, but all of them, and I believe the only way to do that is to replace " " with "", which is basicly equal to removing the spaces – Boyen Jul 23 '14 at 11:13

6 Answers6

8

If you download and install the

Microsoft Access Database Engine 2010 Redistributable

then you can use the following in the connection string for your OleDbConnection object...

Provider=Microsoft.ACE.OLEDB.12.0

...and the Replace() function will be available to your queries. For example, the following code works for me:

using (var conn = new OleDbConnection())
{
    conn.ConnectionString =
            @"Provider=Microsoft.ACE.OLEDB.12.0;" +
            @"Data Source=C:\__tmp\testData.accdb;";
    conn.Open();
    using (var cmd = new OleDbCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText =
            "UPDATE Table1 SET ProductType = Replace(ProductType, ' ', '')";
        cmd.ExecuteNonQuery();
    }
    conn.Close();
}

Note that you need to download and install the version of the Access Database Engine with the same "bitness" as your .NET application: 32-bit applications require the 32-bit version of the database engine and 64-bit applications require the 64-bit version of the database engine.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • 1
    Note that the Access 2016/2019 Engine (`Microsoft.ACE.OLEDB.16.0`) does not support JET 3.0 databases (Access 97 and earlier), and annoyingly, the ACE 16 driver also installs itself as an alias for the ACE 12 driver because so many programs hardcoded `Microsoft.ACE.OLEDB.12.0` into their connection-strings even though ACE 12 supported JET 3.0 databases. Grrr. (The non-redistributable ACE 15 driver in Office 2013 also replaced ACE 12 for the same reason). – Dai Jul 28 '20 at 01:46
3

I had the same problem with REPLACE function, however, I fixed by changing my OleDb connection with an Odbc Connection as follows:

Dim dbConn As New System.Data.Odbc.OdbcConnection
dbConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\db_name.accdb;Uid=Admin;Pwd=;"
dbConn.Open()

Dim objCmd As New System.Data.Odbc.OdbcCommand()
With objCmd
    .Connection = dbConn
    .CommandType = CommandType.Text
    .CommandText = "UPDATE table_name SET target_field = Replace(source_field, ' ', '') "
End With
objCmd.ExecuteNonQuery()

dbConn.Close()

I hope this helps.

Regards

Issac Peña
  • 97
  • 1
  • 5
2

I commented it only at last in my other answer: my VBA code unfortunately does't work with OleDbCommand, but isn't this a solution for you:

  1. For I guess that they had the same problem, see: Stackoverflow: Exception when trying to execute “REPLACE” against MS Access => They workarounded it with INSTR / MID... maybe this could help you?

  2. And there is an additional solution: See: Codeguru: Replace doesnt work...

Does this help you?

Greetings

Adelphos

Community
  • 1
  • 1
Adelphos
  • 83
  • 7
  • These are both update queries, I'm trying to make them work for my select query but I'm not figuring it out yet, I'll try to update you when I do – Boyen Jul 23 '14 at 17:35
1

If you can put VBA code in a Access Module, you can use this code in Access VBA to replace a String with an other String instead of using buliltin Access Function Replace:

Public Function TransformString(ByVal ToTransformStr As String, ByVal ReplaceStr As String, ByVal ToReplaceStr As String) As String
  Dim i As Long, sTmpString As String

  sTmpString = ""
  For i = 1 To Len(ToTransformStr)
    If Mid$(ToTransformStr, i, Len(ReplaceStr)) = ReplaceStr Then
      sTmpString = sTmpString & ToReplaceStr
      If Len(ReplaceStr) > 1 Then
        i = i + Len(ReplaceStr) - 1
      End If
    Else
      sTmpString = sTmpString & Mid$(ToTransformStr, i, 1)
    End If
  Next i

  TransformString = sTmpString

End Function

Test this code with:

Sub test()

Dim test As String

test = TransformString(" xyzABC ABCxyz ", " ", "")

End Sub

This is the same as:

test = Replace(" xyzABC ABCxyz ", " ", "")

Result is in both cases:

"xyzABCABCxyz"

And then this should work (with additional escaped " as \"):

const string strSql = "SELECT TOP 15 HOOFDGROEP.HOOFDGROEP, SUBGROEP.SUBGROEP, Artikels.*" +
                              " FROM (Artikels LEFT JOIN HOOFDGROEP ON Artikels.HOOFDGROEPID = HOOFDGROEP.ID)" +
                              " LEFT JOIN SUBGROEP ON Artikels.SUBGROEPID = SUBGROEP.ID WHERE TransformString(ArtikelNaam, \" \", \"\") LIKE  '%' + @ArtikelNaam + '%'";

Greetings,

Adelphos

Adelphos
  • 83
  • 7
  • I'd have no clue where to use it, I want to do this in my c# project – Boyen Jul 23 '14 at 12:26
  • If you have the possibility to change Access DB, you can add a Module and put above code in it. In Gordon Linoffs answer above you should write: \" \", \"\" instead of: " ", "" => you need to escape " then it should compile – Adelphos Jul 23 '14 at 12:54
  • I don't have that possibility (runs on more about 300 computers) , and I'll try that thne – Boyen Jul 23 '14 at 12:59
  • Can you complile it and it appears the same error message? – Adelphos Jul 23 '14 at 13:17
  • I guess in older versions of VB/VBA there is no builtin function **Replace**. See: http://www.vbarchiv.net/commands/cmd_replace.html - I don't know how to workaround this without additional Module. Sorry. – Adelphos Jul 23 '14 at 13:45
  • with VB do you mean Visual Basic ? because this is c# code – Boyen Jul 23 '14 at 13:59
  • You use the C# OleDBCommand to call a query on an access 2007 DB. I think the problem is, that it isn't possible to use the Access function **Replace** in your strSql String, because it says:"you aren't using the Access query engine at all". Access 2007 itself supports VBA function **Replace** - it is new enough. – Adelphos Jul 23 '14 at 14:25
  • They had the same problem, I guess: http://stackoverflow.com/questions/4834536/exception-when-trying-to-execute-replace-against-ms-access => They workarounded it with ugly INSTR / MID... maybe this could help you? – Adelphos Jul 23 '14 at 14:36
  • And last but not least there is an additional solution: http://forums.codeguru.com/showthread.php?514165-RESOLVED-Replace-doesnt-work – Adelphos Jul 23 '14 at 14:41
0

I suspect the problem is that you are using SQL Server syntax instead of MS Access syntax. I think this is the MS Access version:

SELECT TOP 15 HOOFDGROEP.HOOFDGROEP, SUBGROEP.SUBGROEP, Artikels.*
FROM (Artikels LEFT JOIN
      HOOFDGROEP
      ON Artikels.HOOFDGROEPID = HOOFDGROEP.ID) LEFT JOIN
     SUBGROEP
     ON Artikels.SUBGROEPID = SUBGROEP.ID
WHERE REPLACE(ArtikelNaam, " ", "") LIKE  "*" & @ArtikelNaam & "*";

REPLACE() is an MS Access function, but maybe it doesn't recognize it because of problems with the quotes.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Doesn't compile if I use double quotes (I can't define the string with single outer quotes) – Boyen Jul 23 '14 at 12:24
  • Doesn't fix anything if I escape the quotes either – Boyen Jul 23 '14 at 13:02
  • @Boyen . . . Are you use you are using MS Access? – Gordon Linoff Jul 23 '14 at 13:43
  • Yes, I'm using MS 2007 Access – Boyen Jul 23 '14 at 14:00
  • -1 they are using Access's so called ['ANSI-92' Query mode](http://office.microsoft.com/en-gb/access-help/about-ansi-sql-query-mode-mdb-HP003070483.aspx) syntax. – onedaywhen Aug 07 '14 at 11:03
  • @onedaywhen . . . Given that the question says *nothing* about using ANSI-92 Query mode, that doesn't seem like a reason to downvote the answer. Instead, this would be an appropriate comment to ask the OP. – Gordon Linoff Aug 07 '14 at 12:06
  • 1
    I downvoted your answer because it should be deleted and for the following reasons. 1) The OP has posted ANSI-92 Query Mode syntax (alternatively, has not posted ANSI-89 Query Mode) and that says a lot about ANSI-92 Query Mode. 2) Your suspicion that the OP is using SQL Server is misplaced. The OP is almost certainly using `OleDbCommand` from the .NET Framework, which *always* uses ANSI-92 Query Syntax. You have failed to recognise ANSI-92 Query Mode syntax and have mistaken it for syntax from another DBMS... – onedaywhen Aug 13 '14 at 09:34
  • 1
    3) You have posted ANSI-89 Query Mode syntax. The `OleDbCommand` cannot use ANSI-89 Query Syntax even if the OP wanted to. 4) You have continued to use the `REPLACE` function, which is not available to `OleDbCommand` object. This is the crux of the problem: the `OleDbCommand` cannot use the `REPLACE` function. While `REPLACE` can be used within the Access user interface (either Query Mode), you can’t use the .NET Framework within the Access UI. 5) Your allusion to a problem with quotes is misplaced. The OP’s problem has nothing to do with quotes... – onedaywhen Aug 13 '14 at 09:35
  • 1
    In summary you have misunderstood the use of ANSI-92 Query Mode, posted syntax that the OP cannot use, failed to realise that `REPLACE` cannot be used by the OP, perpetuated misinformation regarding ANSI-92 Query Mode being somehow related to SQL Server and mentioned a problem with quotes for no apparent reason. So tell me, which part of your answer do you think worthy of being kept? – onedaywhen Aug 13 '14 at 09:35
0

Per my experience, I think Gord Thompson's answer is correct. I had Microsoft Access Database Engine 2007 ( there is only 32 bit version) and 64 bit of Microsoft Access Database Engine 2010 Redistributable installed. When I published my .net Click Once App in 32 bits, I had the subjected error, when I published in 64 bits, things went well. I tried further, uninstalled Microsoft Access Database Engine 2007 ( there is only 32 bit version) and 64 bit of Microsoft Access Database Engine 2010 Redistributable, installed 32 bit Microsoft Access Database Engine 2010 Redistributable, and then published my App in 32 bit again, everything was in order.

chphzz
  • 1