| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 543 人关注过本帖
标题:vb查询记录时出了问题
只看楼主 加入收藏
rich_ying
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2006-12-22
收藏
 问题点数:0 回复次数:1 
vb查询记录时出了问题

图片附件: 游客没有浏览图片的权限,请 登录注册


--------------下面是我的程序代码-----------------------------

Dim tj1 As Boolean
Dim tj2 As Boolean
Dim sqlstr As String

Private Sub Command1_Click()

If tj1 = False And tj2 = False Then
MsgBox "请选择查询条件!", vbExclamation, "警告"
Option1.SetFocus
Exit Sub
End If

If Trim(Text1.Text) = "" Then
MsgBox "请输入查询条件!", vbExclamation, "警告"
Text1.SetFocus
Exit Sub
End If

If tj1 = True Then
sqlstr = "select * from jydm where jydm= '" & Trim(Text1.Text) & "' "
Call viewdata
End If

If tj2 = True Then
sqlstr = "select * from jydm where jypy= '" & Trim(Text1.Text) & "' "
MsgBox sqlstr
Call viewdata
End If

End Sub

Private Sub Form_Load()

tj1 = False
tj2 = False

End Sub

Private Sub Option1_Click()

If Option1.Value = True Then
tj1 = True
tj2 = False
Else
tj1 = False
tj2 = True
End If

End Sub

Private Sub viewdata()
Dim msgtext As String
Dim ret As ADODB.Recordset

Set ret = New ADODB.Recordset

Set ret = executeSQL(sqlstr, msgtext)
With MSFlexGrid1
.TextMatrix(0, 0) = "序号"
.TextMatrix(0, 1) = "交易代码"
.TextMatrix(0, 2) = "交易名称"
.TextMatrix(0, 3) = "交易拼音"

Do While Not ret.EOF
.TextMatrix(.Rows - 1, 0) = .Rows - 1
.TextMatrix(.Rows - 1, 1) = ret.Fields(0)
.TextMatrix(.Rows - 1, 2) = ret.Fields(1)
.TextMatrix(.Rows - 1, 3) = ret.Fields(2)
.Rows = .Rows + 1
ret.MoveNext
Loop
End With
ret.Close

End Sub

Private Sub Option2_Click()

If Option2.Value = True Then
tj2 = True
tj1 = False
Else
tj2 = False
tj1 = True
End If

End Sub
----------------------------------下面是公用模块代码------------------------------------------


Public Function executeSQL(ByVal sqlstr As String, msgstr As String) As ADODB.Recordset

Dim conn As ADODB.Connection
Dim ret As ADODB.Recordset
Dim sTokens() As String

Set conn = New ADODB.Connection

On Error GoTo executeSQL_Error

sTokens = Split(sqlstr)


conn.ConnectionString = "dsn=cxjydm;uid=;pwd="
conn.Open

If InStr("insert,delete,update", LCase(sTokens(0))) Then
conn.executeSQL

msgstr = sTokens(0) & "query successful"
Else
Set ret = New ADODB.Recordset
ret.Open Trim$(sqlstr), conn, adOpenKeyset, adLockOptimistic

Set executeSQL = ret
End If

executeSQL_Exit:
Set ret = Nothing
Set conn = Nothing
Exit Function


executeSQL_Error:
msgstr = "查询错误:" & Err.Description
Resume executeSQL_Exit

End Function

----------------------------------------------------------------------

问题:
1:如果我选择了"按交易代码查询",输入0100,结果查询到了一条记录,
如果我再按按钮,就又会增加一条同样的记录,为什么呀?什么原因导致这样呢?

2:如果我选择了"按拼音查询",输入abc,如果没有查询到记录,可结果记录内容还是[第一个]问题的记录,
按常理说不应该是第一个问题的记录的呀?应该是显示字段名,而不显示记录内容才对的呀?
请大家帮我一下

[此贴子已经被作者于2007-1-6 9:12:39编辑过]

搜索更多相关主题的帖子: 记录 查询 
2007-01-06 09:11
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
Do While Not ret.EOF
.TextMatrix(.Rows - 1, 0) = .Rows - 1
.TextMatrix(.Rows - 1, 1) = ret.Fields(0)
.TextMatrix(.Rows - 1, 2) = ret.Fields(1)
.TextMatrix(.Rows - 1, 3) = ret.Fields(2)
.Rows = .Rows + 1
ret.MoveNext
Loop

不需要这样去添加数据到MSHFlexGrid...只要set Mshflexgrid.DataSource=ret就行..
如果一定要这样的话..那添加之前..就MSHFlexGrid1.Clear
2.还是应该在查询之前就MSHFlexGrid1.Clear

我的msn: myfend@
2007-01-06 09:50
快速回复:vb查询记录时出了问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039892 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved