Using "Like" in VB

  • Thread starter Thread starter David W
  • Start date Start date
D

David W

This statement always returns false
I am probably doing this wrong

Dim MyHour
MyHour = Me.hour1 Like "[.5 - 24.0]"
If MyHour = False Then

I am trying to limit the input into a textbox to 1/2 an hour through 24
hours.

What have I done wrong?
 
Like is used for evaluating strings. The comparison you are trying to make
is numeric. You need to do something like this...

If Me.Hour1 < .5 or Me.Hour1 > 24 Then
MyHour = False
End if

I'm assuming that Hour1 is limited to numeric data...otherwise, you need to
test for that. Or if the data is actually date/time, you need to convert it
and write the comparison differently.
 
Your code step:
MyHour = Me.hour1 Like "[.5 - 24.0]"

is not a proper syntax for using "wildcard ranges" for such testing.

If you want to ensure that the user has entered a number that is a multiple
of .5 and that is not larger than 24, you'll need a slightly more "complex"
validation test.

In your case, why not use an Input Mask to limit the entry to "99\.9"
format, and then test the value of the two parts of the number for being in
the right range:

If Not Fix(Me.hour1) Between 0 And 24 Then
' user entered a number that is too large
ElseIf Right(Me.hour1, 1) <> 0 And Right(Me.hour1, 1) <> 5 Then
' user entered a number that is not a multiple of 0.5
End If
 
That almost worked great, but it will not take 1.0 for some reason.
Any ideals?

Ken Snell said:
Your code step:
MyHour = Me.hour1 Like "[.5 - 24.0]"

is not a proper syntax for using "wildcard ranges" for such testing.

If you want to ensure that the user has entered a number that is a
multiple of .5 and that is not larger than 24, you'll need a slightly more
"complex" validation test.

In your case, why not use an Input Mask to limit the entry to "99\.9"
format, and then test the value of the two parts of the number for being
in the right range:

If Not Fix(Me.hour1) Between 0 And 24 Then
' user entered a number that is too large
ElseIf Right(Me.hour1, 1) <> 0 And Right(Me.hour1, 1) <> 5 Then
' user entered a number that is not a multiple of 0.5
End If

--

Ken Snell
<MS ACCESS MVP>


David W said:
This statement always returns false
I am probably doing this wrong

Dim MyHour
MyHour = Me.hour1 Like "[.5 - 24.0]"
If MyHour = False Then

I am trying to limit the input into a textbox to 1/2 an hour through 24
hours.

What have I done wrong?
 
Define what you mean by "won't take".

Post the actual input mask that you're using.
--

Ken Snell
<MS ACCESS MVP>




David W said:
That almost worked great, but it will not take 1.0 for some reason.
Any ideals?

Ken Snell said:
Your code step:
MyHour = Me.hour1 Like "[.5 - 24.0]"

is not a proper syntax for using "wildcard ranges" for such testing.

If you want to ensure that the user has entered a number that is a
multiple of .5 and that is not larger than 24, you'll need a slightly
more "complex" validation test.

In your case, why not use an Input Mask to limit the entry to "99\.9"
format, and then test the value of the two parts of the number for being
in the right range:

If Not Fix(Me.hour1) Between 0 And 24 Then
' user entered a number that is too large
ElseIf Right(Me.hour1, 1) <> 0 And Right(Me.hour1, 1) <> 5 Then
' user entered a number that is not a multiple of 0.5
End If

--

Ken Snell
<MS ACCESS MVP>


David W said:
This statement always returns false
I am probably doing this wrong

Dim MyHour
MyHour = Me.hour1 Like "[.5 - 24.0]"
If MyHour = False Then

I am trying to limit the input into a textbox to 1/2 an hour through 24
hours.

What have I done wrong?
 
It looks to me like you want to do this. The <> signs were mixed up in the
previous post.

If Me.Hour1 > .5 or Me.Hour1 < 24 Then
MyHour = False
End if
 
99\.9 is the input mask on the textbox
it will accept 1.5, but not 1.0

Ken Snell said:
Define what you mean by "won't take".

Post the actual input mask that you're using.
--

Ken Snell
<MS ACCESS MVP>




David W said:
That almost worked great, but it will not take 1.0 for some reason.
Any ideals?

Ken Snell said:
Your code step:
MyHour = Me.hour1 Like "[.5 - 24.0]"

is not a proper syntax for using "wildcard ranges" for such testing.

If you want to ensure that the user has entered a number that is a
multiple of .5 and that is not larger than 24, you'll need a slightly
more "complex" validation test.

In your case, why not use an Input Mask to limit the entry to "99\.9"
format, and then test the value of the two parts of the number for being
in the right range:

If Not Fix(Me.hour1) Between 0 And 24 Then
' user entered a number that is too large
ElseIf Right(Me.hour1, 1) <> 0 And Right(Me.hour1, 1) <> 5 Then
' user entered a number that is not a multiple of 0.5
End If

--

Ken Snell
<MS ACCESS MVP>


This statement always returns false
I am probably doing this wrong

Dim MyHour
MyHour = Me.hour1 Like "[.5 - 24.0]"
If MyHour = False Then

I am trying to limit the input into a textbox to 1/2 an hour through 24
hours.

What have I done wrong?
 
What is the code that you're using? Are you saying the the code is rejecting
the entry? or the input mask is rejecting the entry?

--

Ken Snell
<MS ACCESS MVP>

David W said:
99\.9 is the input mask on the textbox
it will accept 1.5, but not 1.0

Ken Snell said:
Define what you mean by "won't take".

Post the actual input mask that you're using.
--

Ken Snell
<MS ACCESS MVP>




David W said:
That almost worked great, but it will not take 1.0 for some reason.
Any ideals?

Your code step:
MyHour = Me.hour1 Like "[.5 - 24.0]"

is not a proper syntax for using "wildcard ranges" for such testing.

If you want to ensure that the user has entered a number that is a
multiple of .5 and that is not larger than 24, you'll need a slightly
more "complex" validation test.

In your case, why not use an Input Mask to limit the entry to "99\.9"
format, and then test the value of the two parts of the number for
being in the right range:

If Not Fix(Me.hour1) Between 0 And 24 Then
' user entered a number that is too large
ElseIf Right(Me.hour1, 1) <> 0 And Right(Me.hour1, 1) <> 5 Then
' user entered a number that is not a multiple of 0.5
End If

--

Ken Snell
<MS ACCESS MVP>


This statement always returns false
I am probably doing this wrong

Dim MyHour
MyHour = Me.hour1 Like "[.5 - 24.0]"
If MyHour = False Then

I am trying to limit the input into a textbox to 1/2 an hour through
24 hours.

What have I done wrong?
 
Alternative code that you might try:

If Not Fix(Me.hour1) Between 0 And 24 Then
' user entered a number that is too large
ElseIf Right(Me.hour1, 1) Like "[1-4]" Or Right(Me.hour1, 1) Like
"[6-9]" Then
' user entered a number that is not a multiple of 0.5
End If
 
I could get this to work
If Not Fix(Me.hour1) Between 0 And 24 Then
the between caused a compile, error expected: then or goto
so I did
If Me.hour1 > 24 Then
that part worked fine,
the not a multiple of 5 is where the problem resides now.
it will only take anything ending with .5
I tried to enter both 1 and 1.0 and I kept getting an error.


If Me.hour1 > 24 Then
' user entered a number that is too large
MsgBox "You cannot exceed 24 hours a day!", vbExclamation +
vbOKOnly, "24 Hour Day Limit Exceeded"
Me.hour1.Undo
Cancel = True
ElseIf Right(Me.hour1, 1) <> 0 And Right(Me.hour1, 1) <> 5 Then
' user entered a number that is not a multiple of 0.5
MsgBox "Use only 1/2 hour Increments.", vbExclamation + vbOKOnly,
"Less than 1/2 Hour"
Me.hour1.Undo
Cancel = True
End If
 
My apologies....

If Not (Fix(Me.hour1) >= 0 And Fix(Me.hour1) <= 24) Then
' user entered a number that is too large
ElseIf Right(Me.hour1, 1) Like "[1-4]" Or Right(Me.hour1, 1) Like
"[6-9]" Then
' user entered a number that is not a multiple of 0.5
End If
 
Back
Top