Solved Behavior of an inputbox (1 Viewer)

zelarra821

Registered User.
Local time
Today, 06:38
Joined
Jan 14, 2019
Messages
809
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 21:38
Joined
Aug 30, 2003
Messages
36,124
For starters, your test for an operator needs to use And rather than Or.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:38
Joined
Oct 29, 2018
Messages
21,455
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?
 

zelarra821

Registered User.
Local time
Today, 06:38
Joined
Jan 14, 2019
Messages
809
In the second mailbox, I do not want the user to be able to enter letters, and my code does not detect it correctly.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:38
Joined
Oct 29, 2018
Messages
21,455
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?
 

zelarra821

Registered User.
Local time
Today, 06:38
Joined
Jan 14, 2019
Messages
809
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.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:38
Joined
Oct 29, 2018
Messages
21,455
Good work! Good luck with your project.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 21:38
Joined
Aug 30, 2003
Messages
36,124
Did you change the Or's to And's in the first test? Or will not work correctly.
 

zelarra821

Registered User.
Local time
Today, 06:38
Joined
Jan 14, 2019
Messages
809
pbaldy, yes, I changed it, and now it works perfectly. I forgot to tell you. Thank you.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 21:38
Joined
Aug 30, 2003
Messages
36,124
No worries, glad it worked for you.
 

Users who are viewing this thread

Top Bottom