Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a data-bound DataRepeater Control

Database

Create a data-bound DataRepeater Control

by  MikeBronner  Posted    (Edited  )
DataRepeaters can come in handy when you want to make a form where a user can edit/add/update records in a database.
This can be a very powerfull tool in combination with custom controls (a control created out of a combination of standard controls, which correspond to fields in your database).

creating a data-bound user control will be the first step. In this tutorial I will be using only a single text-field in order to simplify things.

1. Create a new ActiveX Control project.
2. Add a text-box (Text1).
3. In order for the control to get data from the database,
insert the following procedure. This is necessary for reading information from the database table:

Private Property Let TextBox(byval newText as String)
Text1.Text = newText
End Property

4. To insert data into the database, add the following
procedure. This is necessary to add data to your
database table:

Private Property Get TextBox()
TextBox = Text1.Text
End Property

Keep in mind that the name of this procedure must
correspond to the name of the Let procedure. However,
how you name these procedures is totally up to you.
5. In order to update existing records in your database
table, you will need to add the following procedure:

Private Sub Text1_Change()
PropertyChanged "TextBox"
End Sub

Here, however, it is vital that the name of the
procedure matches the name of your control. Also, the
quoted text must be the same name as used with the Get
and Let functions.
6. On the Tools menu click Procedure Attributes. On the
Procedures Attributes dialog box, click Advanced. The
Name box contains the property you want to make data-
bound, and should contain ProductName. Click Property is
data bound, then click Show in DataBindings collection
at design time. Click the Name box and click UnitPrice.
Once again, click Property is data bound, then click
Show in DataBindings collection at design time. Click OK
to close the dialog box.
7. Save the project to its own folder.
8. In the File menu, click Make Project1.ocx to make the
control file. Note that you should name your control to
something meaningfull by changing the name property of
the project at design time.


Now that you have created your custom control, you will need to insert it in your Visual Basic application. Follow the following steps:

1. On the Project menu, click Components. In the Components
dialog box, click the Controls tab, and check Microsoft
Data Repeater Control and Microsoft ADO Data Control.
Click OK to close the dialog box.
2. Draw a DataRepeater control on the form. Make the
control large enough to accommodate several "rows" of
the control you want to repeat. One "row" is the height
of the repeated control, as determined by the size of
the UserControl object's designer.
3. Set the data source of the DataRepeater, using either an
ADODB control or hardcoding it in your code, to connect
to your database and select (all or some) records.
4. Click the DataRepeater control to select it. On the
Properties window, click DataSource, and click ADODC1 to
set the data source. In the Properties window, click
RepeatedControlName to display a drop-down list of all
controls available on the computer. On the list, click
the control you just created. Note that if you did not
name it anything special, you will find it listed as
Project1. The selected control will be repeated in the
DataRepeater control.
5. Right-click the DataRepeater control, and then click
DataRepeater Properties. On the Property Pages dialog
box, click the RepeaterBindings tab. Click the
PropertyName box to display a drop-down list of the
data-bound properties of the repeated control. Click
ProductName. Click the DataField box to display a
drop-down list of available data fields from the data
source. Click ProductName. Click the Add button to add
the pair of property and data field to the
RepeaterBindings collection.Repeat this for any
remaining properties you may have created in your User
Control.
6. If you like, you can also set the format of the
datafields by clicking on the format tab and choosing
the settings as necessary. Click OK to close the dialog
box.
7. Now you are ready to test your DataRepeater control. Run
your project and scroll through the datarepeater control.

Any additional functionality, such as input-validation, maneuvering, adding new records, and such, will have to be coded manually in your application or user control. The following are some examples:

adoRecordSet.AddNew
===================
This adds a new record after the current position in your recordset. To add a record at the end, use the adoSet.MoveLast method before calling the .AddNew method.

adoRecordSet!Data
==================
This will let you access a specific field of your record set. Note that insteat of !Data, use !FieldName according to your database table.

adoRecordSet.Update
===================
This method saves any changes done in your records to your database. This only applies to the current record, so be sure to call when moving to another record.

The above are only the most important methods available. There are many more (of which there are many that I don't know as well - heck, I just learned this last week.)

This FAQ has been compiled according to personal experience and using MS Documentation ( http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbcon98/vbconusingdatarepeatercontrol.htm ).

I wrote this FAQ out of personal frustration, that I had a hard time learning how the DataRepeater control works and what procedures I need to use. Hopefully others won't have to travel the path of trial & error that I had to take.
Please feel free to send me any suggestions, comments, corrections, or updates.

Good Luck!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top