我试图将参数从一个程序传递到另一个程序。一切看起来都不错,参数被传递,但它确实符合Select case
场景中给出的标准。
我做错了什么?
传递参数的过程:
Private Sub but_next_Click()
Call zmien_fakture("but_next")
End Sub
接收参数的程序:
Private Sub zmien_fakture(ByVal txt_name As String)
Select Case txt_name
Case txt_name = "but_next"
If Lastrow > nr_faktury_dol Then
nr_faktury_dol = nr_faktury_dol + 1
Else
MsgBox ("To jest ostatnia faktura")
End If
Case txt_name = "but_prev"
If nr_faktury_dol <> 1 Then: nr_faktury_dol = nr_faktury_dol - 1
Case Else
End Select
End Sub
答案 0 :(得分:2)
将Case txt_name = "but_next"
替换为Case "but_next"
。正确的代码是:
Private Sub zmien_fakture(ByVal txt_name As String)
Select Case txt_name
Case "but_next"
If Lastrow > nr_faktury_dol Then
nr_faktury_dol = nr_faktury_dol + 1
Else
MsgBox ("To jest ostatnia faktura")
End If
Case "but_prev"
If nr_faktury_dol <> 1 Then: nr_faktury_dol = nr_faktury_dol - 1
Case Else
End Select
End Sub
为什么语法不起作用
当您使用Case txt_name = "but_next"
时,语句txt_name = "but_next"
会评估为TRUE
或FALSE
,您会得到:
Select Case txt_name ' txt_name="but_next", but txt_name<>TRUE and txt_name<>FALSE
Case True
'do something
Case False
'do something
Case Else
End Select
但在Select Case txt_name
变量中txt_name
不是True
或False
,而您实际上是跳到Case Else
部分。
替代方法是将Select Case txt_name
更改为Select Case TRUE
,如下所示:
Private Sub zmien_fakture(ByVal txt_name As String)
Select Case True
Case txt_name = "but_next"
If Lastrow > nr_faktury_dol Then
nr_faktury_dol = nr_faktury_dol + 1
Else
MsgBox ("To jest ostatnia faktura")
End If
Case txt_name = "but_prev"
If nr_faktury_dol <> 1 Then: nr_faktury_dol = nr_faktury_dol - 1
Case Else
End Select
End Sub