我正在制作一个连续的搜索表单,当用户在多个搜索框中键入时,它会自我更新。
代码一直工作到用户键入与任何记录都不对应的参数为止。
然后查询找不到任何东西,我得到错误
"当控件没有焦点时不能使用属性或方法";
我不知道表单的哪个元素在做这件事。将.SetFocus添加到文本框控件没有帮助。
关于如何使用的任何想法
- 再次将焦点设置为文本框并防止其丢失,或者
- 弄清楚是什么偷走了焦点并将其禁用
在文本框_Change sub.中调用以下sub.我添加了一个解决方法:
Private Sub RefreshTB(textbox As Control)
'This is to prevent Acces from removing trailing spaces
'If the textbox isn't empty and there is a space at the end, don't requery. This preserves trailing spaces as Access trims them on Me.requery
If Len(textbox.Text) <> 0 And InStr(Len(textbox.Text), textbox.Text, " ", vbTextCompare) Then
Exit Sub
End If
'If the last character isn't a space, requery on change to show new results of the query
Me.Requery
'The workaround: If the query returns no results, detect that, warn the user and clear search box. Requery to show some results again.
If DCount("*", "DatasetsFilterQ") = 0 Then
If MsgBox("No results found. The last TextBox you searched in will be cleared.", vbOKOnly, "No Records") = vbOK Then
textbox = ""
Me.Requery
End If
Exit Sub
End If
textbox.SetFocus
textbox.SelStart = Len(Nz(textbox.Text))
End Sub
我尝试了一个过滤器,但在向函数传递.text值时遇到了同样的错误。
您不能将SelStart
设置为Null,因此请使用Nz避免这种情况:
textbox.SelStart = Len(Nz(textbox.Value))