Looping through form controls and updating their properties

Posted on 2006-04-05
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
Question by:LVGICT
    LVL 52

    Accepted Solution

    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))
    LVL 44

    Assisted Solution

    Hi LVGICT,

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

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

    Tim Cottee

    Author Comment


    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

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now