Solved

test for additional space

Posted on 2011-03-08
8
320 Views
Last Modified: 2012-06-27
I would like to test two strings to see if a space has been added.

str1 "100S LWB CH5S"

str2 "100 S LWB CH5S"

The string is essentially the same text but only had a space added at some part.

Is that possible to test for

ie determine if space added and if added add the space temporarily to str1 and then do a compare to make sure they are equal strings?

0
Comment
Question by:PeterBaileyUk
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Well, you can use StrComp to see if the are equal or note:

example:

strcomp("100S LWB CH5S","100 S LWB CH5S")

returns 1 because they are not equal

StrComp returns :
string1 is less than string2 -1
string1 is equal to string2 0
string1 is greater than string2 1
string1 or string2 is Null Null

mx

0
 

Author Comment

by:PeterBaileyUk
Comment Utility
and is it easy to find where the space belongs in field 1?

i am thinking maybe checking each character until they dont match and at that point string2(position string1 +1) should be a space and if it is then add a space at string1(position)

0
 

Author Comment

by:PeterBaileyUk
Comment Utility
or another thought find current space positions in string2

string 1 positions should correspond to string 2 bar 1 and thats my insertion point on string 1

0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"or another thought find current space positions in string2"
You can use the InStr() function to find the first occurrence of a Space (or any character)

InStr(1,SearchString, SearchingFor) returns the first position fo Searching For

InStr(1,"100 S LWB CH5S", " ") returns 4

mx
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Accepted Solution

by:
JAMcDo earned 500 total points
Comment Utility
You may wish to try this function:

Function MakeEqualStr(Str1 As String, Str2 As String) As String

    Dim Pntr As Integer
    Dim MaxPntr As Integer
   
    Pntr = 1
       
    MaxPntr = IIf(Len(Str1) < Len(Str2), Len(Str1), Len(Str2))
   
    Do While StrComp(Str1, Str2) <> 0
        Select Case StrComp(Left$(Str1, Pntr), Left$(Str2, Pntr))
            Case -1
                Str2 = Left$(Str1, Pntr) & Mid$(Str2, Pntr)
            Case 1
                Str1 = Left$(Str2, Pntr) & Mid$(Str1, Pntr)
            Case 0
                Pntr = Pntr + 1
                If Pntr > MaxPntr Then Exit Do
        End Select
    Loop
    MakeEqualStr = Str1
End Function

Create -> Module and copy/paste the function into a module.

J.
0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
I would use Trim() in your compare.  That would get rid of unnecessary spaces.

If trim(fielda) = Trim(fieldb) then...
0
 

Author Closing Comment

by:PeterBaileyUk
Comment Utility
worked great thx
0
 
LVL 3

Expert Comment

by:JAMcDo
Comment Utility
Thanks for the points,

J.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

10 Experts available now in Live!

Get 1:1 Help Now