Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / VBScript

Using DataGrid and Data Controls to Access an Access Database

Rate me:
Please Sign up or sign in to vote.
4.20/5 (12 votes)
8 Jan 2003CPOL2 min read 205.2K   5.6K   31   7
Using DataGrid and Data Controls to Access an Access Database

Sample Image - Data.jpg

Introduction

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:

VB.NET
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
Data.Refresh

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.

History

  • 8th January, 2003: Initial post 

License

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


Written By
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.

Comments and Discussions

 
Questionhow to set the grid values to the backend Pin
vgviji4-Jun-09 8:43
vgviji4-Jun-09 8:43 
Questionusing Datagrid Control in VB6 Pin
Sadhasivam B8-May-06 21:31
Sadhasivam B8-May-06 21:31 
GeneralError when using update code Pin
legoman5511-Nov-05 17:36
legoman5511-Nov-05 17:36 
GeneralMshFlexGrid Problem Pin
sabankocal28-Jul-05 23:03
sabankocal28-Jul-05 23:03 
Generalto know about saving a record Pin
Anonymous16-Sep-03 4:00
Anonymous16-Sep-03 4:00 
GeneralRe: to know about saving a record Pin
Hesham Amin16-Sep-03 23:18
Hesham Amin16-Sep-03 23:18 
GeneralRe: to know about saving a record Pin
Anonymous16-Sep-04 1:57
Anonymous16-Sep-04 1: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.