MS Access VBA call sub works on the first pass but not on the second

被刻印的时光 ゝ 提交于 2020-01-25 08:39:21

问题


I have hit a wall and I am completely at a loss.

So I have a Form in MS Access. In it I have a single text box and a single button. When I type in a number in the text box and then click the button it opens a public sub which then runs a few queries, updates the database, displays a text box and then clears out the text box. This all works perfectly.

My issue is trying to do the exact same thing with hitting enter in the text box. The strangest thing is that the code works fine right after I open up the form, but all subsequent attempts give the following error until I close the form and reopen it:

Data type mismatch in criteria expression.

For the life of me I cannot figure out why it does what I want the first time, then falls apart on me.

Here is the complete VBA code for reference:

    Option Compare Database
Public Sub Cut_Update()

    On Error GoTo Cut_Update_Err

        DoCmd.OpenQuery "UPDATE_WIP_Cut", acViewNormal, acEdit
        DoCmd.OpenQuery "UPDATE_LastRun", acViewNormal, acEdit

        MsgBox "Database Updated"
        [Forms]![Portal_02_Cut]![WO_Num].Value = ""
Cut_Update_Exit:
        Exit Sub

Cut_Update_Err:
        MsgBox Error$
        Resume Cut_Update_Exit

End Sub
'------------------------------------------------------------
' Return in Textbox
'
'------------------------------------------------------------
Private Sub WO_Num_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
        Call Cut_Update
        Me.Refresh
    End If
End Sub
'------------------------------------------------------------
' Command2_Click
'
'------------------------------------------------------------
Private Sub Command2_Click()
    Call Cut_Update
End Sub

回答1:


The only thing you do in between the calls in the form is:

[Forms]![Portal_02_Cut]![WO_Num].Value = ""

So, try with either:

[Forms]![Portal_02_Cut]![WO_Num].Text = ""

or:

[Forms]![Portal_02_Cut]![WO_Num].Value = Null



回答2:


My only guess is that the update is trying to update with a non numeric value. Let me know

       Option Compare Database 
       Public Sub Cut_Update()

                On Error GoTo Cut_Update_Err

                '' Make sure it is numeric
                If IsNumeric([WO_Num].Value) Then

                    DoCmd.OpenQuery "UPDATE_WIP_Cut", acViewNormal, acEdit
                    DoCmd.OpenQuery "UPDATE_LastRun", acViewNormal, acEdit

                    MsgBox "Database Updated"
                    [Forms]![Portal_02_Cut]![WO_Num].Value = ""

                End If

            Cut_Update_Exit:
                    Exit Sub

            Cut_Update_Err:
                    MsgBox Error$
                    Resume Cut_Update_Exit

     End Sub 
'------------------------------------------------------------ ' Return in Textbox ' 
'------------------------------------------------------------ 
Private Sub WO_Num_KeyUp(KeyCode As Integer, Shift As Integer)
                If KeyCode = 13 Then
                    Call Cut_Update
                    Me.Refresh
                End If 
End Sub 
'------------------------------------------------------------ ' Command2_Click ' 
'------------------------------------------------------------ 
        Private Sub Command2_Click()
                Call Cut_Update 
        End Sub


来源:https://stackoverflow.com/questions/59556810/ms-access-vba-call-sub-works-on-the-first-pass-but-not-on-the-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!