Mouse Hover textbox boarder color change (1 Viewer)

alvingenius

IT Specialist
Local time
Today, 12:31
Joined
Jul 10, 2016
Messages
169
Hello
I have a form with many fields to fill
and I'm using this code to make specific textbox to change border color on mouse hover

SQL:
Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Me.txtUsername.BorderColor = 15197669
End Sub


Private Sub txtUsername_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Me.txtUsername.BorderColor = 14862256  
End Sub

So I have to repeat the same code in every control on the form

So my question is, is there a way to make this code work for all controls in the form ( text boxes / combo boxes ) without having to code every control?

Thanks
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:31
Joined
Jan 23, 2006
Messages
15,361
You may find this material relevant to your requirement and adapt as needed. Let us know how you progress.
I am also posting a link to this thread/discussion regarding some interaction with controls on a form. There are some sample solutions that my be adapted or serve as references in your pursuit of a solution.
Good luck.
 

Micron

AWF VIP
Local time
Today, 06:31
Joined
Oct 20, 2018
Messages
3,476
I guess I'd have nothing to add seeing as how I already posted at the other discussion (post 8). Let me know if you have questions, assuming there's any interest. If this is for showing required fields, consider conditional formatting. If it's for highlighting a field for some specific reason, consider that moving to the control via keyboard won't highlight the border.
 

alvingenius

IT Specialist
Local time
Today, 12:31
Joined
Jul 10, 2016
Messages
169
I guess I'd have nothing to add seeing as how I already posted at the other discussion (post 8). Let me know if you have questions, assuming there's any interest. If this is for showing required fields, consider conditional formatting. If it's for highlighting a field for some specific reason, consider that moving to the control via keyboard won't highlight the border.
What post exactly?
 

alvingenius

IT Specialist
Local time
Today, 12:31
Joined
Jul 10, 2016
Messages
169
Number 8 in the link provided. As soon as I post this, I'm going to double check but it worked yesterday...
Yep, still OK.
Wow that's exactly what i want
and I'm quoting the code here to ask a question and i hope you help me on it
Function PassName()
SQL:
Function PassName()
Dim ctl As Control

For Each ctl In Forms!frmFormName.Controls
  If ctl.ControlType = acTextBox Then
  ctl.OnMouseMove = "=MyFunc(""" & ctl.Name & """)"
End If
Next
End Function

Public Function MyFunc(strControlName As String)
  MsgBox strControlName
End Function

i want to change the border color of controls when mouse move to RED and black when lost focus "instead of msgbox in ur code"
and as I see, this work only in textbox. what if i want to include Combobox too?
 

Micron

AWF VIP
Local time
Today, 06:31
Joined
Oct 20, 2018
Messages
3,476
More than one approach comes to mind. Assuming you don't want separate functions (I try not to repeat code when one procedure will do) then maybe it's best to deal with the control type rather than the name in case you want to do something different based on type. If not, you can stick with Name but the major thing is to include all types in the calling sub where it starts with If ctl.ControlType. If you had more options, a Select Case block might be better than If's. The following is untested. If you have a problem, provide details and I'll actually test according to your issue. Might not be today though. Of course, to get black I'm assuming you have that set in design. If not, then this gets more complicated because AFAIK, there is no "MouseOff" type of event in Access to detect that you moused off of a control. Far better to just go back to the default/design colour that's set in design view.
EDIT - you realize this approach is indiscriminate - that it doesn't matter which control of any particular type you mouse over? It might become an annoyance to users.

Code:
Function PassName()
Dim ctl As Control

For Each ctl In Forms!frmFormName.Controls
  If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
  ctl.OnMouseMove = "=MyFunc(""" & ctl.ControlType & """)"
End If
Next

End Function

Public Function MyFunc(ctlType As Control)
  If ctlType = acTextBox Then ctl.BorderColor = vbRed
  If ctlType = acComboBox Then ctl.BorderColor = vbGreen
End Function
 

alvingenius

IT Specialist
Local time
Today, 12:31
Joined
Jul 10, 2016
Messages
169
More than one approach comes to mind. Assuming you don't want separate functions (I try not to repeat code when one procedure will do) then maybe it's best to deal with the control type rather than the name in case you want to do something different based on type. If not, you can stick with Name but the major thing is to include all types in the calling sub where it starts with If ctl.ControlType. If you had more options, a Select Case block might be better than If's. The following is untested. If you have a problem, provide details and I'll actually test according to your issue. Might not be today though. Of course, to get black I'm assuming you have that set in design. If not, then this gets more complicated because AFAIK, there is no "MouseOff" type of event in Access to detect that you moused off of a control. Far better to just go back to the default/design colour that's set in design view.
EDIT - you realize this approach is indiscriminate - that it doesn't matter which control of any particular type you mouse over? It might become an annoyance to users.

Code:
Function PassName()
Dim ctl As Control

For Each ctl In Forms!frmFormName.Controls
  If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
  ctl.OnMouseMove = "=MyFunc(""" & ctl.ControlType & """)"
End If
Next

End Function

Public Function MyFunc(ctlType As Control)
  If ctlType = acTextBox Then ctl.BorderColor = vbRed
  If ctlType = acComboBox Then ctl.BorderColor = vbGreen
End Function

Thanks for the fast reply
All Controls i wanna effect on it is Textbox and ComboBox only
and after i put the updated code i got this error
Capture.PNG


it appear on every textbox mouse move
 
Last edited:

Micron

AWF VIP
Local time
Today, 06:31
Joined
Oct 20, 2018
Messages
3,476
It's probably this MyFunc(ctlType As Control)
If so, one fix would be to pass the control itself then test it's type, in which case it doesn't have to be typed in the calling sub. Another would be to pass the name. I have company showing up in 45 minutes and I have a couple of things to do right now so I might not get to work on this today. Will see.
BTW, it's always appreciated if you show what line raises the error as it's not always obvious.
 

alvingenius

IT Specialist
Local time
Today, 12:31
Joined
Jul 10, 2016
Messages
169
It's probably this MyFunc(ctlType As Control)
If so, one fix would be to pass the control itself then test it's type, in which case it doesn't have to be typed in the calling sub. Another would be to pass the name. I have company showing up in 45 minutes and I have a couple of things to do right now so I might not get to work on this today. Will see.
BTW, it's always appreciated if you show what line raises the error as it's not always obvious.

that's what i see when compile
Error.PNG
 

Micron

AWF VIP
Local time
Today, 06:31
Joined
Oct 20, 2018
Messages
3,476
I see what the problem is and it's due to me rushing things and not testing. I also see now that your first post indicates that even the same type of control might be getting different border colours depending on which one it is, yet I was under the impression that it depended on control type. If the border colour is going to be specific to a control rather than just a type, you will probably want to make use of the tag property. Rather than me re-code this right now, it might be best if I knew exactly what's supposed to happen.

Or you can try changing ctl.BorderColor to ctlType.BorderColor and see if it does what you want.
That mistake was borne out of habit.
 

Users who are viewing this thread

Top Bottom