Monday, December 10, 2012

Design a project for implementing data connectivity with : Dao

Dim db As Database
Dim dbr As Recordset

Private Sub CmdFirst_Click()
dbr.MoveFirst
Text1.Text = dbr.Fields(0)
Text2.Text = dbr.Fields(1)
Text3.Text = dbr.Fields(2)
End Sub
Private Sub CmdLast_Click()
dbr.MoveLast
Text1.Text = dbr.Fields(0)
Text2.Text = dbr.Fields(1)
Text3.Text = dbr.Fields(2)
End Sub
Private Sub CmdNext_Click()
dbr.MoveNext
If dbr.EOF Then
dbr.MoveLast
MsgBox "this is last record"
End If
Text1.Text = dbr.Fields(0)
Text2.Text = dbr.Fields(1)
Text3.Text = dbr.Fields(2)
End Sub
Private Sub CmdPrevious_Click()
dbr.MovePrevious
If dbr.BOF Then
dbr.MoveFirst
MsgBox "this is first record"
End If
Text1.Text = dbr.Fields(0)
Text2.Text = dbr.Fields(1)
Text3.Text = dbr.Fields(2)
End Sub

Private Sub CmdAdd_Click()
dbr.AddNew
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
CmdFirst.Enabled = False
CmdLast.Enabled = False
CmdNext.Enabled = False
CmdPrevious.Enabled = False
CmdCancel.Enabled = True
CmdUpdate.Enabled = True
CmdDelete.Enabled = False
CmdAdd.Enabled = False
CmdEdit.Enabled = False
CmdFind.Enabled = False
End Sub

Private Sub CmdUpdate_Click()
dbr.Fields(0) = Text1.Text
dbr.Fields(1) = Text2.Text
dbr.Fields(2) = Text3.Text
dbr.Update
MsgBox "record updated"
CmdFirst.Enabled = True
CmdLast.Enabled = True
CmdNext.Enabled = True
CmdPrevious.Enabled = True
CmdCancel.Enabled = False
CmdUpdate.Enabled = False
CmdDelete.Enabled = True
CmdAdd.Enabled = True
CmdEdit.Enabled = True
CmdFind.Enabled = True
End Sub
Private Sub CmdDelete_Click()
dbr.Delete
MsgBox "data deleted"
dbr.MoveNext
If dbr.EOF Then
dbr.MoveFirst
ElseIf dbr.BOF Then
MsgBox "no more records"
End If
Text1.Text = dbr.Fields(0)
Text2.Text = dbr.Fields(1)
Text3.Text = dbr.Fields(2)
End Sub

Private Sub CmdCancel_Click()
dbr.CancelUpdate
MsgBox "data not saved"
CmdFirst.Enabled = True
CmdLast.Enabled = True
CmdNext.Enabled = True
CmdPrevious.Enabled = True
CmdCancel.Enabled = False
CmdUpdate.Enabled = False
CmdDelete.Enabled = True
CmdAdd.Enabled = True
CmdEdit.Enabled = True
CmdFind.Enabled = True
End Sub
Private Sub CmdEdit_Click()
dbr.Edit
CmdFirst.Enabled = False
CmdLast.Enabled = False
CmdNext.Enabled = False
CmdPrevious.Enabled = False
CmdCancel.Enabled = True
CmdUpdate.Enabled = True
CmdDelete.Enabled = False
CmdAdd.Enabled = False
CmdEdit.Enabled = False
CmdFind.Enabled = False
End Sub
Private Sub CmdFind_Click()
Dim str As String
str = InputBox("enter book name")
dbr.MoveFirst
Do
If (dbr.Fields(0) = str) Then
MsgBox "record found"
Text1.Text = dbr.Fields(0)
Text2.Text = dbr.Fields(1)
Text3.Text = dbr.Fields(2)
Exit Sub
End If
dbr.MoveNext
Loop Until dbr.EOF
MsgBox "record not found"
End Sub
Private Sub Form_Load()
Set db = DBEngine.Workspaces(0).OpenDatabase("D:\book.mdb")
Set dbr = db.OpenRecordset("mca_book", dbOpenTable)
End Sub
Private Sub Text2_LostFocus()
If Not IsNumeric(Text2.Text) Then
MsgBox "plz enter correct book id"
Text2.Text = ""
Text2.SetFocus
End If
End Sub
Private Sub CmdExit_Click()
End
End Sub   




 Output

Note:- please set some property of your control like caption or id corresponding to your control.
please also set the property of data control like datasource and recordsource and some data bound control like data field etc.
Goto visual basic programs index page