Solved

Sending control name to function

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Combo box question 6 53
Combo box with two columns question 7 36
Using a combo box to search a form. 3 34
Conditionally trimming using VBA 8 30
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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