vb连接数据库时出现错误:实时错误 '3704' 对象关闭时,不允许操作。
下面是vb的代码:OptionExplicitPublicFunctiongetrs(ByValstrqueryAsString)AsADODB.RecordsetDim...
下面是vb的代码:
Option Explicit
Public Function getrs(ByVal strquery As String) As ADODB.Recordset
Dim rs As New ADODB.Recordset
Dim conn As New ADODB.Connection
On Error GoTo getrs_error
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=F:\vb资料\考勤管理系统\考勤管理系统.mdb"
conn.Open
rs.Open Trim(strquery), conn, adOpenKeyset, adLockOptimistic
Set getrs = rs
getrs_exit:
Set rs = Nothing
Set conn = Nothing
Exit Function
getrs_error:
MsgBox (Err.Description)
Resume getrs_exit
End Function
Public Sub executesql(ByVal strcmd As String)
Dim conn As New ADODB.Connection
On Error GoTo executesql_error
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=F:\vb资料\考勤管理系统\考勤管理系统.mdb"
conn.Open
conn.Execute Trim(strcmd)
executesql_exit:
Set conn = Nothing
Exit Sub
executesql_error:
MsgBox (Err.Description)
Resume executesql_exit
End Sub
以上是通用声明代码部分。
Private Sub cmdlogin_Click()
On Error GoTo err_cmdlogin_click
Dim rs As New ADODB.Recordset
Dim str As String
Dim num As Integer
str = "select count(ID)from Person where ID='" & Me.username.Text
str = str & "'and Password= '" & Me.password.Text & "'"
Set rs = getrs(str)
num = rs(0)
If Me.username.Text = "" Then
MsgBox "请输入用户名称!", , "提示!"
ElseIf Me.password.Text = "" Then
MsgBox "请输入用户密码!", , "提示!"
ElseIf num <> 1 Then
MsgBox "用户名或密码不正确,请重新输入!", , "警告!"
Me.username.Text = ""
Me.password.Text = ""
Me.username.SetFocus
Else
Me.Visible = False
loginflag = True
MDIForm1.Show
End If
exit_cmdlogin_click:
Exit Sub
err_cmdlogin_click:
MsgBox (Err.Description)
Resume exit_cmdlogin_click
End Sub
以上是登录窗体代码部分。
MDIForm1出来之后,点击“修改时间”部分,出来“修改时间窗体”
Private Sub cmdReset_Click()
Me.txtStartAm.Text = "9:00:00"
Me.txtStartPm.Text = "12:00:00"
Me.txtEndAm.Text = "13:00:00"
Me.txtEndPm.Text = "18:00:00"
End Sub
Private Sub cmdUpdate_Click()
Dim rs As New ADODB.Recordset
Dim strsql As String
strsql = "select * from worktime"
rs.AddNew
rs("starttimeam") = Me.txtStartAm.Text
rs("starttimepm") = Me.txtStartPm.Text
rs("endtimeam") = Me.txtEndAm.Text
rs("endtimepm") = Me.txtEndPm.Text
rs.Update
rs.Close
End Sub
Private Sub Form_Load()
Dim rs As New ADODB.Recordset
Dim strsql As String
strsql = "select * from worktime"
Set rs = getrs(strsql)
Me.txtStartAm.Text = rs("starttimeam")
Me.txtStartPm.Text = rs("starttimepm")
Me.txtEndAm.Text = rs("endtimeam")
Me.txtEndPm.Text = rs("endtimepm")
rs.Close
End Sub
但是执行到“rs.AddNew”便会出现 如题目所说的错误提示。各位大侠帮我看下怎么解决啊。我用Vb连接Access数据库。谢谢了!
是不是rs在哪关闭了,还是数据库在哪关闭了,没有打开啊??代码怎么变一下? 展开
Option Explicit
Public Function getrs(ByVal strquery As String) As ADODB.Recordset
Dim rs As New ADODB.Recordset
Dim conn As New ADODB.Connection
On Error GoTo getrs_error
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=F:\vb资料\考勤管理系统\考勤管理系统.mdb"
conn.Open
rs.Open Trim(strquery), conn, adOpenKeyset, adLockOptimistic
Set getrs = rs
getrs_exit:
Set rs = Nothing
Set conn = Nothing
Exit Function
getrs_error:
MsgBox (Err.Description)
Resume getrs_exit
End Function
Public Sub executesql(ByVal strcmd As String)
Dim conn As New ADODB.Connection
On Error GoTo executesql_error
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=F:\vb资料\考勤管理系统\考勤管理系统.mdb"
conn.Open
conn.Execute Trim(strcmd)
executesql_exit:
Set conn = Nothing
Exit Sub
executesql_error:
MsgBox (Err.Description)
Resume executesql_exit
End Sub
以上是通用声明代码部分。
Private Sub cmdlogin_Click()
On Error GoTo err_cmdlogin_click
Dim rs As New ADODB.Recordset
Dim str As String
Dim num As Integer
str = "select count(ID)from Person where ID='" & Me.username.Text
str = str & "'and Password= '" & Me.password.Text & "'"
Set rs = getrs(str)
num = rs(0)
If Me.username.Text = "" Then
MsgBox "请输入用户名称!", , "提示!"
ElseIf Me.password.Text = "" Then
MsgBox "请输入用户密码!", , "提示!"
ElseIf num <> 1 Then
MsgBox "用户名或密码不正确,请重新输入!", , "警告!"
Me.username.Text = ""
Me.password.Text = ""
Me.username.SetFocus
Else
Me.Visible = False
loginflag = True
MDIForm1.Show
End If
exit_cmdlogin_click:
Exit Sub
err_cmdlogin_click:
MsgBox (Err.Description)
Resume exit_cmdlogin_click
End Sub
以上是登录窗体代码部分。
MDIForm1出来之后,点击“修改时间”部分,出来“修改时间窗体”
Private Sub cmdReset_Click()
Me.txtStartAm.Text = "9:00:00"
Me.txtStartPm.Text = "12:00:00"
Me.txtEndAm.Text = "13:00:00"
Me.txtEndPm.Text = "18:00:00"
End Sub
Private Sub cmdUpdate_Click()
Dim rs As New ADODB.Recordset
Dim strsql As String
strsql = "select * from worktime"
rs.AddNew
rs("starttimeam") = Me.txtStartAm.Text
rs("starttimepm") = Me.txtStartPm.Text
rs("endtimeam") = Me.txtEndAm.Text
rs("endtimepm") = Me.txtEndPm.Text
rs.Update
rs.Close
End Sub
Private Sub Form_Load()
Dim rs As New ADODB.Recordset
Dim strsql As String
strsql = "select * from worktime"
Set rs = getrs(strsql)
Me.txtStartAm.Text = rs("starttimeam")
Me.txtStartPm.Text = rs("starttimepm")
Me.txtEndAm.Text = rs("endtimeam")
Me.txtEndPm.Text = rs("endtimepm")
rs.Close
End Sub
但是执行到“rs.AddNew”便会出现 如题目所说的错误提示。各位大侠帮我看下怎么解决啊。我用Vb连接Access数据库。谢谢了!
是不是rs在哪关闭了,还是数据库在哪关闭了,没有打开啊??代码怎么变一下? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询