Modify a macro to deactvate a list box

With this macro, I can activate or deactivate a List Box using a button.
How can I modify it to deactivate the List Box when the content of a text field is different from “D”?

Thank you

Sub S_deactivate_Listbox(event)
oButton = event.source.model
oForm = oButton.Parent
oLstbox = oForm.getbyname("lst_FK_ID_P")
if oButton.Label ="activate Listbox" then
    oButton.Label = "deactivate Listbox"
    oLstbox.Enabled = true
else
    oButton.Label = "activate Listbox"
    oLstbox.Enabled = false
endif

End Sub

Hello,

As opposed to “modifying” you routine, it’s more of just using the same concept. Instead of checking the Button status, now you just check the value in the text box:

Sub CheckTextBox(oEvent)
	oForm = oEvent.source.model.Parent
	oLstbox = oForm.getbyname("lst_FK_ID_P")
	sValue = oForm.getbyname("YOUR_TEXT_BOX_NAME").Text
	if sValue = "D" then
	    oLstbox.Enabled = true
	else
	    oLstbox.Enabled = false
	endif
End Sub

Just attach this to the Changed event of the text box.

Also, in your routine, instead of checking a long string, just check the list box status:

if oLstbox.Enabled  then
    oButton.Label = "activate Listbox"
    oLstbox.Enabled = false
else
    oButton.Label = "deactivate Listbox"
    oLstbox.Enabled = true
endif

Thak you, it works.