InputMask details
The docs for InputMask property say this regarding the second section for InputMask property:
If you use 0 for this section, all literal display characters (for example, the parentheses in a phone number input mask) are stored with the value; if you enter 1 or leave this section blank, only characters typed into the control are stored.
Example:
Input Mask: "ID"00000;0;_
This should cause the entire text value, e.g. "ID00001"
, to be passed to NotInList event handler in the NewData parameter.
Buggy InputMask behavior for bound ComboBox control
After extensive testing, it seems that this only works flawlessly for an unbound control. If the datatype of the column in the Control Source property does not support saving the fully formatted text value, then the text input value will be formatted to be compatible with (i.e. match) the bound field datatype even though the displayed text is not what is actually saved in the field. This is the best description I could come up with and I could not find any direct resolution to this bug.
So if the bound column is of text type (e.g. Short Text) then there is no problem. But as in the original question where the bound field is a foreign key column of Long Integer type (Control Source: [Assigned User]; Bound Column: 3; Row Source has [ID] as third column), then the input value is converted to a numerical string value before being passed to NotInList.
Workaround
This preserves the desired behavior of the ComboBox and InputMask with minimal code and one extra hidden TextBox control.
- Add a simple bound TextBox control for the numeric foreign key field with the following properties:
- Control Name:
[Foreign_Key_Column_Name]
- Control Source:
[Foreign_Key_Column_Name]
- Visible:
False
- Add unbound ComboBox control setup with InputMask property and NotInList event handler:
- Control Name:
[X_Foreign_Key_Column_Name]
- Control Source: < Blank / null >
- InputMask:
"ID"00000;0;_
- Row Source:
SELECT UserCode, UserName, ID FROM tblUsers
- Bound Column:
3
(this is not the same as the Control Source property binding to the form record source and apparently not what causes the buggy behavior)
- Add NotInList event handler to unbound control with same code as before.
Private Sub X_Foreign_Key_Column_Name_NotInList(NewData as string, Response as integer)
dim sUserCode as string
sUserCode = NewData
if ActiveDirectoryUserIDExists(sUserCode) then
AddNewUserFromActiveDirectory sUserCode
Response = acDataErrAdded
Else
Response = acDataErrDisplay
end if
End Sub
- Add the following code to manually bind the ComboBox value to the numeric foreign-key column.
Private Sub Form_Current()
Me.X_Foreign_Key_Column_Name.Value = Me.Foreign_Key_Column_Name.Value
End Sub
Private Sub X_Foreign_Key_Column_Name_AfterUpdate()
Me.Foreign_Key_Column_Name.Value = Me.X_Foreign_Key_Column_Name.Value
End Sub