Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I wrote a function for diacritics in ACCESS 2007 which it is:


VB
Function xx(inp As String) As String
Dim i As Integer
Dim temp As String
For i = 1 To Len(inp)
ch = Mid(inp, i, 1)
If ch = "َ" Then
xx = Replace(inp, ch, "")
ElseIf ch = "ِ" Then
xx = Replace(inp, ch, "")
ElseIf ch = "ُ" Then
xx = Replace(inp, ch, "")
ElseIf ch = "ّ" Then
xx = Replace(inp, ch, "")
ElseIf ch = "ً" Then
xx = Replace(inp, ch, "")
ElseIf ch = "ٌ" Then
xx = Replace(inp, ch, "")
ElseIf ch = "ٍ" Then
xx = Replace(inp, ch, "")
ElseIf ch = "إ" Then
xx = Replace(inp, ch, "ا")
ElseIf ch = "أ" Then
xx = Replace(inp, ch, "ا")
ElseIf ch = "ؤ" Then
xx = Replace(inp, ch, "و")
End If
Next i
End Function


I have a table, Table1, which has two columns, Main_EW and Code.
I wrote this SQL Query in ACCESS 2007:


SQL
SELECT Table1.Main_EW
FROM Table1
WHERE (((xx([Main_EW])) Like "*جلز*"));


When "Main_EW" is in the first column of Table1, xx([Main_EW]) gives results and all things are ok. But when "Main_EW" is in the second column of Table1, xx([Main_EW]) gives Error which is Data type mismatch in criteria expression, why?
Posted
Updated 3-Nov-14 21:10pm
v2

1 solution

It should happen no matter of order of fields in a SELECT statement. Why? Your function is written in improper way.

Have a look at example:
VB
Option Explicit

Sub Test()

MsgBox RetNewString("AMADEUS") & vbCr & vbCr & RetNewString2("AMADEUS")

End Sub


Function RetNewString(inp As String) As String
Dim i As Integer

For i = 1 To Len(inp)
    If Mid(inp, i, 1) = "A" Then
        RetNewString = Replace(inp, Mid(inp, i, 1), "B")
    ElseIf Mid(inp, i, 1) = "D" Then
        RetNewString = Replace(inp, Mid(inp, i, 1), "E")
    End if
Next

End Function

'this one should returns proper result, but it's not optimized!
Function RetNewString2(ByVal inp As String) As String
Dim i As Integer, sRetVal As String

sRetVal = inp
For i = 1 To Len(sRetVal)
    If Mid(sRetVal, i, 1) = "A" Then
        sRetVal = Replace(sRetVal, Mid(sRetVal, i, 1), "B")
    ElseIf Mid(sRetVal, i, 1) = "D" Then
        sRetVal = Replace(sRetVal, Mid(sRetVal, i, 1), "E")
    End if
Next

RetNewString2 = sRetVal

End Function


Do you see the difference?

I'd suggest you to forget about for loop. Why? Simple Replace should be enough, becasue Replace function changes every occurance of searched string. See the documentation[^].

VB
Function ATM(ByVal inp As String) As String

inp = Replace(inp, "SearchedChar1", "ReplaceWithChar1")
inp = Replace(inp, "SearchedChar2", "ReplaceWithChar2")
'and so on...
ATM = inp

End Function
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900