Looping through form controls and updating their properties

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

Accepted Solution

Carl Tawn earned 500 total points
ID: 16380317
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 43

Assisted Solution

TimCottee earned 500 total points
ID: 16380324

                    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

ID: 16380544

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

850 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