[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 628
  • Last Modified:

Compare strings in Access

I would like to create a routine that will parse out spaces and perform a comparison on a varchar 255 field

desc
SO GLASS BEER 8.25 OZ PILSNER
GLASS BEER 8Z PILSNER HVY 12

In this example there are 3 strings that would validate - yes?
Any thoughts on where and how to start?  As a note, I should be considered a beginner at Access.

0
franco2
Asked:
franco2
  • 6
  • 3
  • 3
  • +1
2 Solutions
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Well, you can use the Replace() function to parse out spaces.
What is the comparison you want to make?

mx
0
 
jefftwilleyCommented:
Welcome Beginner...
Parsing follows rules. So....in order to know where you want your data split, we need to know the rules. You say out of the data you pasted in, there are 3 valid strings...how are those identified? And...what determines a good string?

That being said....how are you wanting to perform this comparison and what do you plan on doing with the results?
J
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
<jw ... the LTM thing ... man, I'm glad I've had that worked out for years>


ok ... to address just the removing spaces part, for example:

Replace("SO GLASS BEER 8.25 OZ PILSNER"," ","")  returns  SOGLASSBEER8.25OZPILSNER,

Replace can be used in code, a query or an expression in the control source in a Form/Report

The basic syntax is:

Replace(expression, find, replace)
Where, in the example above ...
expression = "SO GLASS BEER 8.25 OZ PILSNER"  <ie, the field/string you are working on to parse out>
find = " "   <a space>
replace= ""   <an empty string>

mx

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
jerryb30Commented:
I think we are looking for a split to determine separate words.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
No clue so far :-(

An *interactive* session would be ... nice :-)

Until then ...

mx
0
 
jerryb30Commented:
maybe, air code
function compareit(string1 as string, string2 as string) as integer
dim var1 as variant
dim var2 as variant
dim i as integer
dim j as integer
var1 = split(string1, " ")
var2 = split(string2, " ")
if ubound(var1) > ubound(var2) then
for i = 0 to ubound(var1) -1
 for j = 0 to ubound(var2)-1
    if var1(i) = var2(j) then
        compareit = compareit +1
   next i
     next j
else
 if ubound(var1) > ubound(var2) then
for i = 0 to ubound(var2) -1
 for j = 0 to ubound(var1)-1
    if var2(i) = var2(i) then
        compareit = compareit +1
   next i
     next j
endif
end function

create a new module named module1
Paste in code between function.... end function

Then in a query
select desc1, descr2, compareit(desc1, descr2) as equalwords from yourtable

but it depends on if descr1 and descr2 are in the same table....
need a lot more info on table(s), field(s), etc.
0
 
jerryb30Commented:
<and very clever using beer in your example. sure to get some response.>
0
 
franco2Author Commented:
Back on line - and yes beer was a bit of a bait for help...sorry, but who doesn't like an ice cold beer.

Great feedback so far...haven't tried anything out.  The purpose is to be able to look at string a and sequencially look at string b to see if there are like characters...thus, both would be loosely identified as being the same product.

ie beer in string a would match to beer in string b  

Once I have this complete, then I compare on additional attributes(not part of this question)to make sure they are the same size shape and beerie''ness....

Creation of the module - wow.  I can try, but unsure.

I am looking at about 60,000 records, this would at least help 'link' some items together....

Thanks in advacnce.


0
 
jerryb30Commented:
Same table. Two different tables?
Any other linking factors between two tables (if two tables)?
0
 
jefftwilleyCommented:
How do you introduce the string into the "search"? Will this be for Same-Field-Comparison only?
0
 
jerryb30Commented:
The module was real bad.
This works.
Function compareit(string1 As String, string2 As String) As Integer
Dim var1 As Variant
Dim var2 As Variant
Dim i As Integer
Dim j As Integer
var1 = Split(string1, " ")
var2 = Split(string2, " ")
If UBound(var1) > UBound(var2) Then
For i = 0 To UBound(var1)
 For j = 0 To UBound(var2)
    If var1(i) = var2(j) Then
        compareit = compareit + 1
        End If
   Next j
     Next i
Else
 
For i = 0 To UBound(var2)
 For j = 0 To UBound(var1)
    If var2(i) = var1(j) Then
        compareit = compareit + 1
        End If
   Next j
     Next i
End If
End Function
0
 
jerryb30Commented:
If you have a single table, and you want to compare one field with all other fields in the table,
SELECT a.descr, b.descr, compareit([a].[descr],[b].[descr]) AS WordMatches
FROM yourTable AS a,yourTable AS b
WHERE (((a.descr)<[b].[descr]) AND ((b.descr)<>[a].[descr]))
ORDER BY a.descr;

Still awaiting more info.......
0
 
jefftwilleyCommented:
I think you scared him off Jerry!
0
 
franco2Author Commented:
Not scard off but having troubles with my internet connection.  Same table but not sure on wordmatches....as in the example, beer may be in different parts of the desciption....I will try out the suggestions later as I am afraid to drop you....I will test this eve and post fidings tomorrow.
Cheers.
0
 
franco2Author Commented:
Split the point slighty - all have a hand in the solution.
compareit was the key - I can now 'assume' that if the description compared on 3>, it is the same product.

Much apprciated.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now