Click here to Skip to main content
11,432,119 members (52,374 online)
Click here to Skip to main content

ActiveX Control to Use DataGrid in VB6

, 13 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
AciveX for using MSDataGrid with ComboBox and DTPicker

Eng01.JPG

Introduction

I wrote an article before about ActiveX using (MSFlexGrid) to edit cells and include (ComboBox) in any column in the FlexGrid. To see this ActiveX and its application, click here.

Now I create a new AciveX for using (MSDataGrid) and include (ComboBox) and (DTPicker) control to any column in the DataGrid. With my ActiveX control, you can:

  • Choose items from ComboBox to fill any field in the DataGrid.
  • Fill the field which has Date data type.
  • Can Add, Edit and Delete records.

Background

When using my ActiveX control (MKDataGrid), you must load the grid with data from database file, see the code in form2 and form3. My ActiveX control has some methods and some properties in the following table, refer to MSDataGrid for other methods and properties:

Method/Property

Definition

Example

GridCaption Set the caption of grid. MKDataGrid1.GridCaption = "School data base"
ColAlignment(c As Long) Set column's Alignment. MKDataGrid1.ColAlignment(2) = gridLeft
ColWidth(c As Long) Set column's width. MKDataGrid1.ColWidth(0) = 150
ColumnCaption(c As Long) Set column's caption. MKDataGrid1. ColumnCaption (2) = "Name"
GridFocus Set focus to grid if it is True MKDataGrid1.GridFocus = True
DateControl c, b Set DTPicker control at column c if b = True MKDataGrid1.DateControl 2, True
ListControl c, rs, f Set ComboBox control at column c with Recordset rs and Field f. MKDataGrid1.ListControl 4, adoCities, "CityName"

To use my ActiveX (MKDataGrid), you must add only one file from files: msado20.tlb, msado21.tlb, msado25.tlb or msado27.tlb to Interface from the folder C:\WINDOWS\SYSTEM32 and add (MKDataGrid) control to the Toolbox.

Using the Code

LoadData() procedure

' Define following variables in General/Declarations
' Dim adoStudents As ADODB.Recordset
' Dim adoCities As ADODB.Recordset
' Dim adoCountries As ADODB.Recordset
' Dim CitySql As String
' Dim CountrySql As String

Dim cn As ADODB.Connection
Dim DataFile As String
Dim StudentSql As String
DataFile = App.Path + "\DataFiles\" + "School_E.mdb"
Set cn = New ADODB.Connection
cn.CursorLocation = adUseClient
cn.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=" & DataFile & ";"
' Load data to the grid
StudentSql = "SELECT  StudentID,StudentName,BirthDate," _
& "Address,City,Country,Phone " _
& " FROM Students " _
& "Order by StudentID"
Set adoStudents = New ADODB.Recordset
adoStudents.Open StudentSql, cn, adOpenStatic, adLockOptimistic
MKDataGrid1.DataSource = adoStudents ' Important: Not use (Set) statement her
' We shall fill the combo box with 'CityName' field
CitySql = "SELECT DISTINCTROW CityID, CityName " _
& "FROM Cities " _
& "ORDER BY CityID;"
Set adoCities = New Recordset
adoCities.Open CitySql, cn
' We shall fill the combo box with 'CountryName' field
CountrySql = "SELECT DISTINCTROW CountryID, CountryName " _
& "FROM Countries " _
& "ORDER BY CountryID;"
Set adoCountries = New Recordset
adoCountries.Open CountrySql, cn

initGrid() Procedure

Dim c As Integer
set Caption to the grid
MKDataGrid1.GridCaption = "School data base"
'set columns Alignment
For c = 0 To 6
MKDataGrid1.ColAlignment(c) = gridLeft
Next c
'set column's Width
MKDataGrid1.ColWidth(0) = 1100
MKDataGrid1.ColWidth(2) = 1000
MKDataGrid1.ColWidth(3) = 2500
MKDataGrid1.ColWidth(4) = 1000
MKDataGrid1.ColWidth(5) = 1000
MKDataGrid1.ColWidth(6) = 1300
MKDataGrid1.ForeColor = QBColor(1) ' text in blue color
MKDataGrid1.HeadLines = 2 ' make column headers two lines
MKDataGrid1.HeadFont.Name = "Verdana" ' type of Head font
MKDataGrid1.HeadFont.Size = 8 ' size of Head font
MKDataGrid1.HeadFont.Bold = True ' font of Head is Bold
MKDataGrid1.Font.Name = "Times New Roman" ' type of Grid font
MKDataGrid1.Font.Size = 10 ' size of Grid font
'set column's Caption
MKDataGrid1.ColumnCaption(1) = "Student name"
MKDataGrid1.ColumnCaption(6) = "Home phone"

Form_Load() Procedure

LoadData ' load data from database file
initGrid ' Caption, ColWidth, HeadFont, ...
MKDataGrid1.DateControl 2, True ' Date control at column #2
' List box at column #4 include 'CityName' field
MKDataGrid1.ListControl 4, adoCities, "CityName"
' List box at column #5 include 'CountryName' field
MKDataGrid1.ListControl 5, adoCountries, "CountryName"

You can go back to the source files of the project (prjDataGrid) to read more than the previous examples.

Remarks

When extracting the file, MKDataGrid.zip, you can find the file VB6DataGrid.ocx in the folder ActiveXcontrol.

Find database files School_A.mdb and School_E.mdb in the folder DataFiles.

Find the project prjDataGrid to test the ActiveX control in the folder MKDataGrid.
This project has three forms:

  • Form1: To choose language
  • Form2: For English language
  • Form3: For Arabic language

Form2 and Form3 have the same controls:

  • The ActiveX control (MKDataGrid1) when adding the file VB6DataGrid.ocx to Toolbox
  • The button control (cmdFirst) to get the first record.
  • The button control (cmdPrevious) to get the previous record.
  • The button control (cmdNext) to get the next record.
  • The button control (cmdLast) to get the last record.
  • The button control (cmAdd) to add a new record.
  • The button control (cmdEdit) to edit a record.
  • The button control (cmdUpdate) to save records.
  • The button control (cmdCancel) to cancel edit.
  • The list box control (cmdDelete) to delete the current row.
  • The button control (cmdRefresh) to sort records.
  • The list box control (cmdClose) to close the form.

Last Words

I hope this article is useful and helps you to create your applications. Please tell me if you have any idea or if you find any problems. Thanks to Code Project and thanks to all.

-- Mostafa Kaisoun
M_Kaisoun@hotmail.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionError Found Pin
Member 1050912112-Feb-14 7:40
memberMember 1050912112-Feb-14 7:40 
Bugcompile error: procedure dclaration dose not match description of event or procedure having the some name Pin
msalmipour13-Oct-12 3:51
membermsalmipour13-Oct-12 3:51 
GeneralDatabount List Pin
Member 361799828-Apr-10 1:31
memberMember 361799828-Apr-10 1:31 
GeneralRe: Databount List Pin
Mostafa Kaisoun29-Apr-10 22:46
memberMostafa Kaisoun29-Apr-10 22:46 
QuestionInside the Combobox Search Option is not working Pin
z_monir9-Jun-09 20:57
memberz_monir9-Jun-09 20:57 
AnswerRe: Inside the Combobox Search Option is not working Pin
Mostafa Kaisoun12-Jun-09 9:56
memberMostafa Kaisoun12-Jun-09 9:56 
QuestionCould you please post the code of student ID? Pin
vbvb923-Apr-09 0:39
membervbvb923-Apr-09 0:39 
AnswerRe: Could you please post the code of student ID? Pin
Mostafa Kaisoun24-Apr-09 3:18
memberMostafa Kaisoun24-Apr-09 3:18 
GeneralRe: Could you please post the code of student ID? Pin
vbvb924-Apr-09 6:17
membervbvb924-Apr-09 6:17 
GeneralRe: Could you please post the code of student ID? Pin
Mostafa Kaisoun25-Apr-09 12:52
memberMostafa Kaisoun25-Apr-09 12:52 
GeneralRe: Could you please post the code of student ID? Pin
vbvb927-Apr-09 16:52
membervbvb927-Apr-09 16:52 
GeneralRe: Could you please post the code of student ID? Pin
Mostafa Kaisoun29-Apr-09 15:38
memberMostafa Kaisoun29-Apr-09 15:38 
GeneralRe: Could you please post the code of student ID? [modified] Pin
vbvb929-Apr-09 17:03
membervbvb929-Apr-09 17:03 
GeneralRe: Could you please post the code of student ID? Pin
Mostafa Kaisoun29-Apr-09 19:03
memberMostafa Kaisoun29-Apr-09 19:03 
GeneralRe: Could you please post the code of student ID? [modified] Pin
vbvb929-Apr-09 19:08
membervbvb929-Apr-09 19:08 
GeneralRe: Could you please post the code of student ID? Pin
Mostafa Kaisoun30-Apr-09 14:15
memberMostafa Kaisoun30-Apr-09 14:15 
GeneralRe: Could you please post the code of student ID? Pin
vbvb91-May-09 0:05
membervbvb91-May-09 0:05 
GeneralRe: Could you please post the code of student ID? Pin
vbvb93-May-09 20:44
membervbvb93-May-09 20:44 
GeneralRe: Could you please post the code of student ID? Pin
Mostafa Kaisoun6-May-09 3:03
memberMostafa Kaisoun6-May-09 3:03 
GeneralRe: Could you please post the code of student ID? Pin
vbvb97-May-09 22:08
membervbvb97-May-09 22:08 
Generalمستنى ردكم بسرعه Pin
Thg_eg12-Mar-09 17:06
memberThg_eg12-Mar-09 17:06 
GeneralRe: مستنى ردكم بسرعه Pin
Mostafa Kaisoun14-Mar-09 14:53
memberMostafa Kaisoun14-Mar-09 14:53 
GeneralMKdatagrid with master detail form Pin
najimali30-Dec-08 0:38
membernajimali30-Dec-08 0:38 
GeneralRe: MKdatagrid with master detail form Pin
Mostafa Kaisoun14-Jan-09 11:14
memberMostafa Kaisoun14-Jan-09 11:14 
GeneralExport DataGrid to Excell. Pin
Mostafa Kaisoun1-Oct-08 10:56
memberMostafa Kaisoun1-Oct-08 10:56 
GeneralCan't clear but you can do... Pin
Mostafa Kaisoun1-Oct-08 10:51
memberMostafa Kaisoun1-Oct-08 10:51 
GeneralUsing MKDataGrid with Excel VBA Pin
Ilyas Kazi30-Sep-08 1:19
memberIlyas Kazi30-Sep-08 1:19 
GeneralClearing the MKDataGrid Pin
sankarmin27-Sep-08 6:58
membersankarmin27-Sep-08 6:58 
GeneralTab key and CR Pin
Mostafa Kaisoun8-Sep-08 11:22
memberMostafa Kaisoun8-Sep-08 11:22 
GeneralTabkey or enterkey using MKDataGrid in vb6 Pin
ppkandasamy7-Sep-08 18:23
memberppkandasamy7-Sep-08 18:23 
GeneralAbout hide column Pin
Mostafa Kaisoun29-Aug-08 0:44
memberMostafa Kaisoun29-Aug-08 0:44 
GeneralColumns Pin
gioradix28-Aug-08 4:01
membergioradix28-Aug-08 4:01 
GeneralTo hide name and email [modified] Pin
Mostafa Kaisoun9-May-08 3:54
memberMostafa Kaisoun9-May-08 3:54 
GeneralNo Name & email address below the grid Pin
acpt6-May-08 1:02
memberacpt6-May-08 1:02 
Generalmove to next cell Pin
Mostafa Kaisoun20-Apr-08 23:54
memberMostafa Kaisoun20-Apr-08 23:54 
GeneralTab Key use in MKDataGrid Pin
sankarmin20-Apr-08 2:16
membersankarmin20-Apr-08 2:16 
GeneralGive me few days Pin
Mostafa Kaisoun11-Apr-08 16:13
memberMostafa Kaisoun11-Apr-08 16:13 
GeneralMKDataGrid Control Pin
kh1405ws2-Apr-08 0:04
memberkh1405ws2-Apr-08 0:04 
GeneralYes, not suppported [modified] Pin
Mostafa Kaisoun11-Mar-08 11:26
memberMostafa Kaisoun11-Mar-08 11:26 
GeneralSome Method and Properties of MSDataGrid are not supported Pin
Ilyas Kazi9-Mar-08 21:20
memberIlyas Kazi9-Mar-08 21:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 13 Apr 2008
Article Copyright 2008 by Mostafa Kaisoun
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid