We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Looping through form controls and updating their properties

LVGICT asked
Medium Priority
Last Modified: 2010-04-23
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
Watch Question

Senior Systems and Integration Developer
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))

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
TimCotteeHead of Software Services

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

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

Tim Cottee



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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.