Working with ActiveControl

Wysy

Registered User.
Local time
Today, 02:46
Joined
Jul 5, 2015
Messages
335
Hi,
I would like to know how to use this
Code:
Dim ctlCurrentControl As Control
 
Set ctlCurrentControl = Screen.ActiveControl
If ctlCurrentControl.Name = "txtCustomerID" Then
 .
 . ' Do something here.
 .
ElseIf ctlCurrentControl.Name = "btnCustomerDetails" Then
 .
 . ' Do something here.
 .
End If
Where do i put in the form VBA? OnCurrent it does not work or i do something wrong.
thanks
 
Perhaps it would best to tell us WHAT you are trying to do.
 
If it was in a form, then you could just as easy use ActiveControl?
Start learning to walk through your code, line by line, to discover why something does not work, rather than cobbling something together and hoping for the best.
It will save you hours down the line.
 
If that certain control has the focus i.e. the active control i would like to have it display as hypertext and have different backcolor. Only in case of controls that have a certain text string value.
I have tried to google the use of active control but did not find the description i was looking for.
 
this is what i am trying to get working. If the filed contains 500 then it should display itself as hyperlink.
Code:
Private Sub Packing_GotFocus()
Dim ctlCurrentControl As control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
strControlValue = ctlCurrentControl.Value
strControlColor = ctlCurrentControl.BackColor
If strControlValue = 500 Then
    ctlCurrentControl.DisplayAsHyperlink = acDisplayAsHyperlinkAlways
Else
    ctlCurrentControl.DisplayAsHyperlink = acDisplayAsHyperlinkIfHyperlink
    

End If
MsgBox strControlName & " " & strControlValue & " " & strControlColor

End Sub
 
Two variables are not declared, which tells me you do not have Option Explicit set.
Walking through your code also tells me you have not? :(

 
If the filed contains 500 then it should display itself as hyperlink.
Do you want it to be a hyperlink or just look like one?
if a control has a value of 500 its not a valid url so why always display it as one?
Sounds like a job for conditional formatting.

Sometimes in setting form/control properties its easier to use the enumeration rather than the ac value.

NameValueDescription
acDisplayAsHyperlinkAlways1Always display the contents of the control as a hyperlink.
acDisplayAsHyperlinkIfHlink0Display the contents of the control as a hyperlink only when its contents are in the form of a Uniform Resource Locator (URL).
acDisplayAsHyperlinkOnScreenOnly2Display the contents of the control as a hyperlink only on the screen.
 
I did correct it. Still does not work.
Code:
Private Sub Packing_GotFocus()
Dim ctlCurrentControl As control
Dim strControlName As String
Dim strcontrolvalue As Long
Dim strControlcolor As Variant
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
strcontrolvalue = ctlCurrentControl.Value
strControlcolor = ctlCurrentControl.BackColor
If strcontrolvalue = 500 Then
    MsgBox ("500")
Else
    MsgBox ("")

End If
This one above works.

Code:
Private Sub Packing_GotFocus()
Dim ctlCurrentControl As control
Dim strControlName As String
Dim strcontrolvalue As Long
Dim strControlcolor As Variant
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
strcontrolvalue = ctlCurrentControl.Value
strControlcolor = ctlCurrentControl.BackColor
If strcontrolvalue = 500 Then
    
    ctlCurrentControl.BackColor = vbGreen
    
Else
    
    ctlCurrentControl.BackColor = vbRed
    
End If
this one works too, but the whole column changes the color and not only the active control.
 
Conditional formatting does not allow the hyperlink display to set. Yes that is correct it is not a real URL it should just look one. If the field shows 500 i want to click on it to perform another action and i would like to have the hand cursor over it.
 
Depending on how you structure your Urls you could also do something like this so that you can still trap case else with something.

Code:
If InStr(1, Screen.ActiveControl.Value, "https://") Or InStr(1, Screen.ActiveControl.Value, "http://") Then

        Call fHandleFile(Screen.ActiveControl.Value, 1)

    Else

        Select Case Screen.ActiveControl.Value

        Case 500

            MsgBox "It's 500, do something"

        Case 1000

            MsgBox "It's 1000, do something else"

        Case 25 To 50

            MsgBox "25 to 50"

        Case Else

            MsgBox "This is Case Else"

        End Select

    End If

End Function
 
Would help to see some examples of what you are trying to display but if all you want is a control to look like a hyperlink if another field = 500 then use conditional formatting. And if you want it to act like a hyperlink use something like the mouse up event to check the value of the other field and if it =500, do what a hyperlink does otherwise do nothing.

You can use the mouse move event to display a hand if the other field =500. But for this to work, the record in question must have the focus or you need more complex code to lookup the other field value
 
. Yes that is correct it is not a real URL it should just look one. If the field shows 500 i want to click on it to perform another action and i would like to have the hand cursor over it.

I dont quite get it either @Pat Hartman , but I believe the OP just wants the text field to look like a hyperlink but not neccessarily function as one.
 

Users who are viewing this thread

Back
Top Bottom