Question

I tried to pass argument from one procedure to another. Everything looks ok, argument was passed, however it does match criteria given in Select case scenario.

What am I doing wrong?

Procedure passing argument:

Private Sub but_next_Click()

Call zmien_fakture("but_next")

End Sub

Procedure receiving argument:

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
Was it helpful?

Solution

Replace Case txt_name = "but_next" with Case "but_next". Correct code is:

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

Why your syntax is not working:

When you're using Case txt_name = "but_next", statement txt_name = "but_next" evaluates to TRUE or FALSE and you get:

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

But in Select Case txt_name variable txt_name is NOT True or False and you actually jumps to Case Else part.

Alternative approach is to change Select Case txt_name to Select Case TRUE like this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top