I'm running a program that validates the input of the text field via Access database. 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. 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.
Please see Microsoft's documentation here:
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.