【问题标题】:receiving error 3075: syntax error for combo box linked to text box接收错误 3075:链接到文本框的组合框的语法错误
【发布时间】:2018-04-20 06:13:47
【问题描述】:

每当我尝试运行我的表单时,我都会收到此错误,

还有错误:

这样做的想法是选择一个字段,输入文本并让它根据文本输入拉记录,如果没有匹配,它会说“没有找到记录”。仍然是 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

【问题讨论】:

  • 每当我运行调试时,它都会突出显示 docmd.applyfilter、sfitler。这里可能需要额外的参数吗?
  • 尝试在"LIKE '"前加一个空格
  • ^^ “做...或不做。没有尝试”-在这种情况下...“做”。缺少的空格将导致该消息 - 仔细查看该消息并尝试在三个标记 FirstNameLIKEjoe* 中找到运算符。
  • 我之前添加了空格,这修复了错误,但是,现在当我选择一个字段并输入名称,然后单击文本框外部时,它返回我的“未找到记录消息”当我输入 joe 作为名字时。过滤器语句中的通配符位置是否尴尬?这是针对 vba 输入数据的捕获:sfilter = "FirstName LIKE 'joe *'"。
  • *前面可能不应该有空格。

标签: vba ms-access


【解决方案1】:

...避免在 cmets 中回答问题。

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

Debug.Print sfilter
FirstNameLIKE 'joe *'

注意缺失和多余的空间,制作它

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

【讨论】:

  • 问题在 cmets 中被回答是因为:1. 回答者不确定他们是否有正确的答案,2. 回答者不想因为发布某人不喜欢的答案而被否决。我一直这样做,
【解决方案2】:

它现在可以工作了,感谢大家的帮助,我已经发布了源代码和编辑。

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
 Debug.Print sfilter


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

 End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    相关资源
    最近更新 更多