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'"