Assigning event at runtime (1 Viewer)

jbenezech

Registered User.
Local time
Today, 18:16
Joined
Jun 23, 2006
Messages
13
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
 

Banana

split with a cherry atop.
Local time
Today, 04:16
Joined
Sep 1, 2005
Messages
6,318
Exactly what is a FormFieldHelper? I suspect it's a custom module and if Acceess is complaining, you may be missing that module or may not have it correctly declared.
 

jbenezech

Registered User.
Local time
Today, 18:16
Joined
Jun 23, 2006
Messages
13
Yes, it's a custom module that I want to call on the onkeydown event.
The module is there and declared correctly. If I call it from the form module class it works fine.
 

Banana

split with a cherry atop.
Local time
Today, 04:16
Joined
Sep 1, 2005
Messages
6,318
I'm not 100% sure but shouldn't this be something like:

Code:
Private Sub KeyDown_YourField

Call resetFieldError (formName, fieldName, color)

End Sub
?

I'm not sure why you need have this part:
Code:
field.OnKeyDown =

Which is telling VBA to make this equal the right side of the equation, which isn't what you're trying to do.
 

jbenezech

Registered User.
Local time
Today, 18:16
Joined
Jun 23, 2006
Messages
13
If I do it this way, I have to code the keydown event for every single field of my application.
I'm trying to use a generic module to handle all that, therefore assigning the onkeydown dynamicaly
 

Banana

split with a cherry atop.
Local time
Today, 04:16
Joined
Sep 1, 2005
Messages
6,318
As far as I know, there's no such thing you described in VBA. If there was, I'd like to know about it myself. However, I have to type in Call xxx in every control's event where I need it to do.
 

ChrisO

Registered User.
Local time
Today, 21:16
Joined
Apr 30, 2003
Messages
3,202
G’day Jerome.

Actually there is a way to do it, maybe, but the syntax must be perfectly understood.

This type of thing does not help anyone to try to answer the question: -
public sub setFieldError(...)
The only time I’ve been able to receive (…) as the argument(s) to a procedure is in ‘C’ interrupt handlers when the number of arguments passed is unknown. But we are not talking about that here so it would be a good idea to skip the short cut and post the real code.

Question1: Is there any way I can get Access to call the FormFieldHelper module?
No, in Access VBA we call a public Procedure in the Module, we do not call the Module. As a side note the Module name must not be the name of a Procedure.

We would need to see the exact code you are using, or better still a small demo in A97 or A2K, so we can look at it.

Hope that helps.

Regards,
Chris.
 

jbenezech

Registered User.
Local time
Today, 18:16
Joined
Jun 23, 2006
Messages
13
You're absolutly right ChrisO, I should have posted the whole code.
Here it is:

1) Module FormFieldHelper:

'Sets a form field in error by setting its back color to red and showing its
'error message label
Public Sub setFieldError(Form As Object, field As Object, _
text As String, Optional defaultField As Object)

On Error Resume Next

field.OnKeyDown = "=resetErrorField(" & """" & Form.NAME & """," & _
"""" & field.NAME & """," & _
field.backcolor & "," & _
"""" & defaultField.NAME & """)"

field.backcolor = RGB(255, 0, 0)
field.SetFocus
Form.Controls(field.NAME & "_ERR").Visible = True
Form.Controls(field.NAME & "_ERR").Caption = text

'Sometimes, there is only one default error field for the form
'and not one by field
defaultField.Visible = True
defaultField.Caption = defaultField.Caption & " - " & text

Exit Sub
End Sub

Public Sub resetErrorField(Form As Object, field As Object, _
Optional defaultField As Object, Optional backcolor As Variant)

On Error Resume Next

If Not IsMissing(backcolor) Then
field.backcolor = backcolor
Else
field.backcolor = RGB(255, 255, 255)
End If
Form.Controls(field.NAME & "_ERR").Caption = ""
Form.Controls(field.NAME & "_ERR").Visible = False
defaultField.Caption = ""
defaultField.Visible = False
End Sub

2) In the form module after user action (Save record for example)

private function validateField()

if Me.USER_ID = "" then
FormFieldHelper.setFieldError Me, USER_ID, "This field is required"
validateField = false
else
validateField = true
end if​
end function

3) In the form module when user press a key in a field that was on error

Public Sub resetErrorField(formName As Variant, fieldName As Variant,
backcolor As Variant, Optional defaultField As Variant)


FormFieldHelper.resetErrorField Me, Me.Controls(backcolor), Me.Controls(formName), fieldName

End Sub



Hope this help. So my 2 questions:

a) In the setErrorField method of the FormFieldHelper, I'd like to call the resetFieldError method directly:

field.OnKeyDown = "=FormFieldHelper.resetErrorField(" & """" & Form.NAME & """," & _
"""" & field.NAME & """," & _
field.backcolor & "," & _
"""" & defaultField.NAME & """)"


I guess you answered that question and it's a NO. it's a shame because it means that all my form modules need to have their own resetErrorField method...

b) When the user presses a key in the field in error, the method resetErrorField of the form module is called but the arguments are in wrong order.
In my example, fieldName would actually contain the value for the color.


That's it
Cheers,
Jerome
 

ChrisO

Registered User.
Local time
Today, 21:16
Joined
Apr 30, 2003
Messages
3,202
G’day Jerome.

I’ve attached a little demo of what I think you are trying to do.

Hope that helps.

Regards,
Chris.
 

Attachments

  • db4.zip
    22.1 KB · Views: 102

Users who are viewing this thread

Top Bottom