[英]VB6 Search Recordset
I'm running a program that validates the input of the text field via Access database. 我正在运行一个程序,该程序通过Access数据库验证文本字段的输入。 Here's a sample code:
这是一个示例代码:
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
I always get the error that the "PrimaryKey" is not an index. 我总是收到“ PrimaryKey”不是索引的错误。 I need help.
我需要帮助。
Almost forgot. 差点忘了。 Here's the code when the form loads:
这是加载表单时的代码:
Private Sub Form_Load()
CenterForm
Me.Top = 0
Set database = OpenDatabase("p:\location\file.mdb")
Set recordset = database.OpenRecordset("table")
End Sub
Inside the database you have to define the field PrimaryKey as an Index. 在数据库内部,您必须将字段PrimaryKey定义为索引。
Please see Microsoft's documentation here: 请在此处查看Microsoft的文档:
https://support.office.com/en-us/article/Create-or-remove-a-primary-key-07b4a84b-0063-4d56-8b00-65f2975e4379 https://support.office.com/zh-CN/article/Create-or-remove-a-primary-key-07b4a84b-0063-4d56-8b00-65f2975e4379
As for the seek/index checking... try this: 至于查找/索引检查...请尝试以下操作:
Try this: 尝试这个:
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
this will tell you whether seek or index is not allowed using the provider you have. 这将告诉您使用您提供的提供商是否允许搜索或索引。 Switching the OLE provider may be your answer.
切换OLE提供程序可能是您的答案。
can the seek method be done without the index?
没有索引就可以完成搜索方法吗? if so, how?
如果是这样,怎么办?
No, seek needs the index. 不,寻求需要索引。 But you can iterate through all records;
但是您可以遍历所有记录。 depending on the number of records this may be a serious performance issue.
根据记录的数量,这可能是一个严重的性能问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.