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

时间:2013-04-16 03:14:44

标签: forms ms-access

只是一些背景信息。我的表(HireHistory)中有50列(水平)。我有一个Form(HireHistoryForm),它有2个文本框(HistoryMovieID和HistoryCustomerID)和一个按钮(该按钮运行查询'HireHistoryQuery')

以下是我的数据摘录(CustomerID位于顶部): Data

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

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

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

=[Forms]![HireHistoryForm]![HistoryCustomerID]

但它没有用。我的查询刚刚返回一个标有“10”的列,行只是由“10”组成。

如果你能提供帮助,我会非常感激。 :)

1 个答案:

答案 0 :(得分:1)

没有违法行为(或者尽可能少),但这是构建数据的 可怕的 方式。您真的需要像这样重组它:

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
...

如果保留当前的数据结构,那么

  1. 你会生气,

  2. 其他任何人都不太可能去附近这个问题。

  3. 修改

    您修改后的数据结构是非常轻微的改进,但它仍然对您不利。请考虑在您的其他问题here中,当您进行查询时,您实际上是在寻找一种“动态”“修复”数据结构的方法。

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

    ID         - AutoNumber, Primary Key
    CustomerID - Number(Long Integer), Indexed (duplicates OK)
    MovieID    - Text(4), Indexed (duplicates OK)
    HireDate   - Date/Time, Indexed (duplicates OK)
    

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

    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日期格式,因此 仔细检查日期转换 以确保它们正确转换。