Forms 使用表单通过查询显示表字段

Forms 使用表单通过查询显示表字段,forms,ms-access,Forms,Ms Access,只是一些背景资料。我的表格(HireHistory)中有50列(水平)。我有一个表单(HireHistoryForm),它有两个文本框(HistoryMovieID和HistoryCustomerID)和一个按钮(按钮运行“HireHistoryQuery”查询) 以下是我的数据摘录(客户ID位于顶部): 所以我需要的是,如果在HistoryCustomerID框中输入了一个数字,那么它就会显示该列。e、 g.如果输入的值为“1”,则在我的查询中,它将显示第1列中的所有记录 如果在Histor

只是一些背景资料。我的表格(HireHistory)中有50列(水平)。我有一个表单(HireHistoryForm),它有两个文本框(HistoryMovieID和HistoryCustomerID)和一个按钮(按钮运行“HireHistoryQuery”查询)

以下是我的数据摘录(客户ID位于顶部):

所以我需要的是,如果在HistoryCustomerID框中输入了一个数字,那么它就会显示该列。e、 g.如果输入的值为“1”,则在我的查询中,它将显示第1列中的所有记录

如果在HistoryMovieID框(例如0001)中输入了一个数字,则它会显示特定客户ID的该MovieID的所有实例。i、 e.第1列是ID,因此对于ID=1,它将显示“0001 on 19/05/2006”,然后将继续查找“0001”的下一个实例等

对于HistoryCustomerID,我尝试将其放入查询的“字段”:

=[Forms]![HireHistoryForm]![HistoryCustomerID]
但它不起作用。我的查询只返回了一个标记为“10”的列,而这些行只是由“10”组成的

如果你能帮忙,我将不胜感激

无意冒犯(或无论如何尽量少冒犯),但这是一种构建数据结构的方法。你真的需要像这样重组它:

CustomerID  MovieID HireDate
----------  ------- --------
1           0001    19/05/2006
1           0003    20/10/2003  
1           0007    13/08/2003
...     
2           0035    16/08/2012
2           0057    06/10/2012
...
Function RestructureHistory()
Dim cdb As DAO.Database, rstIn As DAO.Recordset, rstOut As DAO.Recordset
Dim fld As DAO.Field, a() As String

Set cdb = CurrentDb
Set rstIn = cdb.OpenRecordset("HireHistory", dbOpenTable)
Set rstOut = cdb.OpenRecordset("HireHistoryV2", dbOpenTable)

Do While Not rstIn.EOF
    For Each fld In rstIn.Fields
        If fld.Name Like "Hire*" Then
            If Not IsNull(fld.Value) Then
                a = Split(fld.Value, " on ", -1, vbBinaryCompare)
                rstOut.AddNew
                rstOut!CustomerID = rstIn!CustomerID
                rstOut!MovieID = a(0)
                rstOut!HireDate = CDate(a(1))
                rstOut.Update
            End If
        End If
    Next
    Set fld = Nothing
    rstIn.MoveNext
Loop
rstOut.Close
Set rstOut = Nothing
rstIn.Close
Set rstIn = Nothing
Set cdb = Nothing
MsgBox "Done!"
End Function
如果您保持当前的数据结构,那么

  • 你会发疯的,而且

  • 其他任何人都不太可能接近这个问题

  • 编辑 修改后的数据结构是一个非常微小的改进,但它仍然对您不利。考虑到你的另一个问题,当你做一个查询时,你本质上要求一种方法来“修复”你的数据结构。

    好消息是,您可以一次运行一点VBA代码,将数据结构转换为可行的格式。首先创建新表,我称之为“HireHistoryV2”

    将数据复制到新表的VBA代码如下所示:

    CustomerID  MovieID HireDate
    ----------  ------- --------
    1           0001    19/05/2006
    1           0003    20/10/2003  
    1           0007    13/08/2003
    ...     
    2           0035    16/08/2012
    2           0057    06/10/2012
    ...
    
    Function RestructureHistory()
    Dim cdb As DAO.Database, rstIn As DAO.Recordset, rstOut As DAO.Recordset
    Dim fld As DAO.Field, a() As String
    
    Set cdb = CurrentDb
    Set rstIn = cdb.OpenRecordset("HireHistory", dbOpenTable)
    Set rstOut = cdb.OpenRecordset("HireHistoryV2", dbOpenTable)
    
    Do While Not rstIn.EOF
        For Each fld In rstIn.Fields
            If fld.Name Like "Hire*" Then
                If Not IsNull(fld.Value) Then
                    a = Split(fld.Value, " on ", -1, vbBinaryCompare)
                    rstOut.AddNew
                    rstOut!CustomerID = rstIn!CustomerID
                    rstOut!MovieID = a(0)
                    rstOut!HireDate = CDate(a(1))
                    rstOut.Update
                End If
            End If
        Next
        Set fld = Nothing
        rstIn.MoveNext
    Loop
    rstOut.Close
    Set rstOut = Nothing
    rstIn.Close
    Set rstIn = Nothing
    Set cdb = Nothing
    MsgBox "Done!"
    End Function
    
    注意:您似乎正在使用dd/mm/yyyy日期格式,因此请仔细检查日期转换,以确保它们转换正确。

    无意冒犯(或者尽量少冒犯),但这是一种非常糟糕的数据结构方式。你真的需要像这样重组它:

    CustomerID  MovieID HireDate
    ----------  ------- --------
    1           0001    19/05/2006
    1           0003    20/10/2003  
    1           0007    13/08/2003
    ...     
    2           0035    16/08/2012
    2           0057    06/10/2012
    ...
    
    Function RestructureHistory()
    Dim cdb As DAO.Database, rstIn As DAO.Recordset, rstOut As DAO.Recordset
    Dim fld As DAO.Field, a() As String
    
    Set cdb = CurrentDb
    Set rstIn = cdb.OpenRecordset("HireHistory", dbOpenTable)
    Set rstOut = cdb.OpenRecordset("HireHistoryV2", dbOpenTable)
    
    Do While Not rstIn.EOF
        For Each fld In rstIn.Fields
            If fld.Name Like "Hire*" Then
                If Not IsNull(fld.Value) Then
                    a = Split(fld.Value, " on ", -1, vbBinaryCompare)
                    rstOut.AddNew
                    rstOut!CustomerID = rstIn!CustomerID
                    rstOut!MovieID = a(0)
                    rstOut!HireDate = CDate(a(1))
                    rstOut.Update
                End If
            End If
        Next
        Set fld = Nothing
        rstIn.MoveNext
    Loop
    rstOut.Close
    Set rstOut = Nothing
    rstIn.Close
    Set rstIn = Nothing
    Set cdb = Nothing
    MsgBox "Done!"
    End Function
    
    如果您保持当前的数据结构,那么

  • 你会发疯的,而且

  • 其他任何人都不太可能接近这个问题

  • 编辑 修改后的数据结构是一个非常微小的改进,但它仍然对您不利。考虑到你的另一个问题,当你做一个查询时,你本质上要求一种方法来“修复”你的数据结构。

    好消息是,您可以一次运行一点VBA代码,将数据结构转换为可行的格式。首先创建新表,我称之为“HireHistoryV2”

    将数据复制到新表的VBA代码如下所示:

    CustomerID  MovieID HireDate
    ----------  ------- --------
    1           0001    19/05/2006
    1           0003    20/10/2003  
    1           0007    13/08/2003
    ...     
    2           0035    16/08/2012
    2           0057    06/10/2012
    ...
    
    Function RestructureHistory()
    Dim cdb As DAO.Database, rstIn As DAO.Recordset, rstOut As DAO.Recordset
    Dim fld As DAO.Field, a() As String
    
    Set cdb = CurrentDb
    Set rstIn = cdb.OpenRecordset("HireHistory", dbOpenTable)
    Set rstOut = cdb.OpenRecordset("HireHistoryV2", dbOpenTable)
    
    Do While Not rstIn.EOF
        For Each fld In rstIn.Fields
            If fld.Name Like "Hire*" Then
                If Not IsNull(fld.Value) Then
                    a = Split(fld.Value, " on ", -1, vbBinaryCompare)
                    rstOut.AddNew
                    rstOut!CustomerID = rstIn!CustomerID
                    rstOut!MovieID = a(0)
                    rstOut!HireDate = CDate(a(1))
                    rstOut.Update
                End If
            End If
        Next
        Set fld = Nothing
        rstIn.MoveNext
    Loop
    rstOut.Close
    Set rstOut = Nothing
    rstIn.Close
    Set rstIn = Nothing
    Set cdb = Nothing
    MsgBox "Done!"
    End Function
    

    注意:您似乎正在使用dd/mm/yyyy日期格式,因此请仔细检查日期转换以确保它们正确转换。

    您好,谢谢您的提示。我编辑了我的问题,它显示了我的数据当前的结构。那还可怕吗?因为问题是,我有很多数据,我真的很难把它转换成这种格式。我有没有办法做到这一点?我真的不需要对这些数据做太多其他的事情;这就是我真正需要的。因此,如果不需要完全重新处理我的全部数据就可以做到这一点,那将是一个巨大的时间节约!谢谢我刚用过,它很完美!但是现在如果我尝试使用LIKE or=函数,那么当我为
    CustomerID
    输入'1'时,我得到的查询是LIKE的11和21,如果我使用=当我不输入任何东西时,我什么也得不到。@NightSpy2我可以理解为什么
    LIKE“*1*”
    会匹配
    11
    21
    ,等等。,但我真的不明白你说当你“不放任何东西”时,
    =
    什么也不返回是什么意思。无论您使用什么机制,都可能需要检查“不放入任何内容”(某处…)时的情况,并只使用“*”之类的
    ,这将匹配所有内容。例如,如果我使用
    =“*”&[Forms]![雇佣历史表格]![HistoryMovieID]&“*”
    在其中一个字段(或两个字段)上,我的查询没有返回任何内容。无论我是否输入值。我真的不明白你所说的“它可能需要检查这个案例”是什么意思。你的意思是我需要一些东西来检查它是否是空的,如果是空的,那么它会使用
    像“*”
    来过滤结果?@NightSpy2(1)
    =
    是一种文字比较,因此星号没有特殊意义,也不会匹配,因为你的
    电影ID
    值中没有实际的星号。但是,星号在与
    LIKE
    运算符一起使用时具有特殊的含义。(2) 是的,我正在考虑使用类似于
    IsNull()
    的方法来查看文本框是否为空,如果为空,则只需使用
    类似于“*”
    的方法来匹配所有记录。您好,谢谢您的提示。我编辑了我的问题,它显示了我的数据当前的结构。那还可怕吗?因为问题是,我有很多数据,我真的很难把它转换成这种格式。我有没有办法做到这一点?我真的不需要对这些数据做太多其他的事情;这就是我真正需要的。因此,如果不需要完全重新处理我的全部数据就可以做到这一点,那将是一个巨大的时间节约!谢谢我刚用过,它很完美!但是现在如果我尝试使用LIKE or=函数,那么当我为
    CustomerID
    输入'1'时,我得到的查询是LIKE的11和21,如果我使用=当我没有输入任何东西时,我什么也得不到。@NightSpy2我可以理解为什么
    LIKE“*1*”
    wo