Monday, June 23, 2008

Basic Database programming in VB 6

I wrote a small tutorial for those who still use VB 6. Some people especially students or beginners think of an ADO controls when they heard of a database. But on my experience, a professional guy saw me using a data control and said it is not applicable and not efficient for larger business soft or application. Then he told me not to use a shortcut and made me do this:
First, put 3 textbox on the form and 1 button. then:
1. Add ActiveX Data Objects 2.0 (or later) as reference :
In VB 6 environment/IDE: go to Projects> references

2. Declare

dim cn as ADODB.Connection

dim rs as ADODB.Recordset

3. Create connection string

cn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data source= " & App.Path & "\db.mdb;Persist security info=False"
cn.Open

4. Open the table and query data you want to come out

rs.Open "SELECT * FROM table1 WHERE name = '" & Text1.Text & "'", cn, adOpenKeyset, adLockBatchOptimistic

5. Fill the fields with the date you queried from the database

Text2.Text = rs.Fields("Name")
Text3.Text = rs.Fields("age")

Note: I used MS access database in this tutorial

0 comments: