Click here to Skip to main content
12,290,988 members (59,322 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked

Using DataGrid and Data Controls to Access an Access Database

, 8 Jan 2003 CPOL
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

Team Leader
Egypt Egypt
I started programming with GWBasic as a hobby during high school and then with VB6 throughout college, where I majored in Structural Engineering. In my final year of college, I decided to change over to programming. Currently, I work as a Lead Software Engineer building educational portals. My programming skills include C++, C#.NET, VB .NET, PHP, MS SQL Server, and MySQL. I also started post graduate studies in computer science.

You may also be interested in...

Comments and Discussions

GeneralError when using update code Pin
legoman5511-Nov-05 17:36
memberlegoman5511-Nov-05 17:36 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 9 Jan 2003
Article Copyright 2003 by hspc
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid