读书人

VB小疑点~高手们请留步~

发布时间: 2012-03-11 18:15:39 作者: rapoo

VB小问题~~~~高手们请留步~~~
command4是一个查找功能~~~

Dim Rs As ADODB.Recordset
Dim Cn As ADODB.Connection '这两行是通用里的


Private Sub Form_Load()
Set Rs = New ADODB.Recordset
Set Cn = New ADODB.Connection
Dim cnstr As String
Dim Sql As String
cnstr = "provider=sqloledb;User ID = sa;password = 880805;initial catalog = 小区物业管理;Data Source = WAXSNYBB\WAXSN"
Cn.Open cnstr
Sql = "select * from 业主信息"
Rs.CursorLocation = adUseClient
Rs.Open Sql, Cn, adOpenDynamic, adLockOptimistic
Text1.Text = Rs(0)
Text2.Text = Rs(1)
Text3.Text = Rs(2)
Text4.Text = Rs(3)
Text5.Text = Rs(4)
Text6.Text = Rs(5)
Text7.Text = Rs(6)
End Sub


Private Sub Command4_Click()
Dim cz As String
cz = InputBox$("请输入业主编号", "查找")
Set Rs = Cn.Execute("select count (*) from 业主信息 where 业主编号 like'" & cz & "'")
Text1.Text = Rs(0)
Text2.Text = Rs(1)
Text3.Text = Rs(2)
Text4.Text = Rs(3)
Text5.Text = Rs(4)
Text6.Text = Rs(5)
Text7.Text = Rs(6)
End Sub

数据库链接神什么的都正常
现在的问题时 当我运行后,点击command4按钮,输入了 业主编号 之后

弹出个框框
实时错误3265
在对用所需名称或序数的集合中未找到项目

点调试后,黄色框框定位在text2.text = RS(1)这一行上
对了,我RS(0),也就是表中的第一列是 业主编号

我删掉text2.text = RS(1)后
黄色框框就定位在text3.text = RS(2)

这是什么原因呢,应该怎样修改代码呢,请高手们指点一下,不胜感激!

[解决办法]

VB code
Private Sub Command4_Click()Dim cz As Stringcz = InputBox$("请输入业主编号", "查找")Set Rs = Cn.Execute("select * from 业主信息 where 业主编号 like'" & cz & "'")if Rs.EOF or Rs.BOF then Exit subText1.Text = Rs(0)Text2.Text = Rs(1)Text3.Text = Rs(2)Text4.Text = Rs(3)Text5.Text = Rs(4)Text6.Text = Rs(5)Text7.Text = Rs(6)End Sub
[解决办法]
Dim Rs As ADODB.Recordset
Dim Cn As ADODB.Connection '这两行是通用里的


Private Sub Form_Load()
Set Rs = New ADODB.Recordset
Set Cn = New ADODB.Connection
Dim cnstr As String
Dim Sql As String
cnstr = "provider=sqloledb;User ID = sa;password = 880805;initial catalog = 小区物业管理;Data Source = WAXSNYBB\WAXSN"
Cn.Open cnstr
Sql = "select * from 业主信息"
Rs.CursorLocation = adUseClient
Rs.Open Sql, Cn, adOpenDynamic, adLockOptimistic
Text1.Text = Rs(0)
Text2.Text = Rs(1)
Text3.Text = Rs(2)
Text4.Text = Rs(3)
Text5.Text = Rs(4)
Text6.Text = Rs(5)
Text7.Text = Rs(6)
End Sub


Private Sub Command4_Click()
Dim cz As String
cz = InputBox$("请输入业主编号", "查找")
Set Rs = Cn.Execute("select count (*) from 业主信息 where 业主编号 like'" & cz & "'")’该语句执行后,覆盖Load事件中加载的业主信息数据,该命令只返回唯一的一列(条目数量)
Text1.Text = Rs(0)
'rs(0)之后的列都将无效
Text2.Text = Rs(1)
Text3.Text = Rs(2)
Text4.Text = Rs(3)
Text5.Text = Rs(4)
Text6.Text = Rs(5)
Text7.Text = Rs(6)
End Sub


数据库链接神什么的都正常
现在的问题时 当我运行后,点击command4按钮,输入了 业主编号 之后

弹出个框框
实时错误3265
在对用所需名称或序数的集合中未找到项目

点调试后,黄色框框定位在text2.text = RS(1)这一行上
对了,我RS(0),也就是表中的第一列是 业主编号

我删掉text2.text = RS(1)后
黄色框框就定位在text3.text = RS(2)

这是什么原因呢,应该怎样修改代码呢,请高手们指点一下,不胜感激!


上述蓝色语句改为:

Set Rs = Cn.Execute("select * from 业主信息 where 业主编号 like '%" & cz & "%'")

读书人网 >VB

热点推荐