求vb6.0对Access数据库文件中的数据进行增、删、改和查询操作的一些实例
展开全部
'先建立一个公共模块
Option Explicit
Public Function GetConnStr() As String
Dim ConnString As String
'连接Access
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库名;Persist Security Info=False"
GetConnStr = ConnString
End Function
Public Function OpenConn(ByRef Conn As ADODB.Connection) As Boolean
'打开数据库连接,连接成功返回true,出错时返回false
Set Conn = New ADODB.Connection
'出错处理
On Error GoTo Error_box
Conn.Open GetConnStr
OpenConn = True
Exit Function
Error_box:
MsgBox "连接数据库失败!请重新连接!"
OpenConn = False
Exit Function
End Function
Public Sub ExecuteSQL(ByVal SQL As String, ByRef msg As String)
'执行SQL语句
Dim Conn As ADODB.Connection
'出错处理
On Error GoTo Error_box
'打开数据库连接
If OpenConn(Conn) Then
Conn.Execute SQL
msg = "操作执行成功!"
End If
Exit Sub
Error_box:
msg = "执行错误: " & Err.Description
Set Conn = Nothing
Exit Sub
End Sub
'*********************************************************************************************************************
'添加数据
Call ExecuteSQL("INSERT INTO 表名称(字段1,字段2,字段N) VALUES ('值1','值2','值N')", msg)
'注意:以上字段值如果为表达式并且是文本类型应采用如此格式:'" & 表达式 &"',如果是数值应为" & 表达式 &"
'删除数据
Call ExecuteSQL("DELETE FROM 表名称", msg)
'修改数据
Call ExecuteSQL("Update 表名称 SET 要修改的字段名='" & Trim(Text1.Text) & "' WHERE 条件='" & 表达式 & "'", msg)
'如:
Call ExecuteSQL("Update G借债 SET 姓名='" & Trim(Text1(0).Text) & "' WHERE 姓名='" & Lbl_str.Caption & "'", msg)
Option Explicit
Public Function GetConnStr() As String
Dim ConnString As String
'连接Access
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库名;Persist Security Info=False"
GetConnStr = ConnString
End Function
Public Function OpenConn(ByRef Conn As ADODB.Connection) As Boolean
'打开数据库连接,连接成功返回true,出错时返回false
Set Conn = New ADODB.Connection
'出错处理
On Error GoTo Error_box
Conn.Open GetConnStr
OpenConn = True
Exit Function
Error_box:
MsgBox "连接数据库失败!请重新连接!"
OpenConn = False
Exit Function
End Function
Public Sub ExecuteSQL(ByVal SQL As String, ByRef msg As String)
'执行SQL语句
Dim Conn As ADODB.Connection
'出错处理
On Error GoTo Error_box
'打开数据库连接
If OpenConn(Conn) Then
Conn.Execute SQL
msg = "操作执行成功!"
End If
Exit Sub
Error_box:
msg = "执行错误: " & Err.Description
Set Conn = Nothing
Exit Sub
End Sub
'*********************************************************************************************************************
'添加数据
Call ExecuteSQL("INSERT INTO 表名称(字段1,字段2,字段N) VALUES ('值1','值2','值N')", msg)
'注意:以上字段值如果为表达式并且是文本类型应采用如此格式:'" & 表达式 &"',如果是数值应为" & 表达式 &"
'删除数据
Call ExecuteSQL("DELETE FROM 表名称", msg)
'修改数据
Call ExecuteSQL("Update 表名称 SET 要修改的字段名='" & Trim(Text1.Text) & "' WHERE 条件='" & 表达式 & "'", msg)
'如:
Call ExecuteSQL("Update G借债 SET 姓名='" & Trim(Text1(0).Text) & "' WHERE 姓名='" & Lbl_str.Caption & "'", msg)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询