Looping through form controls and updating their properties

Howdy Folks,

After a bit of searching I can't quite find a clear answer so thought I would ask the question (probably again, so apologies).

I have written a function that is intending to loop through the controls on a form to replace apostrophes with a safe version to aid with dynamic sql production (the sql bit isn't important, just context)

so here is the function:

    Public Sub TextBoxQuotes(ByVal Parent As Object)
        Dim c As Control
        For Each c In Parent.Controls
            If c.HasChildren Then
                If TypeOf (c) Is TextBox Then
                    c.Text.Replace(Chr(39), Chr(96)) 'replace ' with `
                End If
            End If
    End Sub

Tracing through a run, the function is entered into and will step in correctly but the replace doesn't seem to do anything. I assume this is some schoolboy error on my part! I have tried ByRef and ByVal to no change and have wondered if i am somehow creating an instance of the control instead of actually updating the original.

Can anyone see why the replace isn't doing anything?

Any help would be appreciated
Who is Participating?
Carl TawnSystems and Integration DeveloperCommented:
The Replace method on a string actually returns a string rather than simply acting on the string itself. So you will need to change the line to:

     c.Text = c.Text.Replace(Chr(39), Chr(96))
TimCotteeHead of Software ServicesCommented:

                    c.Text = c.Text.Replace(Chr(39), Chr(96)) 'replace ' with `

You are not assigning the result of the replace to anything.

Tim Cottee
LVGICTAuthor Commented:

spot the fool (*raises hand*)

Many Thanks guys

not quite sure why i thought the replace would act on itself

Have split the points as you were within a miniute of each other so i feel its unfair to say one answered and the other didn't... hope that is ok
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.