?? formpec.frm
字號:
Left = 3240
TabIndex = 5
Top = 2760
Width = 1215
End
Begin VB.ComboBox CmbCarID
Height = 300
Left = -73440
Style = 2 'Dropdown List
TabIndex = 4
Top = 720
Width = 2175
End
Begin VB.ComboBox CmbDriverID
Height = 300
Left = -73440
Style = 2 'Dropdown List
TabIndex = 3
Top = 1200
Width = 2175
End
Begin MSComCtl2.DTPicker DTPQueDate
Height = 375
Left = 2280
TabIndex = 2
Top = 1680
Width = 2175
_ExtentX = 3836
_ExtentY = 661
_Version = 393216
Format = 20774913
CurrentDate = 36526
End
Begin MSComCtl2.DTPicker DTPDate
Height = 375
Left = -69000
TabIndex = 20
Top = 720
Width = 2175
_ExtentX = 3836
_ExtentY = 661
_Version = 393216
Format = 20774913
CurrentDate = 38718
End
Begin VB.Label Label8
Caption = "違章原因:"
Height = 375
Index = 1
Left = -74760
TabIndex = 29
Top = 1680
Width = 1455
End
Begin VB.Label Label10
Caption = "備注:"
Height = 375
Index = 0
Left = -74760
TabIndex = 25
Top = 2040
Width = 735
End
Begin VB.Label Label8
Caption = "違章罰款(元):"
Height = 375
Index = 0
Left = -70440
TabIndex = 24
Top = 1320
Width = 1455
End
Begin VB.Label Label4
Caption = "違章日期:"
Height = 255
Index = 0
Left = -70440
TabIndex = 23
Top = 840
Width = 1215
End
Begin VB.Label Label2
Caption = "違章司機ID:"
Height = 255
Index = 0
Left = -74760
TabIndex = 22
Top = 1200
Width = 1215
End
Begin VB.Label Label1
Caption = "違章車輛ID:"
Height = 255
Index = 0
Left = -74760
TabIndex = 21
Top = 720
Width = 1215
End
End
End
Attribute VB_Name = "FormPec"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'返回按鈕
Private Sub CmdBack_Click()
Me.LvResult.Visible = False
Me.CmdBack.Visible = False
End Sub
'刪除按鈕
Private Sub CmdDel_Click()
If MsgBox("確定要刪除選定檔案嗎?", vbOKCancel, "刪除違章記錄") = vbOK Then
Adodc1.Recordset.Delete
MsgBox "刪除成功!", , "刪除違章記錄"
CmdEmpty_Click
End If
Exit Sub
End Sub
'清空按鈕
Private Sub CmdEmpty_Click()
Me.TxtReason.Text = ""
Me.DTPDate.Value = Format(Now, "yyyy-mm-dd")
Me.TxtPay.Text = ""
Me.TxtRemark.Text = ""
End Sub
'修改按鈕
Private Sub CmdMod_Click()
DataGrid1.Columns(1).Text = Me.CmbCarID.Text
DataGrid1.Columns(2).Text = Me.CmbDriverID.Text
DataGrid1.Columns(3).Text = Me.TxtReason.Text
DataGrid1.Columns(4).Text = Me.DTPDate.Value
DataGrid1.Columns(5).Text = Me.TxtPay.Text
DataGrid1.Columns(6).Text = Me.TxtRemark.Text
MsgBox "修改成功", , "修改違章記錄"
End Sub
'查詢按鈕
Private Sub CmdQue_Click()
Dim Questr As String
Dim RsQuery As New ADODB.Recordset
Dim LtItm As ListItem
Dim Remark As String
Dim i As Integer
'按車輛ID查詢
If Me.OptQue(0).Value = True Then
'判斷查詢條件
If Me.TxtQueCar.Text = "" Then
MsgBox "請輸入要查詢的車輛ID!", , "查詢違章記錄"
Exit Sub
End If
'生成查詢語句
Questr = "select * from PecRec where PecCarID = " & Val(Me.TxtQueCar.Text)
'按司機ID查詢
ElseIf Me.OptQue(1).Value = True Then
'判斷查詢條件
If Me.TxtQueDriver.Text = "" Then
MsgBox "請輸入要查詢的司機ID!", , "查詢違章記錄"
Exit Sub
End If
'生成查詢語句
Questr = "select * from PecRec where PecDriverID = " & Val(Me.TxtQueDriver.Text)
'按違章日期查詢
ElseIf Me.OptQue(2).Value = True Then
'生成查詢語句
Questr = "select * from PecRec where PecDate= #" & Me.DTPQueDate.Value & "#"
'按備注查詢
ElseIf Me.OptQue(3).Value = True Then
'替換單引號
Remark = Replace(Trim(Me.TxtQueRemark.Text), "'", "''")
'生成查詢語句
Questr = "select * from PecRec where Remark Like '%" & Remark & "%'"
End If
'打開數據集
Debug.Print Questr
RsQuery.Open Questr, DBCn, adOpenStatic, adLockOptimistic
'顯示查詢結果
If RsQuery.EOF Then
MsgBox "數據庫中沒有符合要求的記錄!", , "查詢違章記錄"
Exit Sub
End If
Me.LvResult.Visible = True
Me.CmdBack.Visible = True
'清空列表
Me.LvResult.ListItems.Clear
'數據集指針指向第一個記錄
RsQuery.MoveFirst
For i = 1 To RsQuery.RecordCount
Set LtItm = Me.LvResult.ListItems.Add()
LtItm.Text = RsQuery.Fields("PecID").Value
LtItm.SubItems(1) = RsQuery.Fields("PecCarID").Value
LtItm.SubItems(2) = RsQuery.Fields("PecDriverID").Value
LtItm.SubItems(3) = RsQuery.Fields("PecReason").Value
LtItm.SubItems(4) = RsQuery.Fields("PecDate").Value
LtItm.SubItems(5) = RsQuery.Fields("PecCost").Value
If RsQuery.Fields("Remark").Value <> "" Then
LtItm.SubItems(6) = RsQuery.Fields("Remark").Value
End If
'數據集指針指向下一條記錄
RsQuery.MoveNext
Next i
'關閉數據集
RsQuery.Close
End Sub
'DataGrid控件中的焦點變換
Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
'檢驗是否為空行
If DataGrid1.Columns(0).Text = "" Then
Exit Sub
End If
'將DataGrid中數據讀入各個控件顯示
Me.CmbCarID.Text = DataGrid1.Columns(1).Text
Me.CmbDriverID.Text = DataGrid1.Columns(2).Text
Me.TxtReason.Text = DataGrid1.Columns(3).Text
Me.DTPDate.Value = DataGrid1.Columns(4).Text
Me.TxtPay.Text = DataGrid1.Columns(5).Text
Me.TxtRemark.Text = DataGrid1.Columns(6).Text
End Sub
Private Sub Form_Load()
Dim RsDB As New ADODB.Recordset
Dim i As Integer
'初始化日期
Me.DTPDate.Value = Format(Now, "yyyy-mm-dd")
Me.DTPQueDate.Value = Format(Now, "yyyy-mm-dd")
'初始化ADO控件,連接數據庫,設置列首
Adodc1.ConnectionString = CnStr
Adodc1.RecordSource = "Select PecID as 違章記錄號," & _
"PecCarID as 違章車輛ID," & _
"PecDriverID as 違章司機ID," & _
"PecReason as 違章原因," & _
"PecDate as 違章日期," & _
"PecCost as 違章罰款," & _
"Remark as 備注 " & _
"From PecRec"
Debug.Print Adodc1.RecordSource
Set DataGrid1.DataSource = Adodc1 '不能缺少
'讀入已有車輛ID和司機ID
RsDB.Open "select CarID from CarInfo order by CarID ", DBCn, adOpenStatic, adLockReadOnly, -1
If RsDB.RecordCount > 0 Then
If Not RsDB.BOF Then RsDB.MoveFirst
For i = 1 To RsDB.RecordCount
Me.CmbCarID.AddItem (RsDB.Fields("CarID").Value)
If Not RsDB.EOF Then RsDB.MoveNext
Next i
Else
MsgBox "還沒有車輛檔案,不能添加違章記錄", , "車輛違章記錄管理"
End If
RsDB.Close
RsDB.Open "select DriverID from DriverInfo order by DriverID ", DBCn, adOpenStatic, adLockReadOnly, -1
If RsDB.RecordCount > 0 Then
If Not RsDB.BOF Then RsDB.MoveFirst
For i = 1 To RsDB.RecordCount
Me.CmbDriverID.AddItem (RsDB.Fields("DriverID").Value)
If Not RsDB.EOF Then RsDB.MoveNext
Next i
Else
MsgBox "還沒有司機檔案,不能添加違章記錄", , "車輛違章記錄管理"
End If
RsDB.Close
End Sub
'添加違章記錄
Private Sub CmdAdd_Click()
Dim rsAdd As New ADODB.Recordset
Dim SqlStr As String
Dim Remark As String
'首先檢驗輸入
'沒有選擇車輛ID
If Len(Trim(Me.CmbCarID.Text)) <= 0 Then
MsgBox "請選擇車輛ID!", , "添加違章記錄"
Exit Sub
End If
'沒有選擇司機ID
If Len(Trim(Me.CmbDriverID.Text)) <= 0 Then
MsgBox "請選擇司機ID!", , "添加違章記錄"
Exit Sub
End If
'沒有違章原因
If Len(Trim(Me.TxtReason.Text)) <= 0 Then
MsgBox "請輸入違章原因!", , "添加違章記錄"
Exit Sub
End If
'沒有輸入違章罰款
If Len(Trim(Me.TxtPay.Text)) <= 0 Then
MsgBox "請輸入違章罰款!", , "添加違章記錄"
Exit Sub
End If
'檢驗完畢,數據入庫,備注項可選
If Me.TxtRemark.Text = vbNullString Then '沒有備注項
SqlStr = "INSERT INTO PecRec"
SqlStr = SqlStr & "(PecCarID,PecDriverID,PecReason,PecDate,PecCost) "
SqlStr = SqlStr & "VALUES ('" & Me.CmbCarID.Text & "',"
SqlStr = SqlStr & "'" & Me.CmbDriverID.Text & "',"
SqlStr = SqlStr & "'" & Me.TxtReason.Text & "',"
SqlStr = SqlStr & "#" & Me.DTPDate.Value & "#,"
SqlStr = SqlStr & "'" & Me.TxtPay.Text & "');"
Debug.Print SqlStr
DBCn.Execute SqlStr
Else '有備注項
Remark = Replace(Trim(Me.TxtRemark.Text), "'", "''")
SqlStr = "INSERT INTO PecRec"
SqlStr = SqlStr & "(PecCarID,PecDriverID,PecReason,PecDate,PecCost,Remark) "
SqlStr = SqlStr & "VALUES ('" & Me.CmbCarID.Text & "',"
SqlStr = SqlStr & "'" & Me.CmbDriverID.Text & "',"
SqlStr = SqlStr & "'" & Me.TxtReason.Text & "',"
SqlStr = SqlStr & "#" & Me.DTPDate.Value & "#,"
SqlStr = SqlStr & "'" & Me.TxtPay.Text & "',"
SqlStr = SqlStr & "'" & Remark & "');"
Debug.Print SqlStr
DBCn.Execute SqlStr
End If
MsgBox "添加成功", , "添加違章記錄"
Adodc1.Refresh
End Sub
'設置焦點對應的單選按鈕
Private Sub TxtQueCar_GotFocus()
Me.OptQue(0).Value = True
EmptyQue
End Sub
Private Sub TxtQueDriver_GotFocus()
Me.OptQue(1).Value = True
EmptyQue
End Sub
Private Sub DTPQueDate_Click()
Me.OptQue(2).Value = True
EmptyQue
End Sub
Private Sub TxtQueRemark_GotFocus()
Me.OptQue(3).Value = True
EmptyQue
End Sub
'清空查詢內容函數
Private Sub EmptyQue()
Me.TxtQueCar.Text = ""
Me.TxtQueDriver.Text = ""
Me.DTPQueDate.Value = "2006-1-1"
Me.TxtQueRemark.Text = ""
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -