gpt4 book ai didi

vba - 接收错误 3075 : syntax error for combo box linked to text box

转载 作者:行者123 更新时间:2023-12-04 18:28:40 25 4
gpt4 key购买 nike

每当我尝试运行我的表单时,我总是收到这个错误,

screenshot of form

错误:

error

这个想法是选择一个字段,输入文本并让它根据文本输入提取记录,如果没有匹配,它会说“找不到记录”。仍然是 VBA 新手,我们将不胜感激。代码如下。 cboField 是组合框,txtBox 是文本框。我正在运行 Access 2010 以供引用。

Option Compare Database

Private Sub cboField_Enter()

Dim oRS As DAO.Recordset, i As Integer
If Me.Form.FilterOn = True Then DoCmd.ShowAllRecords

Set oRS = Me.RecordsetClone

cboField.RowSourceType = "Value List"
cboField.RowSource = ""

For i = 0 To oRS.Fields.Count - 1
If oRS.Fields(i).Type = dbText Then cboField.AddItem oRS.Fields(i).Name
Next i

End Sub

Private Sub txtBox_Exit(Cancel As Integer)

Dim sfilter As String, oRS As DAO.Recordset
If IsNull(cboField) Then
DoCmd.ShowAllRecords
MsgBox "select a field"
Exit Sub
End If

If IsNull(txtBox) Then DoCmd.ShowAllRecords: Exit Sub

sfilter = cboField & "LIKE '" & txtBox & " *'"
DoCmd.ApplyFilter , sfilter

Set oRS = Me.RecordsetClone

If oRS.RecordCount = 0 Then
MsgBox " no record matches"
DoCmd.ShowAllRecords
End If

End Sub

最佳答案

...避免在评论中回答问题。

无论如何。在 VBA 中构建 SQL 字符串时,Debug.Print 和仔细阅读输出是您的好 helper 。 How to debug dynamic SQL in VBA

Debug.Print sfilter
FirstNameLIKE 'joe *'

注意遗漏和多余的空格,把它做好

sfilter = cboField & " LIKE '" & txtBox & "*'"

关于vba - 接收错误 3075 : syntax error for combo box linked to text box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47164788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com