0 оценок0% нашли этот документ полезным (0 голосов)
27 просмотров7 страниц
This document describes how to connect an Access database to a VB interface. It includes code to:
1. Design an Access table called "SAMPLE" with fields for name and age.
2. Configure an ADODC connection to the Access database file.
3. Write VB code to perform CRUD (create, read, update, delete) operations on the Access table via the connection, including displaying data and generating reports.
This document describes how to connect an Access database to a VB interface. It includes code to:
1. Design an Access table called "SAMPLE" with fields for name and age.
2. Configure an ADODC connection to the Access database file.
3. Write VB code to perform CRUD (create, read, update, delete) operations on the Access table via the connection, including displaying data and generating reports.
This document describes how to connect an Access database to a VB interface. It includes code to:
1. Design an Access table called "SAMPLE" with fields for name and age.
2. Configure an ADODC connection to the Access database file.
3. Write VB code to perform CRUD (create, read, update, delete) operations on the Access table via the connection, including displaying data and generating reports.
DESIGN A TABLE IN ACCESS WITH DATABASE NAME AS “NEW” AND TABLE NAME AS “SAMPLE” WITH FOLLOWING STRUCTURE
SELECT ADODCS CONNECTION STRING PROPERTY
CLICK THE BROWSE BUTTON( )
CLICK BUILD--SELECT MICROSOFT JET 4.0 OLEDB PROVIDER
CLICK NEXT
CLICK TO SELECT A DATA BASE(HERE IT IS “NEW”)
CLICK THE OPEN BUTTON
CLICK TEST CONNECTION AND GET FOLLOWING MESSAGE
CLICK OK BUTTONS IN ALL THE WINDOWS
AGAIN SELECT ADODC AND TAKE THE RECORD SOURCE PROPERTY AS SHOWN BELOW SELECT 2-ADCMDTABLE AND “SAMPLE”(YOUR TABLE NAME FROM THE LIST) AS THE STORED PROCEDURE
CLICK OK
ADD THE FOLLOWING CODE IN THE CORRESPONDING OBJECTS
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub add_Click()
Dim qry As String qry = "select * from sample where name='" & Text1.Text & "'" rs.Open qry, cn, adOpenDynamic, adLockOptimistic If rs.EOF And rs.BOF Then rs.AddNew rs!Name = Text1.Text rs!age = Text2.Text rs.Update rs.Close MsgBox "created" Else MsgBox "entry exists.", vbOKOnly + vbInformation, "Group exists !!!" rs.Close End If Exit Sub End Sub
Private Sub UPDATE_Click()
On Error GoTo updateerror cn.Execute " update sample set name = '" & Text1.Text & "',age = '" & Text2.Text & "'where name = '" & Text1.Text & "' " MsgBox "modified" Exit Sub updateerror: MsgBox Err.Description, vbOKOnly + vbInformation, "error" End Sub
Private Sub DELETE_Click()
Dim ans As String ans = MsgBox("are u sure", vbYesNo + vbQuestion, "deleting") If ans = vbNo Then Exit Sub cn.Execute ("delete from sample where name='" & Text1.Text & "'") MsgBox "data deleted" Text1.Text = "" Text2.Text = "" End Sub
Private Sub FIND_Click()
Dim ans, qry As String ans = InputBox("enter name to find") qry = "select * from sample where name ='" & ans & "'" rs.Open qry, cn, adOpenDynamic, adLockOptimistic If rs.EOF And rs.BOF Then rs.Close MsgBox "sorry no record" Else Text1.Text = rs!Name Text2.Text = rs!age rs.Close End If End Sub
Private Sub Form_Load()
Set cn = New ADODB.Connection Set rs = New ADODB.Recordset cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\E1\Desktop\new.mdb;Persist Security Info=False" cn.Open End Sub
Private Sub display()
cn.Open rs.Open "sample", cn, adOpenDynamic, adLockOptimistic Text1.Text = rs!Name Text2.Text = rs!age End Sub
Private Sub report_Click()
Dim ans As String ans = InputBox("enter name") DataEnvironment1.Command1 ans DataReport1.Caption = "Details" DataReport1.Show End Sub