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(" ", "");
3
  • 3
    Please edit you question and show the command where you are getting the error. Commented Jul 23, 2014 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 Commented Jul 23, 2014 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 Commented Jul 23, 2014 at 11:13

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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).
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

Comments

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

1 Comment

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
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

9 Comments

I'd have no clue where to use it, I want to do this in my c# project
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
I don't have that possibility (runs on more about 300 computers) , and I'll try that thne
Can you complile it and it appears the same error message?
I guess in older versions of VB/VBA there is no builtin function Replace. See: vbarchiv.net/commands/cmd_replace.html - I don't know how to workaround this without additional Module. Sorry.
|
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.

9 Comments

Doesn't compile if I use double quotes (I can't define the string with single outer quotes)
Doesn't fix anything if I escape the quotes either
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...
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...
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?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.