运行时错误"13"类型不匹配,单击VBA输入框上的取消



我使用输入框将值传递给存储过程。但是,当我单击"取消"时,会出现运行时错误"13"类型不匹配错误。我试图包含一个出口子命令,但没有成功。下面的代码。如有任何帮助,我们将不胜感激。谢谢

选项显式

Sub reverse_posted()
Const PROC = "ashcourt_concrete_balfour_reverse_posting"
Dim con As ADODB.Connection, cmd As ADODB.Command, i As Long
i = InputBox("Invoice Number to be re-posted")
If Len(i) < 1 Then Exit Sub
Set con = New ADODB.Connection
con.Open "Provider=SQLOLEDB;Data Source=ashcourt_app1;" & _
"Initial Catalog=ASHCOURT_Weighsoft5;" & _
"Integrated Security=SSPI;Trusted_Connection=Yes;"
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = con
.CommandType = adCmdStoredProc
.CommandText = PROC
.Parameters.Append .CreateParameter("P1", adInteger, adParamInput)
.Execute , i
End With
con.Close
Set con = Nothing
MsgBox ("Invoice Number " & i & " reversed")
End Sub

InputBox返回一个字符串
当您按Cancel时,它会返回不是数字的"",因此您会得到错误(如果您输入文本并按OK,则会得到相同的错误(。

也许使用两个变量——i As StringlngI As Long

Dim i As String, lngI As Long
i = InputBox("Invoice Number to be re-posted")
If IsNumeric(i) Then
lngI = CLng(i)
Else
Exit Sub
End If

InputBox总是返回文本,因此在取消时为空字符串,因此使用Val:

i = Val(InputBox("Invoice Number to be re-posted"))
If i = 0 Then Exit Sub

相关内容

  • 没有找到相关文章

最新更新