Alex
Alex

Reputation: 113

MS-ACCESS VBA How to call a public function by formname and controlnames

I have a public function that just copy the value from a textbox to another.

Public Function ChangeDocType(frmName As Form, ctrlSourceName As control, ctrlDestName As control)
frmName.ctrlDestName.value = frmName.ctrlSourceName.value
End Function

I'm calling this function in a form like this but doesn't work..help

ChangeDocType Me, Me.Source.Name, Me.Dest.Name

Upvotes: 1

Views: 1049

Answers (2)

Gustav
Gustav

Reputation: 55961

Your arguments' names are confusing, as you don't pass the names of the controls but the controls themselves. Also, when doing so, the form is of no use. Further, as June7, I would use a subprocedure, thus:

Public Sub ChangeDocType(Source As Control, Destination As Control)

    Destination.Value = Source.Value

End Sub

To be called:

ChangeDocType Me!Source, Me!Destination

If you wish to pass the names of the controls, the form is needed, here by its name as well:

Public Sub ChangeDocType(FormName As String, SourceName As String, DestinationName As String)

    Forms(FormName).Controls(DestinationName).Value = Forms(FormName).Controls(SourceName).Value

End Sub

To be called:

ChangeDocType Me.Name, Me!SourceControl.Name, Me!DestinationControl.Name

Upvotes: 2

June7
June7

Reputation: 21379

I got this to work (I made it a Sub but Function would work):

ChangeDocType Me, Me.Source, Me.Dest

Public Sub ChangeDocType(frmName As Form, ctrlSourceName As Control, ctrlDestName As Control)
frmName(ctrlDestName.Name) = frmName(ctrlSourceName.Name)
End Sub

Upvotes: 1

Related Questions