How to: Look up a value with a button beside the text
Use WklButtonEdit, bound to a field on the form, to let users either type a value or fetch it with
a button: look up a Windows account in a search dialog, test a connection string, or replace a
secret.
Requires WKL Forms Object Library 1.43 or later.
Solution overview
- Bind it like a text box: Control Source (
ControlSource) names the field. - The button raises
ButtonClick. Your procedure opens the dialog and writes the result both to the field (the record becomes dirty and is saved like any other edit) and toValue(so the control shows it immediately). - Read-only text for secrets: set
TextEditable = FalseandPasswordMask = True, and label the button “Change…”.PasswordMaskonly hides what's displayed: the value is still plain text in the bound field and inValue.
Implementation
A form frmIdentity is bound to tblIdentity and contains a WklButtonEdit btnAccount bound to
the field Account:
' Once, in design view (or on the designer's Button page); saved with the form.
With Forms!frmIdentity!btnAccount
.ControlSource = "Account"
.Object.ButtonText = "Find account"
.Object.Caption = "Windows account"
End With
' Module of frmIdentity: the button fetches the account.
Private Sub btnAccount_ButtonClick()
Dim account As String
account = InputBox("Windows account (DOMAIN\Name):", "Find account", Nz(Me!Account, ""))
If Len(account) = 0 Then Exit Sub
Me!Account = account ' the field: the record is now changed (dirty)
Me!btnAccount.Object.Value = account ' the control: shows it at once
End Sub
A secret that only your own dialog can replace:
With Forms!frmIdentity!btnSecret.Object
.TextEditable = False
.PasswordMask = True
.ButtonCaption = "Change…"
.ButtonText = "Change secret"
End With
Remarks
- While the user is typing, read the control. The control reports every change to Access, which copies it to the field when the record is saved; until then,
Me!Accountmay still hold the previous value (just as with a boundWklToggleSwitch). - Locked and AllowEdits. A locked control (
Locked) or a form withAllowEdits = Falserejects typing, but the button still raisesButtonClick. If your dialog would change the record, checkMe.AllowEditsin the procedure. - Esc restores the value from before the user's first change; a second Esc goes to Access and undoes the record.
- Not in the detail section of a continuous form: Access creates only one instance of the control there; in a list, use a
WklGridcolumn instead.
Related topics
en/wiki/msaccess/comctl/how-to/look-up-a-value-with-a-button.txt · Last modified: by Wolfgang van der Stille