Tuesday 23 May 2017

VB Project 2017


Module Code
==============================================================

Option Explicit

Public con As New Connection
Public rs As New Recordset
Public num As Integer

Public Sub connect()
If con.State = 1 Then
con.Close
End If
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data Source=D:\Project_2017\project2017.mdb"
con.Open
MsgBox ("Conected")

End Sub

Public Sub num_query(ByVal q As String)

If rs.State = 1 Then
rs.Close
End If
rs.Open q, con, adOpenDynamic, adLockPessimistic
If rs.BOF Then
num = 0
Else
num = rs.Fields(0).Value
End If

End Sub
Public Sub inupdel(ByVal q As String)
If rs.State = 1 Then
rs.Close
End If
rs.Open q, con, adOpenDynamic, adLockPessimistic
End Sub
Public Sub sel_query(ByVal q As String)
If rs.State = 1 Then
rs.Close
End If
rs.CursorLocation = adUseClient
rs.Open q, con, adOpenDynamic, adLockPessimistic

End Sub
=========================================================================

Student Info Form Code
Option Explicit

Private Sub cmdAdd_Click()
Module1.num_query ("select max(s_rl)+1 from student")
txtRl.Text = num
txtRl.Enabled = False
cmdAdd.Enabled = False
cmdSave.Enabled = True
End Sub

Private Sub cmdNext_Click()
rs.MoveNext
If rs.EOF Then
rs.MoveLast
MsgBox ("This is last Record")
End If
Call loaddata
End Sub

Private Sub cmdPrev_Click()
rs.MovePrevious
If rs.BOF Then
rs.MoveFirst
MsgBox ("This is First Record")
End If
Call loaddata
End Sub

Private Sub cmdSave_Click()
If txtAdd.Text <> "" And txtmb.Text <> "" And txtnm.Text <> "" And txtRl.Text <> "" And cmbClass.Text <> "" Then
Module1.inupdel ("insert into student values(" & CInt(txtRl.Text) & ",'" & txtnm.Text & "','" & txtmb.Text & "','" & txtAdd.Text & "','" & cmbClass.Text & "')")
MsgBox "Student Added Successfully", vbInformation + vbOKOnly, "Insertion"
cmdAdd.Enabled = True
cmdSave.Enabled = False
txtRl.Text = ""
txtnm.Text = ""
txtAdd.Text = ""
txtmb.Text = ""
cmbClass.Text = "Select"
Else
MsgBox "Fields Should not be empty", vbCritical + vbOKOnly, "Error"
End If
End Sub

Private Sub cmdSearch_Click()
Dim rl As Integer
If txtSrch.Text <> "" Then
rl = CInt(txtSrch.Text)
Module1.sel_query ("select * from student where s_rl= " & rl & " ")
If rs.BOF Then
txtRl.Text = ""
txtnm.Text = ""
txtAdd.Text = ""
txtmb.Text = ""
cmbClass.Text = "Select"
MsgBox ("Record Not Found")
Else
txtRl.Text = rs.Fields(0).Value
txtnm.Text = rs.Fields(1).Value
txtmb.Text = rs.Fields(2).Value
txtAdd.Text = rs.Fields(3).Value
cmbClass.Text = rs.Fields(4).Value
cmdUpdate.Enabled = True
End If
Else
MsgBox ("Enter Roll no")
End If
End Sub

Private Sub cmdUpdate_Click()
If txtAdd.Text <> "" And txtmb.Text <> "" And txtnm.Text <> "" And txtRl.Text <> "" And cmbClass.Text <> "" Then
Module1.inupdel ("update student set s_name='" & txtnm.Text & "',s_mob='" & txtmb.Text & "',s_add='" & txtAdd.Text & "',s_class='" & cmbClass.Text & "' where s_rl=" & CInt(txtRl.Text) & "")
MsgBox ("Student Info Updated")
Else
MsgBox ("Enter All Fields")
End If
End Sub

Private Sub Command1_Click()
If cmbclssrch.Text = "Select Class to search" Then
MsgBox ("Select Class")
Else
Module1.sel_query ("select * from Student where s_class='" & cmbclssrch.Text & "'")
Set DataGrid1.DataSource = rs
Call loaddata
End If
End Sub

Private Sub Form_Load()
Me.Width = 9450
Me.Height = 7695
Me.Left = 4000
Me.Top = 1000
Module1.sel_query ("select distinct(s_class) from student")
While Not rs.EOF
cmbclssrch.AddItem (rs.Fields(0).Value)
rs.MoveNext
Wend
Module1.sel_query ("select * from student")
Set DataGrid1.DataSource = rs
End Sub

Private Sub loaddata()
txtRl.Text = rs.Fields(0).Value
txtnm.Text = rs.Fields(1).Value
txtmb.Text = rs.Fields(2).Value
txtAdd.Text = rs.Fields(3).Value
cmbClass.Text = rs.Fields(4).Value
End Sub

Private Sub txtmb_KeyPress(KeyAscii As Integer)
'MsgBox (KeyAscii)
If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then

Else
MsgBox "Enter only Number", vbCritical + vbOKOnly, "Invalid Input"
KeyAscii = 0
End If
End Sub

Private Sub txtmb_LostFocus()
If Mid(txtmb.Text, 1, 1) >= 7 And Mid(txtmb.Text, 1, 1) <= 9 And Len(txtmb.Text) = 10 Then

Else
MsgBox "Invalid Mobile Number", vbCritical + vbOKOnly, "Invalid Input"
txtmb.SetFocus
End If

End Sub

Private Sub txtnm_KeyPress(KeyAscii As Integer)
If KeyAscii >= 65 And KeyAscii <= 90 Or KeyAscii >= 97 And KeyAscii <= 122 Or KeyAscii = 32 Or KeyAscii = 8 Then
'nothing to do
Else
MsgBox "Enter only alphabets", vbCritical + vbOKOnly, "Invalid Input"
KeyAscii = 0
End If

End Sub