0

我有一个数据输入表 (Access 2007),旨在查明捕获的动物是否已有 WHno。不幸的是,数据很混乱,而且这些不是一个唯一的标识符,因此必须执行几个测试来缩小搜索范围。

动物可能有 1 到 10 条不同的信息,这将有助于识别该动物在数据库中的存在。(到目前为止,该脚本只测试了其中大约一半)我认为最好的方法是根据用户选择的字段“构建”一个 DCount 和/或 SQL 语句。我希望测试一下是否已经填写了特定的文本字段框(未绑定),如果是,则将该段代码连接到 DCount/SQL 语句,然后转到下一个文本字段框进行测试。

完全构建语句后,我想测试一下已计算/选择了多少条记录。如果选择了一条记录,我想在 FormA 中显示结果。如果找到 2 条或更多记录,我想以多列表形式 (FormB) 显示记录,用户可以根据未测试但显示在 FormB 中的附加信息从中选择正确的动物。如果找到零记录,我想创建一个新记录,将输入到表格中的数据更新到表中。

我现在正在努力解决的障碍是构建 DCount 语句。我不断收到语法错误。当函数因为语法不完整而崩溃时,我不知道如何将它们零碎地拼凑起来(直到我完成“构建”它为止。)

我知道数据是一团糟。现场一片混乱,不同的人收集不同类型的信息,而且并非所有应该在纸质表格上输入的数据都被完整填写——如果有的话。数据收集程序不太可能很快改变。

想法?也欢迎使用不同但更简单的方法想法。新手,不确定我所有的编程选项。

另外,这个声明在轰炸之前可以持续多久?

到目前为止的代码:

Private Sub GenerateWHno_Click()
Dim rs As DAO.Recordset

If IsNull(Forms!F_HotelEntry!txtSpecies) Or (Forms!F_HotelEntry!txtSpecies) = "" Then
    MsgBox "Species is a required field. Please enter a species"
    Exit Sub

End If


MsgBox txtSpecies

' Each line of code below indicates a data entry field(s) that needs testing and appended to SpeciesCount if "true". The first line is unchanging and is declared upfront.

'SpeciesCount = DCount("[Species]", "AnimalInfo", "(nz([Status])= '' OR [Status] = 'Alive' OR [Status] = 'Unknown') AND ([Species]= '" & txtSpecies & "')" _
'    & "AND (((nz([L_ET_Color1])= '" & Nz(txtL_ET_Color1) & "' AND nz([L_ET_No1])= '" & nz(txtL_ET_No1) & "')" _
'    & "AND (((nz([R_ET_Color1])= '" & Nz(txtR_ET_Color1) & "' AND nz([R_ET_No1])= '" & nz(txtR_ET_No1) & "')" _
'    & "AND nz([L_ET_No2])= '" & nz(txtL_ET_No2) & "')" _
'    & "AND nz([R_ET_No2])= '" & nz(txtR_ET_No2) & "')" _
'    & "")


'If txtL_ET_Color Is Not Null Or txtL_ET_No Is Not Null Then
    'LET1 = & "AND (((nz([L_ET_Color1])= '" & Nz(txtL_ET_Color1) & "' AND nz([L_ET_No1])= '" & nz(txtL_ET_No1) & "')" _
    'Species Count = SpeciesCount & LET1
'End If

'If txtR_ET_Color Is Not Null Or txtR_ET_No Is Not Null Then
    'RET1 = & "AND (((nz([R_ET_Color1])= '" & Nz(txtR_ET_Color1) & "' AND nz([R_ET_No1])= '" & nz(txtR_ET_No1) & "')" _
    'Species Count = SpeciesCount & RET1
'End If

'If txtL_ET_No2 Is Not Null Then
    'LET2 = AND nz([L_ET_No2])= '" & nz(txtL_ET_No2) & "')" _
'Species Count = SpeciesCount & LET2
'End If

'If txtR_ET_No2 Is Not Null Then
    'RET2 = AND nz([R_ET_No2])= '" & nz(txtR_ET_No2) & "')" _
'Species Count = SpeciesCount & RET2
'End If

'There are about 4 more options/fields to add to the script but you get the idea.

'Thus: If user selected Species, and filled out L_ET_Color1 and/or L_ET_No1, the final concatenation (DCount statement)would look like this:
SpeciesCount = DCount("[Species]", "AnimalInfo", "([Status]= 'Alive' OR [Status] = 'Unknown' OR nz([Status]) = '') AND [Species]= '" & txtSpecies & "' AND (nz([L_ET_Color1])= '" & Nz(txtL_ET_Color1) & "' AND nz([L_ET_No1])= '" & Nz(txtL_ET_No1) & "')")

    If SpeciesCount > 1 Then
        MsgBox SpeciesCount & " Greater than 1. Please select correct animal"
        'Create SQL statement that mimics DCount statement and display all fields from AnimalInfo table as multilisting to select from

    ElseIf SpeciesCount = 0 Then
        MsgBox "You need a new WHno"

        WHno = Nz(DMax("WHno", "AnimalInfo")) + 1
        MsgBox WHno

        Set rs = CurrentDb.OpenRecordset("AnimalInfo")
            rs.AddNew
            rs!WHno = WHno
            rs!Species = txtSpecies
            rs!L_ET_Color1 = txtL_ET_Color1
            rs!L_ET_No1 = txtL_ET_No1
            rs.Update
            rs.Close
    Else
        'Create SQL statement that mimics DCount statement and display all fields from AnimalInfo table as single listing in a form.
        MsgBox "You're WHno is " & WHno & " Is this the correct WHno?"
    End If
    Forms!F_HotelEntry!txtSpecies = ""
    Forms!F_HotelEntry!txtL_ET_Color1 = ""
    Forms!F_HotelEntry!txtL_ET_No1 = ""
End Sub
4

1 回答 1

0

我建议首先将条件组合成一个字符串变量。在那里,您可以通过 Debug.Print 打印其内容并查看问题可能是什么。

如果仅通过检查无法发现问题,请将生成的字符串粘贴到正确查询的 Sql 视图中,然后查看 Access 是否为您提供了有关切换到设计视图的有用信息。

于 2013-11-14T18:03:41.157 回答