问题
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