Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10

    Unanswered: Problem with Replace() Statment

    Hmm, so I have a replace statement on my logon to remove all apostrophes (') and equal signs (=) which looks like this:

    Code:
    Dim CleanString
    Dim DirtyString
    
    DirtyString = Trim(Forms![Clean]![txtDirty])
    
    If DirtyString Is Null Then
        Exit Sub
    Else
        DirtyString = Replace(DirtyString, "'", "")
        DirtyString = Replace(DirtyString, "=", "")
        txtDirty = DirtyString
    End If
    And when txtDirty is blank (null) then I get "Object Required" in the first line of my IF statement.
    Basically: If Null Is Null then
    returns the error.

    Any ideas guys and gals?

    -GeorgeV
    George
    Home | Blog

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    try isnull() instead

    If isnull(DirtyString) or len(DirtyString)=0 Then
    Exit Sub
    Else
    DirtyString = Replace(DirtyString, "'", "")
    DirtyString = Replace(DirtyString, "=", "")
    txtDirty = DirtyString
    End If

    you could probably tidy it up by using Not Isnull
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    C'etait perfect!

    Can't believe I forgot isnull()

    Cheers bud
    George
    Home | Blog

  4. #4
    Join Date
    Oct 2002
    Location
    Leicester - UK
    Posts
    820
    you've made a couple of errors.
    tip you should always type your objects, it makes the errors you made more apparent.

    Firstly you set dirtyString to Forms![Clean]![txtDirty] this is a textbox not a string

    second an empty string = "" not null and if you were looking for a null its IsNull(DirtyString)

    try this
    Code:
    Dim CleanString As String
    Dim DirtyString As String
    
    Forms![Clean]![txtDirty].SetFocus
    DirtyString = Trim(Forms![Clean]![txtDirty].Text)
    
    If DirtyString = "" Then
        Exit Sub
    Else
        DirtyString = Replace(DirtyString, "'", "")
        DirtyString = Replace(DirtyString, "=", "")
        txtDirty.Text = DirtyString
    End If
    Last edited by m.timoney; 01-29-07 at 09:10.
    Definition of a Beginner, Someone who doesn't know the rules.

    Definition of an Expert, Someone who knows when to ignore the rules.

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    ahh very true, I didn't even consider that!
    How foolish, eh!

    thanks for the advice
    George
    Home | Blog

  6. #6
    Join Date
    Oct 2002
    Location
    Leicester - UK
    Posts
    820
    not foolish just inexperience. To err is human, it's also the best teaching method
    Definition of a Beginner, Someone who doesn't know the rules.

    Definition of an Expert, Someone who knows when to ignore the rules.

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Well I used IsNull somewhere else within the same project - so it was foolish to not even consider it

    I'm glad you pointed out the .Text - it never clicked that this was then referencing the string, I guess my use of null is becoming a bad habit I know how to kick!
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •