检查输入是否已完成

时间:2014-08-04 06:25:36

标签: vba access-vba ms-access-2007

我正在寻找一种更智能的方法来检查记录中的文本是否已经存在。 基本上我写了一个存储分钟的数据库。 由于只有最新信息应该存储在文本框中(有条目),“旧”信息会进入历史记录框,我正在寻找一种方法来防止此历史记录框中的双重条目。

出于这个原因,我使用InStr函数来检查历史字段中是否已存在前10个字母,它是否未添加信息。我从位置100开始,因为我在这些条目之间有不同的标题。 实际上它大部分时间都有效,但我无法弄清楚为什么它不会多次添加信息然后这些信息实际上是新的,所以我寻找一种更聪明的方式或者是否有人看到错误?

Private Sub txt_Comments_W9_LostFocus()
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ' History function.
 ' Since only the latest information should be stored at the text field the old/other     information will be stored at the history box
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim temp As String
Dim newComment As String
Dim Version As String

temp = " "
newComment = " "

Version = "<b>" & "****************************************" & "Version " & Date & ": " & "****************************************" & "</b>"
newComment = Version & txt_Comments_W9.Value


If Len(Nz(txt_History_W9.Value)) = 0 Then
    If Len(Nz(txt_Comments_W9.Value)) = 0 Then
        Exit Sub
    Else
        txt_History_W9.Value = "<div>" & newComment & "<BR>"
    End If
Else
    temp = txt_History_W9.Value
    If InStr(100, temp, Mid(txt_Comments_W9.Value, 1, 10)) = 0 Then
        txt_History_W9.Value = "<div>" & temp & newComment & "<BR>"
    End If
End If

End Sub

由于

2 个答案:

答案 0 :(得分:0)

如果Len(Nz(txt_History_W9.Value))返回0,您将在其他人之后跳过检查。这是故意的吗?

此外,您使用Mid(txt_Comments_W9.Value, 1, 10)代替Left(txt_Comments_W9.Value, 10)是否有原因。

如果Instr的参数如下:InStr([start,]string1,string2[,compare]),它可以返回:

If string1 is "" - InStr returns 0
If string1 is Null - InStr returns Null
If string2 is "" - InStr returns start
If string2 is Null - InStr returns Null
If string2 is not found - InStr returns 0
If string2 is found within string1 - InStr returns the position at which match is found
If start > Len(string1) - InStr returns 0

确保您知道InStr函数在不匹配时返回的内容。

答案 1 :(得分:0)

这个表格结构化的方式很奇怪。理想情况下,你的桌面设计应该是这样的。

tbl_MinutesHistory
------------------
minID   |   minDate     |   minEntree   |   minNotes
--------+---------------+---------------+--------------------------------
1       |   03/04/2014  |   Paul        |   Meeting for Boss birthday
2       |   05/05/2014  |   Eugin       |   Meeting to elect new boss
3       |   02/06/2014  |   Francis     |   Company shutting down meeting

然后您的历史记录框(我猜它是一个列表框)将包含除最后日期之外的所有内容。 Rowsource就像是

SELECT minDate, minEntree, minNotes
FROM tbl_MinutesHistory
WHERE minID Not In (SELECT Max(minID) As maxOfID FROM tbl_MinutesHistory);

你使用代码使事情变得更复杂!

相关问题