Solved Behavior of an inputbox

zelarra821

Registered User.
Local time
Today, 20:46
Joined
Jan 14, 2019
Messages
834
Hi. I'm trying to improve the behavior of an inputbox, but I can not. This is the code that I have developed (although I have it in Spanish, I have translated it into English):

Code:
    On Error Resume Next
    Dim miFiltro As String
    Dim Valor As String
    Dim Signo As String
        'If the value of the field is numeric, we can continue
        If IsNumeric(Screen.PreviousControl) Then
            'We leave the event if the name of the field matches any of the following
                If Screen.PreviousControl.Name = "Autor" Then
                    MsgBox "El campo que has seleccionado no está disponible para este filtro.", vbInformation
                Exit Sub
                End If
                If Screen.PreviousControl.Name = "Subgenero" Then
                    MsgBox "El campo que has seleccionado no está disponible para este filtro.", vbInformation
                Exit Sub
                End If
                If Screen.PreviousControl.Name = "Formato" Then
                    MsgBox "El campo que has seleccionado no está disponible para este filtro.", vbInformation
                Exit Sub
                End If
                If Screen.PreviousControl.Name = "Goodreads" Then
                    MsgBox "El campo que has seleccionado no está disponible para este filtro.", vbInformation
                Exit Sub
                End If
                If Screen.PreviousControl.Name = "EsSerie" Then
                    MsgBox "El campo que has seleccionado no está disponible para este filtro.", vbInformation
                Exit Sub
                End If
                If Screen.PreviousControl.Name = "Serie" Then
                    MsgBox "El campo que has seleccionado no está disponible para este filtro.", vbInformation
                Exit Sub
                End If
                If Screen.PreviousControl.Name = "Biblioteca" Then
                    MsgBox "El campo que has seleccionado no está disponible para este filtro.", vbInformation
                Exit Sub
                End If
            'We introduce our first InputBox
                Signo = InputBox("Introduce el signo: mayor (>), menor (<), mayor que (>=) o menor que (<=).", "Filtrar por rango")
            'If the user cancels the InputBox, we exit
                If StrPtr(Signo) = 0 Then Exit Sub
            'If the user leaves the Input blank, a message appears indicating the error and we exit
                If Signo = "" Then
                    MsgBox "Debes introducir un carácter válido.", vbInformation
                    Exit Sub
                End If
            'If the user enters a character that does not correspond to a comparison operator, a message appears with the error and we exit
                If Signo <> ">" Or Signo <> "<" Or Signo <> ">=" Or Signo <> "<=" Then
                    MsgBox "Has introducido un carácter erróneo.", vbInformation
                    Exit Sub
                End If
            'We introduce our second InputBox
                Valor = InputBox("Introduce el valor")
            'If the user cancels the InputBox, we exit
                If StrPtr(Valor) = 0 Then Exit Sub
            'If the user leaves the Input blank, a message appears indicating the error and we exit
                If Valor = "" Then
                    MsgBox "Debes introducir un carácter válido.", vbInformation
                    Exit Sub
                End If
            'If the user enters a non-numeric character, a message appears with the error and we exit
                If IsNumeric(Valor) = False Then
                    MsgBox "Has introducido un carácter erróneo.", vbInformation
                    Exit Sub
                End If
            'Let's take the selected value and the field and create the approximate filter
                miFiltro = Screen.PreviousControl.Name & Signo & Replace(Valor, ",", ".")
            'We apply the filter to the form
                Me.Filter = miFiltro
                Me.FilterOn = True

And the problems I have are:
1. In the first inputbox (signo), I want to achieve that if the user enters a value that is not a comparison operator (>, <,> =, <=), an error will be thrown telling him that he must write a valid character.
2. In the second inputbox (valor), I want to get that if user writes any value that is not a number, I will error.

I think that would solve that, but I would have to try it once these two points work well.

Thanks.
 
For starters, your test for an operator needs to use And rather than Or.
 
Hi. What exactly is the "problem?" Are you getting an error or is the user able to enter invalid characters, and your code is not catching them?
 
In the second mailbox, I do not want the user to be able to enter letters, and my code does not detect it correctly.
 
In the second mailbox, I do not want the user to be able to enter letters, and my code does not detect it correctly.
I am thinking the IsNumeric() check would be enough. Can you give us an example of what the user is entering but you're not catching with the IsNumeric() check?
 
I just tried it and now it works perfectly. He had declared Value as Double. This made me not catch perfectly if the user canceled or entered an empty value. I have changed it to String, and now that works. Then I checked if introducing gave error, and yes it gives. So it works as I want.
Thank you.
 
Good work! Good luck with your project.
 
Did you change the Or's to And's in the first test? Or will not work correctly.
 
pbaldy, yes, I changed it, and now it works perfectly. I forgot to tell you. Thank you.
 
No worries, glad it worked for you.
 

Users who are viewing this thread

Back
Top Bottom