Solved

Sending control name to function

Posted on 2008-10-16
4
1,404 Views
Last Modified: 2013-11-27
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
Comment
Question by:jrameuwissen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 22729327
Pass the control name in quotes.

In your function change the declaration of Ctrl from Control to String.
0
 
LVL 1

Author Comment

by:jrameuwissen
ID: 22729385
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
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 22729456
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
 
LVL 1

Author Comment

by:jrameuwissen
ID: 22730523
Nailed it!

Thanks a lot for your help Peter.

Regards, Johan
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question