Need to compare two strings on two Access columns

I have two sets of data that I need to compare but I can't figure out how to do it. First column contains IP addresses 'nnn.nnn.abc.def'. The second column (called name) contains 'name-abc-def'. I need to verify that the last two octets of the IP address match the second part of the name. The problem is that the name will contain zeros while the IP address will not. So I can't use the Replace(Right()) functions to compare the two values since they will have different lengths. For example:
IP: 123.123.200.6
Name: NAME-200-006

Somehow I would like to show in the query that these two values match. I hope someone can help me with this.
LVL 3
thomasd04Asked:
Who is Participating?
 
MlandaTCommented:
here is a pure access and SQL implementation of the parsing that you need to do. I have created a table adn a view, but it will give you the exact way of doing it.
Database1.mdb
0
 
JezWaltersCommented:
You're probably best to write a little VBA function to do the comparison and then invoke the function from SQL.

Hang on a mo and I'll give you the code.
0
 
Rey Obrero (Capricorn1)Commented:
you will need a function like this

Function getValues(s As String, d As String)
If s & "" = "" Then Exit Function
Dim vArr
vArr = Split(s, d)
Select Case d
    Case "."
        getValues = vArr(2)
   
    Case "-"
        getValues = vArr(1)
    Case Else
        getValues = ""
End Select
End Function

to use in a query

select * from table
where getvalues([IP],".")=getValues([Name],"-")



0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
thomasd04Author Commented:
Thanks.
0
 
Rey Obrero (Capricorn1)Commented:
thomasd04,

place the codes i posted in a regular module, and accept the default name for the module.
0
 
JezWaltersCommented:
OK, this should do the trick:
Option Explicit      ' Force explicit variable declarations
Option Compare Text  ' Perform non-case sensitive comparisons
Option Base 0        ' Start arrays at index 0

'
' Check addresses match
'
Public Function AddressesMatch(pvarAddress As Variant, _
                               pvarName As Variant) As Boolean

    ' Declare variables
    Dim astrAddress() As String  ' Address components
    Dim astrName() As String     ' Name components

    ' Compare address/name components
    astrAddress = Split(Nz(pvarAddress), ".")
    astrName = Split(Nz(pvarName), "-")
    If UBound(astrAddress) = 3 And _
       UBound(astrName) = 2 Then
        If Format(astrAddress(2), "000") = astrName(1) And _
           Format(astrAddress(3), "000") = astrName(2) Then
            AddressesMatch = True
        End If
    End If

End Function

Open in new window

0
 
JezWaltersCommented:
After you've created it, you can use the function like this:

    SELECT *
    FROM table1, table2
    WHERE AddressesMatch(table1.IPAddress, table2.Name)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.