Solved

Sending control name to function

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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 how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

746 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

12 Experts available now in Live!

Get 1:1 Help Now