Ms access 重新查询子表单有时只起作用
我正在尝试重新查询显示客户详细信息的子表单 我确信我的sql在不受主窗体影响的情况下可以作为子窗体正常工作Ms access 重新查询子表单有时只起作用,ms-access,vba,Ms Access,Vba,我正在尝试重新查询显示客户详细信息的子表单 我确信我的sql在不受主窗体影响的情况下可以作为子窗体正常工作 Private Sub btnSearch_Click() Dim sql As String sql = "SELECT tblCustomer.[Job ID], tblCustomer.[Customer Name], tblCustomer.[Street Name], tblCustomer.Area, tblAppointments.[Appointment
Private Sub btnSearch_Click()
Dim sql As String
sql = "SELECT tblCustomer.[Job ID], tblCustomer.[Customer Name], tblCustomer.[Street Name], tblCustomer.Area, tblAppointments.[Appointment Date] " _
& "FROM tblCustomer " _
& "LEFT JOIN tblAppointments ON tblCustomer.[Job ID] = tblAppointments.[Job Number].Value " _
& "WHERE [Customer Name] LIKE '*" & Me.txtKeywords & "*'" _
& "OR [Job ID] LIKE '*" & Me.txtKeywords & "*'" _
& "OR [Street Name] LIKE '*" & Me.txtKeywords & "*'" _
& "OR [Area] LIKE '*" & Me.txtKeywords & "*'" _
& "OR [Appointment Date] LIKE '*" & Me.txtKeywords & "*'" _
& "ORDER BY tblAppointments.[Appointment Date];"
Me.subCustomerList.Form.RecordSource = sql
Me.subCustomerList.Form.Requery
End Sub
调试器在以下行显示错误:
Me.subCustomerList.Form.RecordSource = sql
我还收到了错误消息
对象或类不支持事件集
我创建了一个新的子窗体,并复制了一个不起作用的子窗体,但似乎运行良好。我创建了一个新的子窗体,并复制了一个不起作用的子窗体,但似乎运行良好。在您的情况下,考虑使用存储查询进行参数化,原因如下:
连接上使用索引)
参数txtKeywordsParam文本(255);
选择c.[职务ID]、c.[客户姓名]、c.[街道名称]、c.区域、a.[预约日期]
来自TBL客户c
c上的左连接TBLAPPoints a。[作业ID]=a[作业编号]
其中c.[客户名称]如txtKeywordsParam
或者像txtKeywordsParam这样的c[作业ID]
或者像txtKeywordsParam这样的c[街道名称]
或者像txtKeywordsParam这样的c[区域]
或者像txtKeywordsParam这样的[约会日期]
通过[预约日期]订购;
VBA
Private Sub btnSearch_Click()
Dim qdef As QueryDef
Dim rst As Recordset
' OPEN QUERY AND BIND PARAM
Set qdef = CurrentDb.QueryDefs("mySavedParamQuery")
qdef!txtKeywordsParam = "*" & Me.txtKeyword & "*"
' ASSIGN QUERY RESULT TO RECORDSET
Set rst = qdef.OpenRecordset()
' APPLY RECORDSET TO FORM
Set Me.subCustomerList.Form.Recordset = rst
Set qdef = Nothing
End Sub
在您的案例中,考虑使用存储查询进行参数化有以下几个原因:
连接上使用索引)
参数txtKeywordsParam文本(255);
选择c.[职务ID]、c.[客户姓名]、c.[街道名称]、c.区域、a.[预约日期]
来自TBL客户c
c上的左连接TBLAPPoints a。[作业ID]=a[作业编号]
其中c.[客户名称]如txtKeywordsParam
或者像txtKeywordsParam这样的c[作业ID]
或者像txtKeywordsParam这样的c[街道名称]
或者像txtKeywordsParam这样的c[区域]
或者像txtKeywordsParam这样的[约会日期]
通过[预约日期]订购;
VBA
Private Sub btnSearch_Click()
Dim qdef As QueryDef
Dim rst As Recordset
' OPEN QUERY AND BIND PARAM
Set qdef = CurrentDb.QueryDefs("mySavedParamQuery")
qdef!txtKeywordsParam = "*" & Me.txtKeyword & "*"
' ASSIGN QUERY RESULT TO RECORDSET
Set rst = qdef.OpenRecordset()
' APPLY RECORDSET TO FORM
Set Me.subCustomerList.Form.Recordset = rst
Set qdef = Nothing
End Sub
您的
OR
和ORDER
子句之间需要空格使用Debug.Print sql
在即时窗口中显示已完成的查询,然后将其粘贴到Access中,以查看是否也有错误。您的原始记录源可能正常,但在您运行尝试更改它时没有。我打印了sql并获得以下信息:SELECT TBLCUSTER.[Job ID]、TBLCUSTER.[Customer Name]、TBLCUSTER.[Street Name]、TBLCUSTER.Area、TBLAPPOINTS.[约会日期]从tblCustomer左侧加入tblCustomer上的TBLAPPoints。[作业ID]=TBLAPPoints。[作业编号]。值,其中[Customer Name]类似于's'或[Job ID]类似于's'或[Street Name]类似于's'或[Area]类似于's'或[Appointment Date]类似于's',按TBLAPPoints排序。[约会日期];为什么打印的查询没有星号?在或之前的空格在哪里?请确保在此发布准确的查询。您的或和订单
子句之间需要空格。请使用Debug.Print sql
在即时窗口中显示已完成的查询,然后将其粘贴到Access中,以查看是否也有错误。您的原始记录源可能正常,但在您运行尝试更改它时没有。我打印了sql并获得以下信息:SELECT TBLCUSTER.[Job ID]、TBLCUSTER.[Customer Name]、TBLCUSTER.[Street Name]、TBLCUSTER.Area、TBLAPPOINTS.[约会日期]从tblCustomer左侧加入tblCustomer上的TBLAPPoints。[作业ID]=TBLAPPoints。[作业编号]。值,其中[Customer Name]类似于's'或[Job ID]类似于's'或[Street Name]类似于's'或[Area]类似于's'或[Appointment Date]类似于's',按TBLAPPoints排序。[约会日期];为什么打印的查询没有星号?在或之前的空格在哪里?请确保准确的查询被张贴在这里。谢谢,我会调查的。我还是个新手,所以学习这个将是我的目标。我现在已经修复了它,并且对于修改它有点谨慎,特别是对于我可能会猜测的代码。谢谢你的帮助,尽管我将学习参数化,并在将来使用它。谢谢你,我将研究它。我还是个新手,所以学习这个将是我的目标。我现在已经修复了它,并且对于修改它有点谨慎,特别是对于我可能会猜测的代码。感谢您的帮助,尽管我将学习参数化并在将来使用它。