Change the DataSource at runtime

Change the DataSource at runtime

Visual Basic 6 is the first VB version that lets you programmatically change the DataSource property at runtime, for example to point to another ADO Data control or another ADO Recordset.

However, when you assign the new DataSource property VB immediately checks that the DataField and DataMember properties are valid. This means, for example, that if the new data source doesn’t expose a field with the same name as the current value of the DataField property, you get error Error 545 – Unable to bind to field or DataMember: ‘fieldname‘.

Fortunately, you can work around this problem by simply setting the FieldName property to a null string before setting the DataSource property, and then assign it the new value:

' have Text1 point to another Data controlText1.DataField = ""Set Text1.DataSource = Adodc2Text1.DataField = "City"


Share the Post: