Click here to Skip to main content
16,004,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
is there a way I can make sure a user is putting a SPACE between the time (h:mm) and AM (or PM) when they TYPE into the textbox? the textbox data is then saved to a listview which is read and acted on. but I cannot get the actions to work unless there is a SPACE between the time and the AMPM.

What I have tried:

I have tried so many different ways my brain is CoNFuSeD!
Posted
Updated 3-Aug-24 12:31pm
v2
Comments
Ralf Meier 3-Aug-24 18:24pm    
Perhaps it is useful to see your code to be able to help you.
Basicly I would, if a special format is required, check the input and perhaps modify it before I transfer it to somewhere else ...
PIEBALDconsult 3-Aug-24 18:32pm    
Don't use a text box for that.
Dave Kreskowiak 3-Aug-24 21:12pm    
What are you using a textbox when a date/time picker would be more appropriate, offering consistent user experience and constancy in your data?
Nelek 3-Aug-24 21:14pm    
why don't you do it in 24 hours format? This way you would simplify the data entry and the validation.
Richard MacCutchan 4-Aug-24 8:55am    
As others have already asked: why are you using a textbox? Textboxes should only ever be used for simple text that does not need to be in any particular format. You cannot expect users to remember the required format to enter information. Use a control that allows them to enter the data in a format familiar to them, but which provides it in the format that your code requires, or better still provides the data in a non-formatted way, such as a DateTime, Time etc. object.

VB6 is ancient now, and very limited - it was superseded by VB.NET over twenty years ago! It really shouldn't be used for new development ...

It doesn't directly support Regular Expressions, which are the easiest way to process text string matching: ^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM|am|pm)$will match only valid times in 12 hour format.
However, they can be "added" to VB6: Regular Expressions in Visual Basic 6 - VB RegExp[^]
If you really can't bear to move to less outdated languages, then adding Regexes to VB6 woudl save you a lot of coding!
 
Share this answer
 
As mentioned above, VB6 has been dead for many a year. If you do however still want to use it, you should maybe look at using a Rich TextBox Control where you can format the input accordingly, there are still tons of examples online - set rich textbox format in vb6[^]

If you only have the option to use a text box then maybe format the result typed in the text box by the user when the textbox looses focus. I have not done VB6 for a very long time but if memory serves me right your code should look something similar to -

VB6
Private Sub TextBox1_LostFocus()
    Dim inputDateTime As Date
    Dim formattedDateTime As String
    
    ' Try to convert the input of the user to a valid date/time...
    On Error Resume Next
    inputDateTime = CDate(TextBox1.Text)
    On Error GoTo 0
    
    ' If input was valid by your user, format the date/time to be used in your listbox...
    If inputDateTime <> #12:00:00 AM# Then
        formattedDateTime = Format(inputDateTime, "mm/dd/yyyy hh:nn:ss ampm")
        TextBox1.Text = formattedDateTime
    Else
        ' Handle the invalid input...
        MsgBox "Invalid date/time format. Please enter a valid date and time.", vbExclamation
        TextBox1.SetFocus
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900