Hey guys, im not sure if this is possible, i really hope so though!!
I have a form with a text box on it (txtStart_Time), this box is used to enter start times for jobs.
What i want to do is stop people from entering times other than in half hour intervals. ie: 10:30 ; 12:00 etc...
I dont want people to be able to enter 10:15 ; 12:04 etc...
is this possible to do??
the field is set to short time format.
Don't have the syntax, but you could use the TimeSerial function
to get the Total minutes. Then you can do a mod "60" on it
and if it is not = 0 and not = 30 then Msgbox and tell them to
enter it right.
Don't work with times much, that's tough. Experiment with it.
Code:
Dim Hrs As Integer
Dim Mins As Integer
Hrs = Val(Mid(Format(Me.txtTime, "hh:mm"), 1, 2))
Mins = Val(Mid(Format(Me.txtTime, "hh:mm"), 4, 2))
If Mins = 0 Or Mins = 30 Then
Exit Sub
ElseIf Mins > 0 And Mins <= 15 Then
Mins = 0
ElseIf Mins > 15 And Mins <= 45 Then
Mins = 30
Else
Mins = 0
Hrs = Hrs + 1
End If
Me.txtTime = TimeSerial(Hrs, Mins, 0)
cheers guys i ended up using Waynes example (i just felt like coding )
i dont suppose you could please explain what this line does (in english):
Code:
Hrs = Val(Mid(Format(Me.txtTime, "hh:mm"), 1, 2))
i know it is putting the hour values into the Hrs variable, but the mid and the 1,2 - is that just telling it to take the hh from the formatted time? (if you know what i mean)
also im a bit unclear of what the timeserial function does exactly, i tried to read up the help file - but you know what those are like!
thanks again guys!!
and Mile - i dont know why i didnt think of putting in a combo box?? i put it down to being late in the afternoon!!
Let's start with all the functions in it. Val(), Mid(), and Format() - in reverse order.
Format(expression, [optional format])
This gives you the opportunity to use a pre-made or user specified presentation of date, numerical, and string data types.
Mid(string, start, [optional length])
Allows you to select data from anywhere in a string by the specified starting point. The optional parameter allows you to specify the numbre of characters after the starting point to extract so saying Mid("Hello World", 2, 4) would return "ello". If the optional parameter is ignored then it would return the string from the starting point to the end so Mid("Hello World", 2) would return "ello World".
Val(string)
This returns a numerical value from a string datatype. Before using it, it may be best to use the IsNumeric() function to test the validity of the string for conversion prior to the actual conversion.
So, the line...
Code:
Hrs = Val(Mid(Format(Me.txtTime, "hh:mm"), 1, 2))
formats the value in txtTime to the hours:minutes format.
takes the string returned by the format from the first character and takes two characters*
converts the string number we've extracted from the time to a numerical value
i.e.
formatting returns 13:14
using the Mid function returns "13"
using the Val function returns 13
s0rtd said:
also im a bit unclear of what the timeserial function does exactly, i tried to read up the help file
The TimeSerial concatenates a time based on the three components that make up time: Hours, Minutes, and Seconds.
i.e.
TimeSerial(hours, minutes, seconds)
where hours= 13, minutes = 14, and seconds = 0 would return:
TimeSerial(13, 14, 0) => 13:14:00
DateSerial does the same for dates.
*The fact that Wayne's code extracts the hour value from the user-formatted time makes me wonder why he chose to use the Mid() function in lieu of the Left() function in the following line: