Solved

Sending control name to function

Posted on 2008-10-16
4
1,400 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now