I currently have form that I use to enter new records that use some vba code that will highlight the field that the user is entering data into. This is similar to using the conditional formatting where the backcolor changes when a field has focus. The only reason why I use vba code instead of the conditional formatting is because I want the backcolor to only change when the user is updating data and not whenever someone just clicks on a control on the form. I have the controls locked down so the only way a user can update the data is by clicking the cmdModify button which unlocks the controls and allows dataentry. It also uses the vba code below to also highlight the field that currently has focus. Once they save their changes the backcolor highlighting will stop. Here's the problem I am running into. I now have a new form with a subform and I cannot get this code to work on the subform. It will only work on the main form. I need to figure out how to modify the vba code to also allow the controls on the subform to be highlighted when a control has focus. Below is the vba code that I use. I fire the code in the cmdModify button to turn it on and then I fire the code again in the cmdSave button to shut it off.
This code is stored in a module called "basChangeFocusColor". I call it like this in the two command buttons:
cmdModify
InitialiseEvents Me
cmdSave
InitialiseEventsOff Me
Is there an easy way to modify the code to also allow it to work on subforms? If so, how and how would I call it on my subform to turn it on and off when the cmdModify & cmdSave buttons are clicked on my main form?
Public Sub InitialiseEvents(ByRef frmThisForm As Form) ' Comments: Creates Events for form controls ' Params : None ' Owner : Copyright (c) Boston Centerless, Inc. ' Source : Boston Centerless, Inc. Source Code ' Usage : Permission granted to Boston Centerless, Inc. ' Module : modChangeFocusColor Dim ctl As Control On Error Resume Next For Each ctl In frmThisForm ctl.OnGotFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')" ctl.OnLostFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')" Next ctl Err.ClearEnd SubPublic Sub InitialiseEventsOff(ByRef frmThisForm As Form) ' Comments: Turns off events for form controls ' Params : None ' Owner : Copyright (c) Boston Centerless, Inc. ' Source : Boston Centerless, Inc. Source Code ' Usage : Permission granted to Boston Centerless, Inc. ' Module : modChangeFocusColor Dim ctl As Control On Error Resume Next For Each ctl In frmThisForm ctl.OnGotFocus = "=HandleFocusOff('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')" ctl.OnLostFocus = "=HandleFocusOff('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')" Next ctl Err.ClearEnd SubPublic Function HandleFocus(ByVal strFormName As String, _ ByVal strControlName As String, _ ByVal strChange As String) ' Comments: Creates backcolor and font color for Got Focus Events ' Params : None ' Owner : Copyright (c) Boston Centerless, Inc. ' Source : Boston Centerless, Inc. Source Code ' Usage : Permission granted to Boston Centerless, Inc. ' Module : modChangeFocusColor Static lngForeColour As Long Static lngFontWeight As Long Static lngBorderStyle As Long Static lngBorderColour As Long Static lngBackStyle As Long Static lngBackColour As Long On Error Resume Next With Forms(strFormName)(strControlName) Select Case strChange Case "Got" ' Save current configuration. lngForeColour = vbBlack lngFontWeight = .FontWeight lngBorderStyle = .BorderStyle lngBorderColour = .BorderColor lngBackStyle = 1 lngBackColour = vbWhite ' Set required configuration. .ForeColor = vbBlue .FontWeight = 700 .BorderStyle = 1 .BorderColor = vbRed .BackStyle = 1 .BackColor = vbYellow Case "Lost" ' Restore saved configuration. .ForeColor = vbBlack .FontWeight = lngFontWeight .BorderStyle = lngBorderStyle .BorderColor = lngBorderColour .BackStyle = 1 .BackColor = vbWhite End Select End With Err.ClearEnd FunctionPublic Function HandleFocusOff(ByVal strFormName As String, _ ByVal strControlName As String, _ ByVal strChange As String) ' Comments: Creates backcolor and font color for Lost Focus Events ' Params : None ' Owner : Copyright (c) Boston Centerless, Inc. ' Source : Boston Centerless, Inc. Source Code ' Usage : Permission granted to Boston Centerless, Inc. ' Module : modChangeFocusColor Static lngForeColour As Long Static lngFontWeight As Long Static lngBorderStyle As Long Static lngBorderColour As Long Static lngBackStyle As Long Static lngBackColour As Long On Error Resume Next With Forms(strFormName)(strControlName) Select Case strChange Case "Got" ' Save current configuration. lngForeColour = vbBlack lngFontWeight = .FontWeight lngBorderStyle = .BorderStyle lngBorderColour = .BorderColor lngBackStyle = 1 lngBackColour = vbWhite Case "Lost" ' Restore saved configuration. .ForeColor = vbBlack .FontWeight = lngFontWeight .BorderStyle = lngBorderStyle .BorderColor = lngBorderColour .BackStyle = 1 .BackColor = vbWhite End Select End With Err.ClearEnd Function
I tried your example and it didn't work. There is code behind the subform for other functions, etc. But I added your code and it didn't highlight the control that had focus.
Lawrence Salvucci
ASKER
@ Dale Fye
Hi Dale....I tried your code and that didn't work. It actually stopped it from working on the main form when I added your code changes.
Dale Fye
Lawrence,
It shouldn't, it should simply call the code (recursively), so that when it reaches the subform, it then calls the function again, and loops through all of the controls in the subform, then, when it leaves that drops out of the function the 2nd time, it should continue with the other controls on the form.
Have you tried putting a breakpoint in the code, and stepping through it, to determine what is happening?
Oh, where is the code? if it is in a module in the code behind the main form, it might be causing a problem with the subform calling that code. Move the code to a standard code module if it is currently in the main forms code module.
Lawrence Salvucci
ASKER
The code was in a standard module. I am going to try the breakpoint and see what's happening. I will post back shortly.
Lawrence Salvucci
ASKER
@ Dale
When I run the breakpoint I get this error when it gets to the "InitialiseEvents (ctl.Form)" line of the code
"you entered an expression that has an invalid reference to the property"
Then it bypasses the 'Else' portion of the IF statement and goes right to the "Next ctl" and just loops through that over and over. It doesn't even hit these lines of code:
It's now working on the main form again but still nothing on the subform.
Lawrence Salvucci
ASKER
When I put a breakpoint in there again it's bypassing the "InitialiseEvents (ctl.Form)" and going straight to the "Else" portion of the IF statement. Could it be because the main form has the focus when I click the cmdModify on the main form so it doesn't see the subform?
Here's something strange. When I step into the breakpoint on this line of code the 'frmThisForm.Name' shows the subform name. And when I step into the breakpoint on the 'ctl.Name' it shows the first label on the subform, 'lblItem'. The cursor is currently on the main form in one of my controls when I click the cmdModify. And it still highlights the backcolor on the main form. Why would I see the subform name and the first label in that line of code? Now I'm even more confused.
None of the controls in the list are on the subform. It does find the subform itself since it's a control on the main form. But none of those listed are on the subform itself.
I tried your example and it didn't work. There is code behind the subform for other functions, etc. But I added your code and it didn't highlight the control that had focus.