Sei sulla pagina 1di 6

Global cn As ADODB.

Connection
Global rs As New ADODB.Recordset

Public Sub Datcon()


Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0.;" & _


"Data Source=" & App.Path & "\Database\Database.mdb;Persist Security Info =
False;"
cn.Open

End Sub

Option Explicit
Dim Msg As String

Sub load_list()
Datcon
With rs
If .State = adStateOpen Then
.Close
End If
.Open "SELECT * FROM ssection order by ssection.level", cn, adOpenKeyset,
adLockOptimistic
If .EOF = True Then
On Error Resume Next
lstSection.ListItems.clear

Exit Sub
End If

lstSection.ListItems.clear
Do While .EOF = False
On Error Resume Next

lstSection.ListItems.Add , , , , 1

lstSection.ListItems(lstSection.ListItems.Count).SubItems(1) = ![sec]
lstSection.ListItems(lstSection.ListItems.Count).SubItems(2) = ![Level]
.MoveNext
Loop

End With
End Sub

Private Sub btnAdd_Click()


clear
enable
btnAdd.Enabled = False
btnAdd.Visible = False
btnDelete.Enabled = False
btnUpdate.Enabled = False
btnSaveu.Enabled = False
cbxLevel.SetFocus

End Sub

Private Sub btnClose_Click()


Unload Me
End Sub

Private Sub btnDelete_Click()


Datcon

With rs
If .State = adStateOpen Then
.Close
End If
Msg = MsgBox("Do you want to delete this section?", vbQuestion + vbYesNo)
If Msg = vbYes Then
.Open "delete *from ssection where sec = '" & txtSection.Text & "'", cn,
adOpenKeyset, adLockOptimistic
MsgBox " Section has been deleted", vbInformation
load_list
Else
Exit Sub
End If
End With

clear
disable
btnDelete.Enabled = False
btnUpdate.Enabled = False
btnSaveu.Enabled = False
End Sub

Private Sub btnSave_Click()

Datcon

With rs
If .State = adStateOpen Then
.Close
End If

.Open "Select * from ssection where sec ='" & txtSection.Text & "'", cn,
adOpenKeyset, adLockOptimistic

If rs.RecordCount >= 1 Then


MsgBox "Section already existing!", vbExclamation
txtSection.Text = ""

ElseIf txtSection.Text = "" Or cbxLevel.Text = "" Then


MsgBox "One of the fields are empty!", vbExclamation
Exit Sub

Else
.AddNew
!sec = txtSection.Text
!Level = cbxLevel.Text
.Update
MsgBox "New section has been added!", vbInformation
btnAdd.Visible = True
btnAdd.Enabled = True
btnDelete.Enabled = False
btnUpdate.Enabled = False
btnSaveu.Enabled = False
load_list
clear
disable
End If

End With

End Sub

Private Sub btnSaveu_Click()


Datcon

With rs
If .State = adStateOpen Then
.Close
End If

.Open "select * from ssection where sec='" & txtSection.Text & "'", cn,
adOpenKeyset, adLockOptimistic

If txtSection.Text = "" Or cbxLevel.Text = "" Then


MsgBox "One of the fields are empty!", vbExclamation
Exit Sub

Else
Msg = MsgBox("Do you want to update this section?", vbQuestion + vbYesNo)
If Msg = vbYes Then
If .EOF Then
On Error Resume Next
End If
.Update
!sec = txtSection.Text
!Level = cbxLevel.Text
.Update

MsgBox " Section has been updated", vbInformation


load_list
btnUpdate.Enabled = False
btnSaveu.Enabled = False
btnUpdate.Visible = True
btnDelete.Enabled = False
btnAdd.Enabled = True
btnSave.Enabled = True
disable

Else
Exit Sub
End If
End If

End With

End Sub

Private Sub btnUpdate_Click()


btnUpdate.Enabled = False
btnUpdate.Visible = False
enable
btnDelete.Enabled = False
btnAdd.Enabled = False
btnSave.Enabled = False
txtSection.SetFocus
End Sub

Private Sub cbxlevel_Click()


sort
End Sub

Private Sub Form_Load()


disable
choices
load_list
End Sub

Sub choices()
Datcon
With rs
If .State = adStateOpen Then
.Close
End If

.Open "select*from slevel", cn, adOpenKeyset, adLockOptimistic


Do While Not rs.EOF
On Error Resume Next
cbxLevel.AddItem rs!Level
.MoveNext
Loop
End With
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, y As
Single)
Static mousex As String
Static mousey As String

Select Case Button

Case 0
mousex = X
mousey = y

Case 1
Me.Left = Me.Left + (X - mousex)
Me.Top = Me.Top + (y - mousey)
End Select

End Sub

Private Sub lstSection_ItemClick(ByVal Item As MSComctlLib.ListItem)


Datcon
txtSection.Text = lstSection.SelectedItem.SubItems(1)
cbxLevel.Text = lstSection.SelectedItem.SubItems(2)

btnDelete.Enabled = True
btnUpdate.Enabled = True
btnSaveu.Enabled = True
End Sub

Sub enable()
txtSection.Enabled = True
End Sub
Sub disable()
txtSection.Enabled = False
End Sub
Sub clear()
txtSection.Text = ""
cbxLevel.Text = ""

End Sub

Sub sort()
Datcon
With rs
If .State = adStateOpen Then
.Close
End If

.Open "select*from ssection where level='" & cbxLevel.Text & "'", cn, adOpenKeyset,
adLockOptimistic
If .EOF = True Then
On Error Resume Next
lstSection.ListItems.clear

Exit Sub
End If
lstSection.ListItems.clear
Do While .EOF = False
On Error Resume Next

lstSection.ListItems.Add , , , , 1

lstSection.ListItems(lstSection.ListItems.Count).SubItems(1) = ![sec]
lstSection.ListItems(lstSection.ListItems.Count).SubItems(2) = ![Level]

.MoveNext
Loop

End With
End Sub

Private Sub Label1_Click()

End Sub

Private Sub txtSection_LostFocus()


txtSection.Text = cSentenceCase(txtSection.Text)
End Sub

Sub box()
Datcon
Dim Getdoc As String

With rs
If .State = adStateOpen Then
.Close
End If
.Open " SELECT Max(sid.idnum) AS MaxOfidnum" & _
" FROM sid;", cn, adOpenKeyset, adLockOptimistic
If IsNull(rs(0).Value) Then
Getdoc = Format(1, "00000")
Else
Getdoc = Format(rs(0).Value + 1, "00000")
End If
lblDoc.Caption = "MDCS- EF-" & Getdoc
End With
End Sub

Potrebbero piacerti anche