Arguments - how to pass from private sub to function (1 Viewer)

monfas

Registered User.
Local time
Today, 16:40
Joined
Jun 18, 2012
Messages
32
Hi,

I have a after update event that will match the written record with any exist record in a table in the field "OrgName". If it doesn't find exact match, will call a function with a "soundex" algorithm to see if there is only a misspelling or another name altogether.

In the afterupdate event, I have a string called strOrg (wich is the name I want to compare).

I have the soundex function in a module, so I can use it for several form generally.

What I want is to pass the strOrg to the soundex function, however I don't know how to declare the variables. however I keep having this error:

"compile error: Argument not optional"

and goes to the line tagged as 1 in the private sub afterupdate

The afterupdate sub is the following:

Code:
Private Sub tOrgName_AfterUpdate()
Dim strOrg As String

strOrg = Me.tOrgName.value

If IsNull(DLookup("orgID", "torg", "OrgName = '" & strOrg & "'")) Then

   resMsg = MsgBox("This organization name is not in the list. If you want to look for similar names press YES, if you want to register a new organization press NO.", vbYesNoCancel, "Organization not found")
      
    If resMsg = 6 Then
    
1   Soundex

    End If

End If

Debug.Print Result
Exit Sub
error_hgen:
    MsgBox "Error:(" & Err.Number & ") " & Err.Description, vbCritical

End Sub
and the soundex function is declared as

Code:
Public Function Soundex(strOrg As String) As String
Dim Result As String, c As String * 1
Dim Location As Integer
Can anyone help me on this? or direct me to a good explanation on how to declare these things?

Thanks a lot
 

pr2-eugin

Super Moderator
Local time
Today, 14:40
Joined
Nov 30, 2011
Messages
8,494
How about this?
Code:
Private Sub tOrgName_AfterUpdate()
On Error GoTo error_hgen
    Dim strOrg As String
    strOrg = Me.tOrgName

    If IsNull(DLookup("orgID", "torg", "OrgName = '" & strOrg & "'")) Then
        If MsgBox("This organization name is not in the list. If you want to look for similar names press YES, if you want to register a new organization press NO.", vbYesNoCancel, "Organization not found") = vbYes Then
            If Len([COLOR=Red][B]Soundex(strOrg)[/B][/COLOR]) > 0 Then
                MsgBox "The organization name is similar to something." & vbCrLf & vbCrLf &_
                        "What do you want to do? The code goes here", vbInformation
            End If
        End If
    End If
error_exit:
    Exit Sub
error_hgen:
    MsgBox "Error:(" & Err.Number & ") " & Err.Description, vbCritical
    Resume error_exit
End Sub
 

monfas

Registered User.
Local time
Today, 16:40
Joined
Jun 18, 2012
Messages
32
Hi, Thanks... tried something similar to you suggestion and worked

Code:
    If resMsg = 6 Then
    
    Soundex (strOrg)

    End If
Was the (strOrg) missing as an argument.

Thanks
 

pr2-eugin

Super Moderator
Local time
Today, 14:40
Joined
Nov 30, 2011
Messages
8,494
A Sub/Function that takes an argument will take in whatever you send to it. As the function Soundex takes in a String, you have already obtained that in the Private function, and since the Soundex is in a Module and takes in a String, it can take wherever it is being called from.

I have no idea what the function Soundex does, but by definition it returns a String, if you are not going to use the return value, why not make it a Sub? IMVHO, your code involves a unnecessary variable resMsg, which is what I optimized it for you, by incorporating the MsgBox in the If.
 

Users who are viewing this thread

Top Bottom