Strange or not????


B

bradsalmon

Hi all,

Been looking at this problem for a a couple of days and just can't
figure out what is going on here.

The below is a simple version of my code that produces the same
strangeness on my computer in Access, but I'm pretty sure it would be
the same no matter which program it was done through -

Public Sub mytest()
Dim mysection As String
mysection = "test & test1"
Debug.Print myreserves(mysection)
End Sub

Private Function myreserves(mystr As String) As String
Dim i As Long

i = InStr(1, mystr, Chr(38))
Do While i > 0
mystr = Left(mystr, i - 1) & "&" & Right(mystr, Len(mystr) -
i)
i = InStr(i + 1, mystr, Chr(38))
Loop

myreserves = mystr

End Function

When running mytest I can understand why I get a debug print of "test
& test1" but if you step through the code and look at the
variables before the sub ends you see that mysection is set to "test
& test1" as well - why is that? Surely this string should stay as
"test & test1"?

Or am I missing something really obvious here?

Thanks,

Brad
 
Ad

Advertisements

B

bradsalmon

Gees - finally answered by own question, but confused myself along the
way.

For anyone else and to ensure I've understood correctly, the key is in
passing the string to the function using ByVal because by default
ByRef is used.

Blimey, been coding VBA for years and never had that before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top