[英]VB6 Search Recordset
我正在运行一个程序,该程序通过Access数据库验证文本字段的输入。 这是一个示例代码:
Private Sub TextBox_LostFocus()
If TextBox <> "" Then
With recordset
.Index = "PrimaryKey"
.Seek "=", TextBox
If .NoMatch Then
MsgBox "Record does not exist!", vbExclamation, Me.Caption
TextBox = ""
TextBox.SetFocus
End If
End With
End If
End Sub
我总是收到“ PrimaryKey”不是索引的错误。 我需要帮助。
差点忘了。 这是加载表单时的代码:
Private Sub Form_Load()
CenterForm
Me.Top = 0
Set database = OpenDatabase("p:\location\file.mdb")
Set recordset = database.OpenRecordset("table")
End Sub
在数据库内部,您必须将字段PrimaryKey定义为索引。
请在此处查看Microsoft的文档:
至于查找/索引检查...请尝试以下操作:
尝试这个:
Private Sub TextBox_LostFocus()
If TextBox <> "" Then
If NOT recordset.Supports(adIndex) THEN MSGBOX("AdIndex not supported")
IF NOT recordset.Supports(adSeek) Then MSGBOX("adSeek not supported")
With recordset
.Index = "PrimaryKey"
.Seek "=", TextBox
If .NoMatch Then
MsgBox "Record does not exist!", vbExclamation, Me.Caption
TextBox = ""
TextBox.SetFocus
End If
End With
End If
End Sub
这将告诉您使用您提供的提供商是否允许搜索或索引。 切换OLE提供程序可能是您的答案。
没有索引就可以完成搜索方法吗? 如果是这样,怎么办?
不,寻求需要索引。 但是您可以遍历所有记录。 根据记录的数量,这可能是一个严重的性能问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.