Click here to Skip to main content
13,249,087 members (39,243 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 8 Jan 2003

Using DataGrid and Data Controls to Access an Access Database

, 8 Jan 2003
Rate this:
Please Sign up or sign in to vote.
Using DataGrid and Data Controls to Access an Access Database

Sample Image - Data.jpg


Hi all.. I write this article as a reply to Anonymous in the CodeProject discussion board.

Anonymous wrote:

Friends, I am very new to programming and have started with a new project which consist of a form which has 2 calendar controls. I wanted to know how can I fetch data that was inputted into a database between two dates and show it into a grid. The date selection is dynamic. Any help will be appreciated.

The program is not something fancy... but I think it can be useful for beginners in VB6 like our Anonymous friend.

The Project

I created a database using Access XP and saved it in Access 97 format. It contains one table named table1.

Table1 has 3 columns:

  1. ID: AutoNumber
  2. Name: Text .. max. Length=50
  3. BirthDate: DataTime

The main Form and Controls

frmMain: The main form... nothing special to say...

Data: A data control with properties:

  • Visible=False
  • DatabaseName=”Path of database”
  • Connect=Access (the default)

Cal(): Array of calendar control that contains 2 elements.
(You can create a control array by dropping a control over the form and copy it, then paste it on the form.)

DBGrid: A data Grid control:

  • dataSource=Data

Now... let’s come to the easy code needed to update data each time the user selects a new date from a calendar control.

This is the code for Cal_AfterUpdate:

Private Sub Cal_AfterUpdate(Index As Integer)
Dim strDate0 As String
Dim strDate1 As String
Dim strSQL As String

strDate0 = Cal(0).Day & "/" & Cal(0).Month & "/" & Cal(0).Year
strDate1 = Cal(1).Day & "/" & Cal(1).Month & "/" & Cal(1).Year

strSQL = "SELECT * FROM table1 WHERE BirthDate Between #" _
        & strDate0 & "# AND #" & strDate1 & "#"

Debug.Print strSQL

Data.RecordSource = strSQL

End Sub

That is all the required coding... thanks to the data control and data biding.

How It Works

  1. I declared two string variables to hold the dates... I composed them using the calendar control properties.
  2. strSQL is the query used to let the jet engine (which is the component that accesses data) know what data we need.
  3. Debug.Print strSQL is called for debugging purposes, so I can check if the query is formed well.
  4. Assign strSQL value to the RecordSource property of the data control and refresh data... Then we can see the extracted result set in the grid control.

Any comments are welcome.


  • 8th January, 2003: Initial post 


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


About the Author

Hesham Amin
Egypt Egypt
I’m a software architect with a long experience in software architecture and design, with interest in cloud platforms and Agile methodologies.
I worked with many teams to successfully deliver software products using many frameworks and languages, and targeting many platforms like Azure, SharePoint, mobile, and desktop applications.
During my work with Agile teams, I coached them in both the technical and engineering aspects and helped developers with different experience levels in the organization to achieve the best results and build their skills. I also worked with product owners to get the best possible value through a continuous delivery model.
All these activities do not keep me away from coding and keeping up to date with new technologies and exploring new things from cool new libraries to cloud offerings to data science.

You may also be interested in...


Comments and Discussions

Questionhow to set the grid values to the backend Pin
vgviji4-Jun-09 9:43
membervgviji4-Jun-09 9:43 
Questionusing Datagrid Control in VB6 Pin
Sadhasivam B8-May-06 22:31
memberSadhasivam B8-May-06 22:31 
GeneralError when using update code Pin
legoman5511-Nov-05 18:36
memberlegoman5511-Nov-05 18:36 
GeneralMshFlexGrid Problem Pin
sabankocal29-Jul-05 0:03
membersabankocal29-Jul-05 0:03 
Generalto know about saving a record Pin
Anonymous16-Sep-03 5:00
sussAnonymous16-Sep-03 5:00 
GeneralRe: to know about saving a record Pin
hspc17-Sep-03 0:18
memberhspc17-Sep-03 0:18 
GeneralRe: to know about saving a record Pin
Anonymous16-Sep-04 2:57
sussAnonymous16-Sep-04 2:57 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 9 Jan 2003
Article Copyright 2003 by Hesham Amin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid