如果test为true,则通过concetenation在VBA中构建DCount / SQL语句

时间:2013-11-13 21:03:37

标签: ms-access-2007 access-vba

我有一个数据输入表单(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

1 个答案:

答案 0 :(得分:0)

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

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