devxlogo

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"

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist