I'm getting an access violation when passing a string to a DBLookupComboBox in Delphi XE6. The BusName
is a string containing a Company Name coming from the Orders Form. Am I using the right control for this?
Here's some information about the DBLookupComboBox setup.
KeyField
is called CustID
ListField
is called CompanyName
ListSource
is called CustNames
CustNameDBLCBox.Field.AsString:= OrdersForm.BusName;
Not sure what I'm missing?
A DBLookupCombobox
can be used for two different purposes.
The usual would be having assigned Datasource
and Datafield
to use it to change/display a field of one datafield with the data from a lookupdataset.
Every manual change in the DBLookupCombobox would set the Datafield to the Value of the KeyField and any change in the Dataset bound via Datasource will change the displayed value using the Listfield value found by the KeyField in the Dataset defined by ListSource.
The other usage of DBLookupCombobox would be to use it without binding on Datasource/Datafield just for selecting a Value and using the Keyvalue or Text for further work.
TL;DR
Here are two reasons which can cause then access violation:
CustNameDBLCBox.Field.AsString := <sometext>
which will lead to an access violation as described by you, since Field is not assigned: property Field: TField read FDataField;
To change the displayed value you will have to change the KeyValue:
if CustNameDBLCBox.ListSource.DataSet.Locate(CustNameDBLCBox.ListField, OrdersForm.BusName ,[]) then
CustNameDBLCBox.KeyValue := CustNameDBLCBox.ListSource.DataSet.FieldByName(CustNameDBLCBox.KeyField).Value;