Help with events - keypress? afterupdate?

WhatEvil

New member
Local time
Today, 18:20
Joined
Aug 28, 2013
Messages
3
Hi there,

In a DB I'm working on, I have a simple subform with 3 controls, which adds a record to a table. I have a "Save & Close" button and a "Cancel" button which cancels the entry and closes the form.

One of the controls is a combobox (dropdown list linked to an underlying table), and the other two are text entry.

What I'm looking for is to have the Save button disabled until such a time as the 3 fields are all filled in. What I've managed to do so far is to have AfterUpdate events for my 3 controls which link to some code which checks:

Code:
If (Not IsNull(Me.ContactTypeID)) And (Not IsNull(Me.ContactInfo)) And (Not IsNull(Me.ContactRef)) Then

Me.btnSaveClose.Enabled = True

End If

This does work, BUT after typing in the last field, the user must press enter, or tab to another control, or click on another control to make the AfterUpdate event fire and enable the save button - i.e. when typing in the last field, they can't then just click straight on the save button, they have to click another control first, THEN the save button.

Is there any way that I can get an event to trigger with each character they type, checking that all boxes have at least one character in (ie. are not null) and enable it that way, or is the way I've currently got it the only way to do it?
 
Try the OnChange event.
 
After a bit of poking around (and reading something on another forum) I've found out what I needed to do.

I did need to use the OnChange event, but I also needed to be referring to the fieldName.text property.

The .text property is only available when a control has focus and it refers to the visible contents of the control, so the code I ended up with was:

Code:
Private Sub ContactInfo_Change()
If Me.ContactInfo.Text <> "" And (Not IsNull(Me.ContactTypeID)) And (Not IsNull(Me.ContactRef) And Me.ContactRef <> "") Then
    Me.btnSaveClose.Enabled = True
Else
    Me.btnSaveClose.Enabled = False
End If
End Sub

Private Sub ContactRef_Change()
If ContactRef.Text <> "" And (Not IsNull(Me.ContactTypeID)) And (Not IsNull(Me.ContactInfo)) And Me.ContactInfo <> "" Then
    Me.btnSaveClose.Enabled = True
Else
    Me.btnSaveClose.Enabled = False
End If
End Sub

Private Sub ContactTypeID_Change()
If (Not IsNull(Me.ContactTypeID)) And (Not IsNull(Me.ContactInfo)) And Me.ContactInfo <> "" And (Not IsNull(Me.ContactRef) And Me.ContactRef <> "") Then
    Me.btnSaveClose.Enabled = True
Else
    Me.btnSaveClose.Enabled = False
End If
End Sub

ContactTypeID is different because it is a comboBox rather than a text field.
Checking that fieldName.text <> "" also means that if the user deletes everything in the field then the save button disables itself again, which is the behaviour I want.

So in summary, when a control's text changes, I check first that the currently selected control is not a zero length string, and that the other 2 controls which need to have info in are not null or empty strings, then if all required fields contain info, I enable the save button.

I find this is a nicer way of stopping people trying to save records with empty info than allowing them to click the button and getting an error box popup, and I guess there will be other cool things you can do with this method.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom