Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2005
    Posts
    313

    Unanswered: Visual Basic: compare

    Hi:

    In VB:

    How can I do compare?

    For example:
    Invoice Table:

    [Vendor Name] [Code1] [Code2]
    Jim___________M12345____M12399

    I want to do similar: like there is portion M123, which is same, then I set that it is similar.

    So, In query, I can use Mid([Code1],1,1) = Mid([Code2],1,1)....etc.
    Check each character.

    So, In VB, how can I do that? Do I create a array. Please tell me how to create it.

    Thanks.

  2. #2
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    there is probably a neater way than char-by-char compare, but it doesn't spring to mind. so in a global module try:

    public function testSimilar(in1 as string, in2 as string) as integer
    'ACCEPTS two strings in1, in2
    'RETURNS an integer representing the similarity of the two strings
    '-1: error
    '0: no shared character sequence starting 1st left char
    '1...32xxx: length of shared character sequence starting 1st left char
    dim myPointer as integer
    dim mySim as integer
    dim myString1 as string
    dim myString2 as string
    on error goto err_testSimilar
    'LATER - deleted unnecessary line that was here
    if len(in1) < len(in2) then
    myString1 = in1
    myString2 = left$(in2, len(in1)
    else
    myString1 = in2
    myString2 = left$(in1, len(in2)
    endif
    mySim = 0
    for myPointer = 1 to len(myString1)
    if mid$(myString1, myPointer, 1) = mid$(myString2, myPointer, 1) then
    mySim = mySim + 1
    else
    exit for
    endif
    next
    testSimilar = mySim
    exit_testSimilar:
    exit function
    err_testSimilar:
    testSimilar = -1
    msgbox err.number & ": " & err.description, vbexclamation, "testSimilar error"
    resume exit_testSimilar
    end function

    SELECT thisField, thatField, testSimilar(thisField, thatField) AS Similarity FROM myTable

    izy
    Last edited by izyrider; 10-01-05 at 04:46.
    currently using SS 2008R2

Posting Permissions

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