Replace(strText,"X","Y")
Left(strText,1) & "Y" & Mid(strText,3)
Left(strText, Len(strText) - 11) & "Y" & Mid(strText, Len(strText) - 9)
Mid(string,2,len(string)-10)
Public Sub TestMid(ByRef X As String, Y As String, Z As Long)
Mid$(X, Z, Len(Y)) = Y
End Sub
sX = "ABCDEF"
sY = "RS"
TestMid sX, sY, 3
X "ABRSEF" String
Function TestMid(ByRef X As String, Y As String, Z As Long)
Mid$(X, Z, Len(Y)) = Y
End Function
Sub CheckTestMid()
Dim sX As String, SY As String
sX = "ABCDE"
SY = "RS"
Debug.Print TestMid sX, SY, 3
End Sub
TestMid[COLOR="Red"];[/COLOR] sX, SY, 3
A simple Debug.Print sX shows the result though?
Surprised to find more than one way to do something in access....
*sigh
Sent from my SM-G950U using Tapatalk
Right?!? Access can be so open ended, there are often many good ways to do something, a few poorer options, and sometimes even a most right way, but there is almost never one way [emoji16]LOL. Back in post 2 I said 'one way of doing this is .....'
There are always lots of solutions, some perhaps better than others.
Colin, one difference was that my function was NOT a function, nor was it declared as such. It was a SUB using a ByRef argument for the string that was the target of the substitution. I was using a 32-bit version of Ac2010. What you wrote, as a function, would not return a value because you never assigned a value to TestMid.
As I said, the way I did it was based on using the Locals window to verify the effect, so that is why all the gyrations. But it worked just fine for me once I got my invariably present typos fixed.
What I presented does the positional replace in-place. I didn't test what happens if the inserted string is longer than or overlaps the end of the target string, but that wasn't the point. Also did not test for the case where the string to be inserted did not match the actual size argument of the Mid$. I just wanted to demonstrate that it was possible.
By the way, Mid$ only returns strings; Mid without the $ returns variants. I don't know whether that is significant to this experiment.
and that's was with TestMid as a function.EDIT...yes it worked .... with or without the $ in the original function
If you are replacing a specific character e.g. X in a string (strText) with e.g. Y then just use
However, if the character to be replaced may change then one way of doing this isCode:Replace(strText,"X","Y")
a) 2nd character from left
b) 10th character from rightCode:Left(strText,1) & "Y" & Mid(strText,3)
If you are going to replace a variety of different position characters, I would make these into functions so the character position to be replaced can be variedCode:Left(strText, Len(strText) - 11) & "Y" & Mid(strText, Len(strText) - 9)
This only changes the LAST ROW of my file. I have 5 rows and the first 4 rows are not affected (but they should be).