Glitch in Replace function / Object Browser documentation

AHeyne

Registered User.
Local time
Today, 18:27
Joined
Jan 27, 2006
Messages
201
I have accidentally noticed a behavior of the String.Replace function that does not match the documentation from the Object Browser.

According to the Object Browser, the default value of the optional parameter 'Compare' is 'vbBinaryCompare':

Replace.png


However, the actual behavior is not like this. Here is a recording from the Immediate Window:

Code:
?Replace("A", "a", "x", , ,vbTextCompare) = "x"
True

?Replace("A", "a", "x", , ,vbBinaryCompare) = "x"
False

?Replace("A", "a", "x") = "x"
True

Without specifying the 'Compare' parameter, the result should actually be 'False'.

Is that the same for you?

I am using Access 2016 including all updates. VBA is version 7.1.1146
 
Replace without compare parameter depends on the “Option Compare” setting.

Code:
Option Compare Database ' OR Text
Option Explicit

Private Sub Test()
   Debug.Print Replace("A", "a", "x") = "x"
End Sub
vs.
Code:
Option Compare Binary
Option Explicit

Private Sub Test()
   Debug.Print Replace("A", "a", "x") = "x"
End Sub
 
regardless of what Option Compare is set, the
documentation of the Default for Compare method is vbBinaryCompare.
so the documentation is wrongly True.
 
Correct would be (online documentation + behavior in VBA):
Code:
Function Replace(Expression As String, Find As String, Replace As String, [Start As Long = 1], [Count As Long = -1], _
                 [Compare As VbCompareMethod = vbUseCompareOption]) As String
vbUseCompareOption = -1 ... this value cannot be used as a parameter value in VBA
 
IIRC, in Excel the default is vbBinaryCompare
Code:
?Replace("hello", "H", "y")
hello
?Replace("hello", "h", "y")
yello

Access relies on the Option Compare if the mode is not explicitly specified.
 
Agree that the VBE documentation isn't clear but:
a) there are actually 4 options for vbCompareMethod though vbUseCompareOption isn't listed in the VBE


b) the default vbCompareMethod where not specified is vbBinaryCompare

c) However in Access, where Opton Compare Database is used, this takes precedence.

 

Users who are viewing this thread

Back
Top Bottom