这是怎么返回空值? [英] How is this returning a blank value?

查看:160
本文介绍了这是怎么返回空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这个code是指作为一个简单的搜索系统进入某些记录的记录。我本来就这样,他们必须点击才能执行搜索的btnGoToID按钮。我决定只让它多了几分人性化,并使其所以搜索领域听了回车键,将执行搜索也是如此。

这是我遇到的时候code到达 strID =修剪(NZ(Me.txtSearch.Value,))问题的价值将随机返回一个空字符串,即使在视觉上我可以看到,有一个在文本框中的值。

我一直没能缩小任何图案时,会出现这个问题了。在这一点上我真的不知道如何解决这个问题,也没有词来搜索可能产生任何结果在谷歌。所有我能说的是,这似乎是记录之间的变化会影响与否的搜索经历。

这一直致力于直到我把txtSearch_Key preSS子过程研究。

 ===================================== =======================================
txtSearch_Key preSS
================================================= ===========================
私人小组txtSearch_Key preSS(KeyAscii为整数)
    如果用户pressed进入
    如果KeyAscii = 13然后
        呼叫btnGoToID_Click
    结束如果
结束小组



================================================= ===========================
btnGoToID_Click
================================================= ===========================
'<<目的>>
允许用户搜索特定的ID
================================================= ===========================
私人小组btnGoToID_Click()
对错误转到Err_Handler

    昏暗的RS作为记录
    昏暗strID作为字符串

    设置RS = Me.RecordsetClone

    strID =修剪(NZ(Me.txtSearch.Value,))

    如果(strID&其中;>中)。然后

        转至ID
        rs.FindFirstID ='&放大器; strID和放大器; '

        如果rs.NoMatch然后
            MSGBOXID不存在
        其他

            如果我们有一个比赛,设置记录作为当前记录
            Me.Bookmark = rs.Bookmark
        结束如果
    其他
        MsgBox请输入一个有效的ID。,vbOKOnly,无效的ID
    结束如果


Exit_Handler:
在错误恢复下一页

    Me.txtSearch.Value =
    rs.Close
    设置RS =什么
    退出小组

Err_Handler:
    呼叫LOGERROR(Err.Number的,Err.Description它将txtSearch的&放大器; Me.Name)
    简历Exit_Handler
结束小组
 

在注释中的谈话我缩小了这个问题,是一大堆简单。这就产生了一个无效使用空的错误消息,即使有在文本字段中几个大字。为什么会发生,我能做些什么,使之拿起值在文本框中?

 ===================================== =======================================
txtUnitNoToSearch_Key preSS
================================================= ===========================
私人小组txtUnitNoToSearch_Key preSS(KeyAscii为整数)
    MSGBOX Me.txtUnitNoToSearch
结束小组
 

解决方案

我觉得你的问题是关系到一个文本框有2个属性的事实,。价值。文,这似乎相似,但不同的表现。

当你在文本框中正在进行中的编辑,变化的内容可通过。文属性。但是, .value的含量属性还没有被更新,以匹配。

在文本框的更新的事件, .value的后将包含新的内容。如果焦点已经从文本框搬走了,它的。文属性将不再,甚至可用。

对不起,我想不出该怎么解释这更好的。但我认为情况会更清楚本的键preSS 的事件过程:

私人小组txtUnitNoToSearch_Key preSS(KeyAscii为整数)     Debug.Print文字:'&放大器; Me.txtUnitNoToSearch.Text和放大器; '     Debug.Print值:'&放大器; Me.txtUnitNoToSearch.Value和放大器; ' 结束小组

观赏 Debug.Print 输出在立即窗口中为您做出更改文本框的范围内。 (控制 + <大骨节病>先按g 打开立即窗口。)

最后一点是,仅仅 Me.txtUnitNoToSearch 让你的默认属性是。价值。记住这一点,你通过你的code,其余的工作。

So this code is meant to serve as a simple search system to go to certain records in a recordset. I originally had it so they had to click the btnGoToID button in order to perform the search. I decided to just make it a little more user friendly and make it so the search field listened for the Enter button and that would perform the search as well.

The issue that I am running into when the code gets to strID = Trim(Nz(Me.txtSearch.Value, "")) the value will randomly come back as an empty string even though visually I can see that there is a value in the textbox.

I haven't been able to narrow down any pattern for when this issue occurs. At this point I don't really even know how to troubleshoot this problem, nor the words to search for that could yield any results in Google. All I can say is that it SEEMS like changing between records will affect whether or not the search goes through.

This has always worked up until I put in the txtSearch_KeyPress sub procedure.

'============================================================================
' txtSearch_KeyPress
'============================================================================
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
    'If user pressed enter
    If KeyAscii = 13 Then
        Call btnGoToID_Click
    End If
End Sub



'============================================================================
' btnGoToID_Click
'============================================================================
' <<Purpose>>
' Allow the user to search for a specific ID 
'============================================================================
Private Sub btnGoToID_Click()
On Error GoTo Err_Handler

    Dim rs As Recordset
    Dim strID As String

    Set rs = Me.RecordsetClone

    strID = Trim(Nz(Me.txtSearch.Value, ""))

    If (strID <> "") Then

        'Go to the ID
        rs.FindFirst "ID = '" & strID & "'"

        If rs.NoMatch Then
            MsgBox "ID does not exist"
        Else

            'If we have a match, set the record as the current record
            Me.Bookmark = rs.Bookmark
        End If
    Else
        MsgBox "Please enter a valid ID.", vbOKOnly, "Invalid ID"
    End If


Exit_Handler:
On Error Resume Next

    Me.txtSearch.Value = ""
    rs.Close
    Set rs = Nothing
    Exit Sub

Err_Handler:
    Call LogError(Err.Number, Err.Description, "txtSearch on " & Me.Name)
    Resume Exit_Handler
End Sub

After the conversation in the comments I have narrowed down this question to be a whole lot simpler. This yields an "Invalid use of null" error message even after there are several characters in the text field. Why would that happen, and what can I do to make it pick up the values in the textbox?

'============================================================================
' txtUnitNoToSearch_KeyPress
'============================================================================
Private Sub txtUnitNoToSearch_KeyPress(KeyAscii As Integer)
    MsgBox Me.txtUnitNoToSearch
End Sub

解决方案

I think your problem is related to the fact that a text box has 2 properties, .Value and .Text, which appear similar but behave differently.

While you have an edit in progress in the text box, the changing content is available via the .Text property. However, the content of the .Value property has not yet been updated to match.

After the text box's After Update event, .Value will contain the new content. And if focus has moved away from the text box, its .Text property will no longer even be available.

Sorry, I couldn't think how to explain that better. But I think the situation will be clearer with this KeyPress event procedure:

Private Sub txtUnitNoToSearch_KeyPress(KeyAscii As Integer)
    Debug.Print "Text: '" & Me.txtUnitNoToSearch.Text & "'"
    Debug.Print "Value: '" & Me.txtUnitNoToSearch.Value & "'"
End Sub

Watch the output from Debug.Print in the Immediate window as you make changes to the context of the text box. (Ctrl+g will open the Immediate window.)

One final point is that just Me.txtUnitNoToSearch gets you its default property which is .Value. Keep that in mind as you work through the rest of your code.

这篇关于这是怎么返回空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆