Working with ActiveControl

Wysy

Registered User.
Local time
Yesterday, 20:15
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.
 
if your using continuous form it wont work.
 
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
 
If I understand what you are trying to do, the active control is irrelevant so the code is never going to work correctly.

in the current event of the form, Look at the specific control(s) you are interested in to check for the leading "HTTP" string and change the property so it looks like a hyperlink.

As someone mentioned, this isn't going to work on a continuous or DS view form anyway because each control has only one set of properties. so either all visible records will look like a hyperlink if the current record is one or they will all be plain text if the current record is not a hyperlink.

1. If you have to look in multiple fields for "HTTP", your table design is wrong.
2. If you allow different types of data to be stored in the same field, your table design is wrong.

Bottom line, just change the format property to display as a hyperlink always. And normalize your data.
 
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.
 
It is the ActiveControl reference I'm questioning. It is possible that the "look like a hyperlink" is the red herring. It makes no sense to have a procedure that checks to see which is the current control to determine what to do. That kind of code belongs in one of the events of the specific control so a button action happens in the click event of a button NOT by looping through all the controls that might have actions associated with them.

So, going back to the initial example, txtCustomerID - the code is most likely validation code and so belongs in the BeforeUpdate event of the control. you don't want to run it just because the control got the focus. Same with the button. The action the button performs should be invoked by the click event of btnCustomerDetails, not because the button got the focus

I've been wrong before, this won't be the first time:)
 

Users who are viewing this thread

Back
Top Bottom