MSAccess using a wildcard in the REPLACE function

主宰稳场 提交于 2019-12-31 05:42:29

问题


I'm trying to do something simple and I don't understand why it's not working. I'm really new to MS Access VBA.

I have a string in a textbox :

\\p9990cdc\C$\Temp

I want to turn it into :
C:\Temp

I'm trying :

strSelectedFile = Replace(strSelectedFile, "\\*\C$", "C:")

and it's not working.

Not sure why RegEx doesn't work either :

strSelectedFile = Replace(strSelectedFile, "\\[\w]\C$", "C:")

Everything is set properly so the problem lies exactly in that replace code, because if I try for example :

strSelectedFile = Replace(strSelectedFile, "C$", "C:")

It works and sucessfully replaces the C$ with C:

\p9990cdc\C:\Temp

How can I make this work?

Thanks a lot for your time!


回答1:


You could instead use Mid(Instr()) to find the index of $ and grab the string from there (minus 1 to keep the directory letter) onwards.

strSelectedFile = Replace(Mid(strSelectedFile, InStr(strSelectedFile, "$") - 1, Len(strSelectedFile)), "$", ":")



回答2:


Replace doesn't do wildcards. You can implement your own function that does, or use regex using VBScript.RegEx.

I've written a small function that does this for you. Performance is suboptimal, however, and I've only done a little testing. It works for your sample input.

Public Function LikeReplace(strInput As String, strPattern As String, strReplace As String, Optional start As Long = 1)
    Dim LenCompare As Long
    Do While start <= Len(strInput)
        For LenCompare = Len(strInput) - start + 1 To 1 Step -1
            If Mid(strInput, start, LenCompare) Like strPattern Then
                strInput = Left(strInput, start - 1) & strReplace & Right(strInput, Len(strInput) - (start + LenCompare - 1))
            End If
        Next
        start = start + 1
    Loop
    LikeReplace = strInput
End Function

Using your inputs and swapping Replace with this LikeReplace should just work.




回答3:


You can use just VBScript.RegEx and the correct pattern for this.

Public Function ReplacePattern(ByRef iString As String, iSearchPattern As String, iReplaceWith As Variant)
'---------------------------------------------------------------------------------------
' Procedure : ReplacePattern
' Purpose   : Searches a string by pattern and replaces the text with given value
' Returns   : Replaced string.
'---------------------------------------------------------------------------------------
'

    Dim RE As Object
    Set RE = CreateObject("VBScript.RegExp")

    RE.ignorecase  = True 
    RE.Global      = True

    RE.Pattern     = iSearchPattern
    iString        = RE.Replace(iString, iReplaceWith)
    ReplacePattern = iString

    On Error Resume Next
    Set RE = Nothing

End Function

Read more about patterns Here

pattern: "^\\\\.*C\$" => Begins with \\ + any number of any character except linebreak + C$

usage

??replacepattern("\\p9990cdc\C$\Temp","^\\\\.*C\$","C:") => C:\Temp



来源:https://stackoverflow.com/questions/51932267/msaccess-using-a-wildcard-in-the-replace-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!