-1

I have an inputbox that asks the user for the number of relatives they live with. But in the inputbox when I clicked on the cancel or accept button I got a type mismatch error. However, what I was able to solve was that when I click cancel or accept, I get an empty form and I simply want it to do nothing that does not execute any action.

   numFamiliares = Val(InputBox("How many family members do you live with?"))
    If numFamiliares = 0 Then Exit Sub

    familiarActual = 1

....Here goes the rest of the code....

I simply want it not to show me the empty form as it is doing, only if the user clicks accept or cancel, simply do nothing, it must execute the rest of the code only if the number of family members is entered.Link File. Here I leave the link of the file if anyone wants to see it, it was built in execution mode.

5
  • I can't get your code to return an error - numFamiliares = Val(InputBox("How many family members do you live with?")) assigns either the number you enter or 0 if you enter text. Commented Mar 20, 2024 at 14:26
  • 1
    What if someone lives alone? They live with 0 family members and as your code stands, it will exit the sub Commented Mar 20, 2024 at 14:35
  • It does not return an error, because as I mentioned, I solved the type problem, the problem is that clicking accept or cancel leaves the form empty and that is not what is wanted. I would like, for example, that if I hit cancel it would simply exit the inputbox and do nothing, not execute the rest of the code. I don't see why the negative vote, just because I have that doubt. In the link I put the file so that you can understand me better. Commented Mar 20, 2024 at 14:42
  • VAL is returning the 0. The Inputbox is returning an empty string. You could declare a string variable for the inputbox result, if it's an empty string then exit the procedure otherwise check it's a numeric value and then wrap it in VAL. Commented Mar 20, 2024 at 15:04
  • Move the 'input part' to a new sub in a standard module (e.g. Module1) and declare the variable globally with Public numFamiliares As Long outside the sub. After Exit Sub add something like Dim uf As Object: Set uf = New Frm.Familiar: uf.Show. But there might be much more to it: read about how it's properly done here. Commented Mar 20, 2024 at 15:37

1 Answer 1

0

An inputbox returns a string value so check it's not blank first, then check if it's numeric before acting on it.

Sub Test()

    Dim ans As String
    ans = InputBox("How many family members do you live with?")
    
    Dim numFamiliares As Long
    If ans <> "" Then
        If IsNumeric(ans) Then
            numFamiliares = Val(ans)
        Else
            MsgBox "Input is not numeric.", vbOKOnly + vbInformation
        End If
    Else
        MsgBox "Input is blank.", vbOKOnly + vbInformation
    End If

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for answering. In fact, your exercise is coherent but in the execution when I click cancel or accept I get the message but it instantly enters the form. When you click the cancel button, it would ideally exit the inputbox and do nothing else. I left a link where the file is. If it's not too much trouble, I don't know if you can look at it and see what I'm talking about, sorry for so much confusion. The link is in the initial issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.