Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Sending control name to function

Dear Experts,

I use the attached function to compare the value in two controls.
If the values are not the same, the copy button specified for these two values should become enabled and the backgroundcolour should change.
When I call the function I have an issue with sending the control name.

I use the following when I call the function :
SetControl Me.RelatienummerO, Me.Relatienummer, RelatienummerO, "Copy_Relatienummer"
Problem is that when I use the function this way, the value in control RelatienummerO is being send to the function, NOT the control name.

Further more there is also a subform within this form.
I also need to call the function for the values in the subform :
SetControl [SGH_FrmHerhalingBewerkenSub].Form![ModelO], [SGH_FrmHerhalingBewerkenSub].Form![Model], [SGH_FrmHerhalingBewerkenSub].Form![ModelO], "Copy_Model"
This also does not work.

I call this function for (almost) each control in the Main form and subform in the Form_Current event of the main form.
Can somebody please advise how I should pass the control names (and not the control value) in the main form and subfrom to this function?

Thanks in advance!





Public Sub SetControl(Value1 As Variant, Value2 As Variant, Ctrl As Control, CopyButton As String)
 
'------------------------------------------------------------------
'Datum : 16-10-2008
'------------------------------------------------------------------
 
If SetErrorHandlers = True Then
    On Error GoTo Errorhandler
End If
 
Dim String1 As String
Dim String2 As String
String1 = Nz(Value1, 0)
String2 = Nz(Value2, 0)
 
If String1 <> String2 Then
    Forms("SGH_FrmHerhalingBewerken").Controls(Ctrl).BackColor = 8421631
    Forms("SGH_FrmHerhalingBewerken").Controls(CopyButton).Enabled = True
Else
    Forms("SGH_FrmHerhalingBewerken").Controls(Ctrl).BackColor = vbWhite
    Me.cmdTerug.SetFocus
    Forms("SGH_FrmHerhalingBewerken").Controls(CopyButton).Enabled = False
End If
Exit Sub
 
Errorhandler:
errordesc = "Application encountered an Error while trying to execute Public Sub SetControl [SetControl]"
Call ErrorLogging(errordesc, Me.Name, Err.Number, Err.Description)
MsgBox (errordesc), vbCritical + vbOKOnly, "Error"
Exit Sub
 
End Sub

Open in new window

0
jrameuwissen
Asked:
jrameuwissen
  • 2
  • 2
1 Solution
 
peter57rCommented:
Pass the control name in quotes.

In your function change the declaration of Ctrl from Control to String.
0
 
jrameuwissenAuthor Commented:
Thanks Peter,

I changed the sub :

Public Sub SetControl(Value1 As Variant, Value2 As Variant, Ctrl As String, CopyButton As String)

'------------------------------------------------------------------
'Datum : 16-10-2008
'------------------------------------------------------------------

If SetErrorHandlers = True Then
    On Error GoTo Errorhandler
End If

Dim String1 As String
Dim String2 As String
String1 = Nz(Value1, 0)
String2 = Nz(Value2, 0)

If String1 <> String2 Then
    Forms("SGH_FrmHerhalingBewerken").Controls(Ctrl).BackColor = 8421631
    Forms("SGH_FrmHerhalingBewerken").Controls(CopyButton).Enabled = True
Else
    Forms("SGH_FrmHerhalingBewerken").Controls(Ctrl).BackColor = vbWhite
    Me.Cancel.SetFocus
    Forms("SGH_FrmHerhalingBewerken").Controls(CopyButton).Enabled = False
End If
Exit Sub

Errorhandler:
errordesc = "Application encountered an Error while trying to execute Public Sub SetControl [SetControl]"
Call ErrorLogging(errordesc, Me.Name, Err.Number, Err.Description)
MsgBox (errordesc), vbCritical + vbOKOnly, "Error"
Exit Sub

End Sub

It works fine now when called from main form (thanks for that!) :
SetControl Me.RelatienummerO, Me.Relatienummer, RelatienummerO, "Copy_Relatienummer"

But when I call the sub for the controls in the subform I receive the error that access cannot find the control :
SetControl [SGH_FrmHerhalingBewerkenSub].Form![ModelO], [SGH_FrmHerhalingBewerkenSub].Form![Model], "[SGH_FrmHerhalingBewerkenSub].Form![ModelO]", "Copy_Model"

Any idea what's going wrong? Is my reference to the control in the subform invalid?

Thanks!
0
 
peter57rCommented:
The function has no idea where the subform is.

You have to provide the full reference including the main form.

Forms!mainformname!subformcontainercontrolname.Form.ControlName
0
 
jrameuwissenAuthor Commented:
Nailed it!

Thanks a lot for your help Peter.

Regards, Johan
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now