1. 程式人生 > >VB資料庫程式設計實現新增、查詢、修改、刪除

VB資料庫程式設計實現新增、查詢、修改、刪除


Private Sub Command1_Click()
Form10.Hide
Form4.Show

End Sub

Private Sub Command10_Click()
For i = 0 To 7
If Text1(i).Text <> "" Then
Exit For
End If
Next i
If i = 8 Then
MsgBox ("不能儲存空記錄!")
Text1(0).SetFocus
Exit Sub
End If

If Text1(0).Text = "" Then
MsgBox ("年級不能為空")
Text1(0).SetFocus
Exit Sub
End If
If Text1(1).Text = "" Then
MsgBox ("姓名不能為空")
Text1(1).SetFocus
Exit Sub
End If


If Text1(2).Text = "" Then
MsgBox ("專業不能為空")
Text1(2).SetFocus
Exit Sub
End If

If Text1(3).Text = "" Then
MsgBox ("時間不能為空")
Text1(3).SetFocus
Exit Sub
End If

Adodc1.Recordset.Update
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = True
Command9.Enabled = True
Command10.Enabled = False
Command11.Enabled = False
For i = 0 To 7
Text1(i).Enabled = False
Next i

MsgBox ("儲存成功")

End Sub

Private Sub Command11_Click()
If Adodc1.Recordset.RecordCount = 0 Then
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = True
Command9.Enabled = True
Command10.Enabled = False
Command11.Enabled = False
Exit Sub
End If
Adodc1.Recordset.CancelUpdate
Adodc1.Refresh
For i = 0 To 7
Text1(i).Enabled = False
Next i
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = True
Command9.Enabled = True
Command10.Enabled = False
Command11.Enabled = False
End Sub

Private Sub Command12_Click()
Set DataGrid1.DataSource = Adodc1
DataGrid1.Visible = True
End Sub

Private Sub Command2_Click()
If Adodc1.Recordset.RecordCount = 0 Then
Exit Sub
End If
Adodc1.Recordset.MoveFirst
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = True
Command9.Enabled = True
Command10.Enabled = False
Command11.Enabled = False
End Sub

Private Sub Command3_Click()
If Adodc1.Recordset.RecordCount = 0 Then
Exit Sub
End If
Adodc1.Recordset.MovePrevious
If Adodc1.Recordset.BOF Then Adodc1.Recordset.MoveFirst
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = True
Command9.Enabled = True
Command10.Enabled = False
Command11.Enabled = False
End Sub

Private Sub Command4_Click()
If Adodc1.Recordset.RecordCount = 0 Then
Exit Sub
End If
Adodc1.Recordset.MoveNext
If Adodc1.Recordset.EOF Then Adodc1.Recordset.MoveLast
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = True
Command9.Enabled = True
Command10.Enabled = False
Command11.Enabled = False
End Sub

Private Sub Command5_Click()
If Adodc1.Recordset.RecordCount = 0 Then
Exit Sub
End If
Adodc1.Recordset.MoveLast
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command8.Enabled = True
Command9.Enabled = True
Command10.Enabled = False
Command11.Enabled = False
End Sub

Private Sub Command6_Click()

Adodc1.Recordset.AddNew
For i = 0 To 7
Text1(i).Enabled = True
Text1(i).Text = ""
Text1(0).SetFocus
Next i
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command8.Enabled = False
Command9.Enabled = False
Command10.Enabled = True
Command11.Enabled = True

End Sub

Private Sub Command7_Click()
If Adodc1.Recordset.RecordCount = 0 Then
MsgBox ("沒有要刪除的記錄")
Exit Sub
End If
Dim str As String
str = MsgBox("真的要刪除此記錄麼?", vbYesNo)
If str = vbYes Then
Adodc1.Recordset.Delete
Adodc1.Recordset.MoveNext
If Adodc1.Recordset.EOF Then Adodc1.Recordset.MoveLast
End If


End Sub

Private Sub Command8_Click()
'On Error Resume Next

Dim str As String
'Adodc1.CursorType = adOpenDynamic

 str = InputBox("請輸入社員年級", "請查詢")
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "SELECT  * FROM  社員  WHERE 年級= '" & str & " ' "
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = " select * from 社員 "
Adodc1.Refresh
MsgBox "沒有此社員", , "查詢"
End If
' If
'Dim mzy As String
'mzy = InputBox("請輸入年級號", "請查詢")
'Adodc1.CommandType = adCmdText
'Adodc1.RecordSource = "Select   *  From 社員 Where  年級=  '" & mzy & " '"
'Adodc1.Refresh
'If Adodc1.Recordset.EOF Then
'MsgBox "無此讀者號!", , "提示"

End Sub

Private Sub Command9_Click()
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command8.Enabled = False
Command9.Enabled = False
Command10.Enabled = True
Command11.Enabled = True
If Adodc1.Recordset.RecordCount <> 0 Then
For i = 0 To 7
Text1(i).Enabled = True
Next i
Else
MsgBox ("沒有要修改的記錄")
End If

End Sub

Private Sub Form_Load()
For i = 0 To 7
Text1(i).Enabled = False
Next i
Command10.Enabled = False
Command11.Enabled = False
End Sub

Private Sub Text2_Change()
End Sub