vbams-accesssearchtextbox

How to pull a value from a table in Access VBA and show in a textbox based on a unique ID displayed in another textbox?


Please bear with me as I am new to Access VBA.

I have a textbox here in Form2 named txtEID with an Employee ID value that is passed from another form.

I also have a sample textbox named txtFullName that should autopopulate the name of a person from table tblEmployees with table field EmployeeName where Form2.txtEID.value is equal to EID of tblEmployees.

I don't know how to do the autopopulation in the textbox but for my trial, I tried it in a ComboBox with this line below in its Rowsource property:

SELECT [tblEmployees].[ID], [tblEmployees].[EmployeeName] FROM tblEmployees WHERE [tblEmployees].[EID] = [txtEID]; 

It is showing the desired value (person's name based on EID of txtEID) in the ComboBox but only as a dropdown list.

image

How can I set the value of the textbox txtFullName to a person's name from the table based on the EID showing in txtEID. I also don't know how to set it up in the txtFullName Control Source property.

This has been my problem for 6 hours. Your idea is greatly appreciated.


Solution

  • Use DLOOKUP in the Control Source to pull the information.

    DLookUp("[Full_Name]","[Table1]","[EID]=" & [txtEID])

    I've wrapped it in an IIF statement as it returns an error if the txtEID control on the form is null.

    =IIf(IsNull([txtEID]),"",DLookUp("[Full_Name]","[Table1]","[EID]=" & [txtEID]))

    enter image description here

    enter image description here