用vb怎么样向access里面增加一条记录,并且要求要验证数据库是否有相同的记录,有的话就不能增加进去。
2个回答
2013-12-02
展开全部
试试这个是不是你要的:Sub addRecord()
On Error GoTo onErr
Set Conn = CreateObject("Adodb.Connection")
Set Rs = CreateObject("Adodb.Recordset")
Rs.CursorLocation = adUseClient
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & [你的数据库] & ";Persist Security Info=False"
Conn.Open
Rs.Open "Select * from [你要增加数据的表] where " & [你判断是否有相同记录的条件], Conn2, 2, 3, -1
if Rs.RecordCount<=0 then '返回的记录数据小于等于0,记录不存在。
Rs.AddNew '增加记录的代码块
Rs("...")=....
.....
Rs.Update
else
MsgBox "记录已存在!", vbCritical, "增加数据"
end if
if Rs.state=1 then Rs.close
if Conn.state=1 then Conn.close
set Rs=Nothing
set Conn=Nothing
Exit Sub
onErr:
MsgBox "错误号:" & Err.Number & vbCrLf & "错误源:" & Err.Source & vbCrLf & "错误信息:" & Err.Description, vbCritical, "错误"
End Sub
On Error GoTo onErr
Set Conn = CreateObject("Adodb.Connection")
Set Rs = CreateObject("Adodb.Recordset")
Rs.CursorLocation = adUseClient
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & [你的数据库] & ";Persist Security Info=False"
Conn.Open
Rs.Open "Select * from [你要增加数据的表] where " & [你判断是否有相同记录的条件], Conn2, 2, 3, -1
if Rs.RecordCount<=0 then '返回的记录数据小于等于0,记录不存在。
Rs.AddNew '增加记录的代码块
Rs("...")=....
.....
Rs.Update
else
MsgBox "记录已存在!", vbCritical, "增加数据"
end if
if Rs.state=1 then Rs.close
if Conn.state=1 then Conn.close
set Rs=Nothing
set Conn=Nothing
Exit Sub
onErr:
MsgBox "错误号:" & Err.Number & vbCrLf & "错误源:" & Err.Source & vbCrLf & "错误信息:" & Err.Description, vbCritical, "错误"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-02
展开全部
On Error Resume Next
Dim Tconnn As New ADODB.Connection
Dim TRss As New ADODB.Recordset
If Tconnn.State = 1 Then Tconnn.Close
If TRss.State = 1 Then TRss.Close
'If Dir(App.Path & "\data\base.ocx") <> "" Then '判断文件是否存在
'MsgBox "在", , ""
'Else
'Call CreateDataBase(NowPath & "base.ocx") '自动生成ACCESS数据库
'End If
' form6.WriteLog ("删除自定义查询")'写入系统日志
Tconnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\base.ocx;Persist Security Info=False;Jet OLEDB:Database Password=huang987654"
TRss.Open "select * from 系统日志 ", Tconnn, 3, 3, -1
If TRss.RecordCount > 0 Then
TRss.MoveLast
End If
TRss.AddNew 'id
TRss.Fields("用户名").value = text1.text
TRss.Fields("操作时间").value = Format(Now, "yyyy.mm.dd ") & Time
TRss.Fields("操作内容").value = text2.text TRss.Update
TRss.Close: Tconnn.Close
Set TRss = Nothing: Set Tconnn = Nothing
你把上面的改成和你数据库一样就可以保存了
Dim Tconnn As New ADODB.Connection
Dim TRss As New ADODB.Recordset
If Tconnn.State = 1 Then Tconnn.Close
If TRss.State = 1 Then TRss.Close
'If Dir(App.Path & "\data\base.ocx") <> "" Then '判断文件是否存在
'MsgBox "在", , ""
'Else
'Call CreateDataBase(NowPath & "base.ocx") '自动生成ACCESS数据库
'End If
' form6.WriteLog ("删除自定义查询")'写入系统日志
Tconnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\base.ocx;Persist Security Info=False;Jet OLEDB:Database Password=huang987654"
TRss.Open "select * from 系统日志 ", Tconnn, 3, 3, -1
If TRss.RecordCount > 0 Then
TRss.MoveLast
End If
TRss.AddNew 'id
TRss.Fields("用户名").value = text1.text
TRss.Fields("操作时间").value = Format(Now, "yyyy.mm.dd ") & Time
TRss.Fields("操作内容").value = text2.text TRss.Update
TRss.Close: Tconnn.Close
Set TRss = Nothing: Set Tconnn = Nothing
你把上面的改成和你数据库一样就可以保存了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询