Hi all,
I need some help with VBA code to assign generic event to a form field at runtime.
I have a module which handles field errors (sets the field color to red to notify the user of a problem). This module has 2 methods: setFieldError(field) and resetFieldError(field).
When the user clicks on a field that has been set in error, the field should reset to normal (change the color back to white).
To do this, I want to assign the method resetFieldError() to the field.onkeydown event when the field is set in error.
This should look like this:
public sub setFieldError(...)
field.OnKeyDown = "=FormFieldHelper.resetErrorField(" & """" & Form.NAME & """," & _
"""" & field.NAME & """," & _
field.backcolor & ")"
...
end sub
With this code, the event is correctly assigned but Access complains when it fires saying that it does not recognise the module "FormFieldHelper".
Question1: Is there any way I can get Access to call the FormFieldHelper module ?
At that point, I changed the event assignment to call a method of the current form module:
public sub setFieldError(...)
field.OnKeyDown = "=formModuleResetErrorField(" & """" & Form.NAME & """," & _
"""" & field.NAME & """," & _
field.backcolor & ")"
...
end sub
and the method formModuleResetErrorField() would then call the FormFieldHelper
public sub formModuleResetErrorField(formName, fieldName, color)
FormFieldHelper.resetErrorField(...)
end sub
This seems to reveal some kind of bug in VB:
- The parameter list that is passed to the formModuleResetErrorField() method is reversed: formName contains the value for color and vice-versa.
Question2: Why are the parameters in reverse order ?
Anyway this works almost fine but is a pain as I need to code the formModuleResetErrorField() method in every form module of my application...
Any comment welcome,
Jerome
I need some help with VBA code to assign generic event to a form field at runtime.
I have a module which handles field errors (sets the field color to red to notify the user of a problem). This module has 2 methods: setFieldError(field) and resetFieldError(field).
When the user clicks on a field that has been set in error, the field should reset to normal (change the color back to white).
To do this, I want to assign the method resetFieldError() to the field.onkeydown event when the field is set in error.
This should look like this:
public sub setFieldError(...)
field.OnKeyDown = "=FormFieldHelper.resetErrorField(" & """" & Form.NAME & """," & _
"""" & field.NAME & """," & _
field.backcolor & ")"
...
end sub
With this code, the event is correctly assigned but Access complains when it fires saying that it does not recognise the module "FormFieldHelper".
Question1: Is there any way I can get Access to call the FormFieldHelper module ?
At that point, I changed the event assignment to call a method of the current form module:
public sub setFieldError(...)
field.OnKeyDown = "=formModuleResetErrorField(" & """" & Form.NAME & """," & _
"""" & field.NAME & """," & _
field.backcolor & ")"
...
end sub
and the method formModuleResetErrorField() would then call the FormFieldHelper
public sub formModuleResetErrorField(formName, fieldName, color)
FormFieldHelper.resetErrorField(...)
end sub
This seems to reveal some kind of bug in VB:
- The parameter list that is passed to the formModuleResetErrorField() method is reversed: formName contains the value for color and vice-versa.
Question2: Why are the parameters in reverse order ?
Anyway this works almost fine but is a pain as I need to code the formModuleResetErrorField() method in every form module of my application...
Any comment welcome,
Jerome