You are on page 1of 8

User Form Dim gerConnection As ADODB.Connection Dim gerRecordset As ADODB.

Recordset Private Sub btnLogin_Click() Call UserMO End Sub Private Sub Form_Load() mdlConnect.CodeForAll End Sub Public Sub UserMO() Set gerConnection = New ADODB.Connection Set gerRecordset = New ADODB.Recordset Dim sqlCommand As String gerConnection.Open mdlConnect.connectionString sqlCommand = "SELECT * FROM tblUser WHERE " & _ "UserName= " & "'" & txtUserName.Text & "'" & _ "'" gerRecordset.Open sqlCommand, gerConnection, adOpenForwardOnly, adLockReadOnly If Not (gerRecordset.EOF) Then frmMain.Show Unload Me Else MsgBox "ULOL MALI", vbExclamation, "LOGIN" End If End Sub "AND PassKey= " & "'" & txtPassword.Text &

Main Form Dim gerConnection As ADODB.Connection Dim gerRecordset As ADODB.Recordset Dim sectorID As Integer Private Sub btnAdd_Click() Call AddFiles End Sub Private Sub btnDelete_Click() Call DeleteFiles End Sub Private Sub btnUpdate_Click() Call UpdateFiles End Sub Private Sub mnu_Exit_Click() End End Sub Private Sub mnu_Search_Click() frmSearch.Show Unload Me End Sub Private Sub txtAgentID_Change() Call ReadFiles End Sub Private Sub Form_Load() mdlConnect.CodeForAll Call bindSector End Sub Private Sub cboSector_Click() Call setNumberSector End Sub

'subroutines================================================= ================================= Public Sub bindSector() Set gerConnection = New ADODB.Connection Set gerRecordset = New ADODB.Recordset Dim sqlCommand As String sqlCommand = "SELECT * FROM tblSector ORDER BY Sector" gerConnection.Open mdlConnect.connectionString gerRecordset.Open sqlCommand, gerConnection, adOpenForwardOnly, adLockReadOnly Do Until gerRecordset.EOF cboSector.AddItem gerRecordset.Fields("Sector").Value gerRecordset.MoveNext Loop End Sub Public Sub setNumberSector() Set gerConnection = New ADODB.Connection Set gerRecordset = New ADODB.Recordset Dim sqlCommand As String sqlCommand = "SELECT * FROM tblSector ORDER BY Sector" gerConnection.Open mdlConnect.connectionString gerRecordset.Open sqlCommand, gerConnection, adOpenForwardOnly, adLockReadOnly Do Until gerRecordset.EOF If (cboSector.Text = gerRecordset.Fields("Sector").Value) Then sectorID = gerRecordset.Fields("SectorID").Value End If gerRecordset.MoveNext Loop End Sub

Public Sub AddFiles() Set gerConnection = New ADODB.Connection Dim sqlCommand As String gerConnection.Open mdlConnect.connectionString sqlCommand = "INSERT INTO tblAgent VALUES " & _ "('" & txtAgentID.Text & "'," & _ "'" & txtLastName.Text & "'," & _ "'" & txtFirstName.Text & "'," & _ txtSalary.Text & "," & sectorID & ")" gerConnection.Execute sqlCommand Call Cleardata MsgBox "File Added", vbInformation, "Add File" End Sub Public Sub ReadFiles() Set gerConnection = New ADODB.Connection Set gerRecordset = New ADODB.Recordset Dim sqlCommand As String sqlCommand = "SELECT * FROM tblAgent " & _ "INNER JOIN tblSector " & _ "ON tblAgent.SectorID = tblSector.SectorID " "WHERE AgentID= " & "'" & txtAgentID.Text & "'" gerConnection.Open mdlConnect.connectionString gerRecordset.Open sqlCommand, gerConnection, adOpenForwardOnly, adLockReadOnly If Not (gerRecordset.EOF) Then txtLastName.Text = gerRecordset.Fields("LastName").Value txtFirstName.Text = gerRecordset.Fields("FirstName").Value txtSalary.Text = gerRecordset.Fields("Salary").Value cboSector.Text = gerRecordset.Fields("Sector").Value Else

& _

txtLastName.Text = "" txtFirstName.Text = "" txtSalary.Text = "" cboSector.Text = "" End If gerConnection.Execute sqlCommand End Sub Public Sub UpdateFiles() Set gerConnection = New ADODB.Connection Dim sqlCommand As String gerConnection.Open mdlConnect.connectionString Call setNumberSector sqlCommand = "UPDATE tblAgent SET " & _ "LastName = " & "'" & txtLastName.Text & "'," & _ "FirstName = " & "'" & txtFirstName.Text & "'," & _ "Salary = " & txtSalary.Text & "," & _ "SectorID = " & sectorID & _ " WHERE AgentID = " & "'" & txtAgentID.Text & "'" gerConnection.Execute sqlCommand Call Cleardata MsgBox "File Updated", vbInformation, "Update File" End Sub

Public Sub DeleteFiles() Set gerConnection = New ADODB.Connection Dim sqlCommand As String gerConnection.Open mdlConnect.connectionString sqlCommand = "DELETE FROM tblAgent " & _ "WHERE AgentID= " & "'" & txtAgentID.Text & "'" gerConnection.Execute sqlCommand Call Cleardata MsgBox "File Deleted", vbInformation, "Delete" End Sub Public Sub Cleardata() txtAgentID.Text = "" txtLastName.Text = "" txtFirstName.Text = "" txtSalary.Text = "" cboSector.Text = "" End Sub

Search Form Dim gerConnection As ADODB.Connection Dim gerRecordset As ADODB.Recordset Private Sub mnu_Exit_Click() End End Sub Private Sub mnu_Main_Click() frmMain.Show Unload Me End Sub Private Sub Form_Load() mdlConnect.CodeForAll Set gerConnection = New ADODB.Connection Set gerRecordset = New ADODB.Recordset gerConnection.Open mdlConnect.connectionString With gerRecordset .ActiveConnection = gerConnection .CursorLocation = adUseClient .CursorType = adOpenForwardOnly .LockType = adLockOptimistic .Source = "SELECT AgentID, " & _ "LastName, " & _ "FirstName, " & _ "Salary, " & _ "Sector " & _ "FROM tblAgent " & _ "INNER JOIN tblSector " & _ "ON tblAgent.SectorID=tblSector.SectorID " & _ "ORDER BY LastName" .Open End With Set dgSearch.DataSource = gerRecordset End Sub

Private Sub txtSearch_Change() Set gerConnection = New ADODB.Connection Set gerRecordset = New ADODB.Recordset gerConnection.Open mdlConnect.connectionString With gerRecordset .ActiveConnection = gerConnection .CursorLocation = adUseClient .CursorType = adOpenForwardOnly .LockType = adLockOptimistic .Source = "SELECT AgentID, " & _ "LastName, " & _ "FirstName, " & _ "Salary, " & _ "Sector " & _ "FROM tblAgent " & _ "INNER JOIN tblSector " & _ "ON tblAgent.SectorID=tblSector.SectorID " & _ "WHERE LastName Like " & "'%" & txtSearch.Text & "%'" & _ "ORDER BY LastName" .Open End With Set dgSearch.DataSource = gerRecordset End Sub Module Global connectionString As String Public Sub CodeForAll() connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Agent.mdb" End Sub

You might also like