vb資料庫代碼
① VB實現ACCESS資料庫記錄的查詢,新增,刪除,修改的代碼是什麼
「修改」按鈕的程序 x0dx0aPrivate Sub cmdEdit_Click() x0dx0asql = "select * from 停時禪橡旅統如液計 where date = cdate('" & Text8.Text & "') and ycqk = '" & Combo1.Text & "'and id = '" & DataGrid1.Columns(2).CellText(DataGrid1.Bookmark) & "'" x0dx0ars.Open sql, dm, adOpenDynamic, adLockOptimistic x0dx0ars.Fields("id") = Text7.Text x0dx0ars.Fields("ycqk") = Combo1.Text x0dx0ars.Fields("date1") = Text1.Text x0dx0ars.Fields("time1") = Text2.Text x0dx0ars.Fields("date2") = Text3.Text x0dx0ars.Fields("time2") = Text4.Text x0dx0ars.Update x0dx0ars.Close x0dx0aEnd Sub x0dx0ax0dx0a'「刪除」按鈕的程序 x0dx0aPrivate Sub cmdDelete_Click() x0dx0astrFCode = DataGrid1.Columns(0).CellText(DataGrid1.Bookmark) x0dx0astrSCode = DataGrid1.Columns(2).CellText(DataGrid1.Bookmark) x0dx0astrCCode = DataGrid1.Columns(1).CellText(DataGrid1.Bookmark) x0dx0asql = "select * from 停時統計 where date='" & strFCode & "' and id='" & strSCode & "' and ycqk='" & strCCode & "'" x0dx0ars.Open sql, dm, adOpenDynamic, adLockOptimistic x0dx0ars.Delete x0dx0ars.Update x0dx0ars.Close x0dx0aEnd Sub x0dx0ax0dx0a'「增加」按鈕的程序 x0dx0aPrivate Sub Command1_Click() x0dx0asql = "select * from 停時賀凳統計 order by id" x0dx0ars.Open sql, dm, adOpenDynamic, adLockOptimistic x0dx0ars.AddNew x0dx0ars.Fields("date") = Date x0dx0ars.Fields("id") = Text7.Text - 1 x0dx0ars.Fields("ycqk") = Combo1.Text x0dx0ars.Fields("date1") = Text1.Text x0dx0ars.Fields("time1") = Text2.Text x0dx0ars.Fields("date2") = Text3.Text x0dx0ars.Fields("time2") = Text4.Text x0dx0ars.Update x0dx0ars.Close x0dx0ax0dx0aWith Adodc1 x0dx0aAdodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False" x0dx0aAdodc1.RecordSource = "select * from 停時統計 where date = cdate('" & Text8.Text & "') and ycqk = '" & Combo1.Text & "' order by id" x0dx0aAdodc1.Refresh x0dx0aDataGrid1.Refresh x0dx0aEnd With x0dx0aEnd Sub
② vb資料庫編程,資料庫某些欄位不能重復,如果錄入數據有重復,保存時,程序自動提醒,怎麼編寫自動提醒代碼。
用Sql語句,查詢或者Find方法查詢,如果已經存在該記錄,就提醒:
如果一下子要保存n行,那有兩種選擇:
1:檢查一行,保存一行。凡是沒有重復的都保存,有重復的不保存。
2:先一一檢查,檢查到有重復就提示,並結束。
不知道你用的是控制項還是對象連接的資料庫?假設你用Adodc控制項,且已經連接。
下面:假設欄位a,b為字元類型,以一行數據為例,a欄位值為「AA」,b欄位值為「BB」,
Adodc1.RecordSource ="Select * From s Where a ='AA' and b = 'BB'"
Adodc1.Refresh
If Not (Adodc1.Recordset.EOF And Adodc1.Recordset.BOF) Then
MsgBox "已經存在相同記錄!"
Exit Sub
Else
'下面編寫添加記錄的代碼
End If
上面是當數據a,b一樣提示,如果a一樣或者b一樣就提示,那麼:
Adodc1.RecordSource ="Select * From s Where a ='AA' Or b = 'BB'"