vb中连接无法用于执行此操作。在此上下文中它可能已被关闭或无效
代码如下OptionExplicitPrivateSubCommand1_Click()Dimrs_bookkindAsNewADODB.RecordsetDimsqlA...
代码如下
Option Explicit
Private Sub Command1_Click()
Dim rs_bookkind As New ADODB.Recordset
Dim sql As String
If Trim(Text1.Text) = "" Then
MsgBox "图书类别名称不能为空!", 16, "类别名称错误"
Text1.SetFocus
Exit Sub
End If
If Trim(Text2.Text) = "" Then
MsgBox "图书类别编号不能为空!", 16, "类别编号错误"
Text2.SetFocus
Exit Sub
End If
sql = "select * from 图书类别 where 类别名称="
rs_bookkind.Open sql, conn, adOpenKeyset, adLockOptimistic
If rs_bookkind.EOF Then
rs_bookkind.AddNew
rs_bookkind.Fields(0) = Trim(Text1.Text)
rs_bookkind.Fields(1) = Trim(Text2.Text)
rs_bookkind.Update
MsgBox "添加图书类别成功!", 16, "图书类别错误"
rs_bookkind.Close
Else
MsgBox "图书类别重复!", 16, "图书类别错误"
Text1.SetFocus
Text1.Text = ""
rs_bookkind.Close
Exit Sub
End If
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
出错语句是
rs_bookkind.Open sql, conn, adOpenKeyset, adLockOptimistic 展开
Option Explicit
Private Sub Command1_Click()
Dim rs_bookkind As New ADODB.Recordset
Dim sql As String
If Trim(Text1.Text) = "" Then
MsgBox "图书类别名称不能为空!", 16, "类别名称错误"
Text1.SetFocus
Exit Sub
End If
If Trim(Text2.Text) = "" Then
MsgBox "图书类别编号不能为空!", 16, "类别编号错误"
Text2.SetFocus
Exit Sub
End If
sql = "select * from 图书类别 where 类别名称="
rs_bookkind.Open sql, conn, adOpenKeyset, adLockOptimistic
If rs_bookkind.EOF Then
rs_bookkind.AddNew
rs_bookkind.Fields(0) = Trim(Text1.Text)
rs_bookkind.Fields(1) = Trim(Text2.Text)
rs_bookkind.Update
MsgBox "添加图书类别成功!", 16, "图书类别错误"
rs_bookkind.Close
Else
MsgBox "图书类别重复!", 16, "图书类别错误"
Text1.SetFocus
Text1.Text = ""
rs_bookkind.Close
Exit Sub
End If
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
出错语句是
rs_bookkind.Open sql, conn, adOpenKeyset, adLockOptimistic 展开
1个回答
展开全部
sql = "select * from 图书类别 where 类别名称='" & Text1.Text & "'" ' 这少添加文本框
rs_bookkind.Open sql, conn, adOpenKeyset, adLockOptimistic 'conn对象在这里没见,要确保打开了
rs_bookkind.Open sql, conn, adOpenKeyset, adLockOptimistic 'conn对象在这里没见,要确保打开了
更多追问追答
追问
那应该怎么改?
追答
Const cntString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=StockLibaray;"
dim conn As new Connection
conn.Open cntString
'这部分打开连接对象,不过是连接SqlServer数据库,根据数据库类型连接串不同的,你可以通过ado控件与数据库建立连接,然后把连接串拷贝过来
sql = "select * from 图书类别 where 类别名称='" & Text1.Text & "'"
rs_bookkind.Open sql, conn, adOpenKeyset, adLockOptimistic
'conn处的参数可以是连接对像也可以是连接串。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询