PDA

View Full Version : Input Date as Formatted



Charmande
06-11-2018, 09:50 AM
I'm attempting to have a user input a date into objTextBox and it must be formatted as ddmmmyy. If the date format is incorrect or something other than a date it'll post Invalid Date into objTextBox5. That part works but my code does not recognize the text objTextBox as a date entered in the format ddmmmyy to then paste into objTextBox3. Any help would be greatly appreciated, I'm a very novice VBA user and have wrecked my brain on this for many iterations lol figured it was time for help.

Sub LnSelR2_Date()
Dim objPresentaion As Presentation
Dim objSlide As Slide
Dim objTextBox As Shape
Dim objTextBox3 As Shape
Dim objTextBox5 As Shape


Set objPresentaion = ActivePresentation
Set objSlide = objPresentaion.Slides.Item(1)
Set objTextBox = objSlide.Shapes.Item("TextBox1")
Set objTextBox3 = objSlide.Shapes.Item("TextBox3")
Set objTextBox5 = objSlide.Shapes.Item("TextBox5")


If objTextBox.TextFrame.TextRange = Format(Date, "ddmmmyy") Then
objTextBox.TextFrame.TextRange.Cut
objTextBox3.TextFrame.TextRange.Paste
Else
objTextBox.TextFrame.TextRange.Cut
objTextBox5.TextFrame.TextRange = "Invalid Date"
End If


End Sub

SamT
06-11-2018, 01:58 PM
I don't really do PP, but generally in VBA and MSForms, something like

If IsDate(objTextBox.TextFrame.TextRange) Then
objTextBox.TextFrame.TextRange= Format(objTextBox.TextFrame.TextRange, "ddmmmyy")
Else
objTextBox5.TextFrame.TextRange = "Invalid Date"
End If
If this doesn't help, at least it will serve to bump your query to the top of the list.

John Wilson
06-11-2018, 01:59 PM
Are the textboxes ActivX textboxes added from the Developer Tab so that text can be entered during a slide show?

Charmande
06-11-2018, 02:46 PM
Are the textboxes ActivX textboxes added from the Developer Tab so that text can be entered during a slide show?

No these are regular textboxes. Text is still being added during the slideshow via action button macros.

Charmande
06-11-2018, 02:47 PM
I didn't get to test this just yet but thank you for the response. Any spark of new ideas has me hopeful because I was starting to just pull my hair lol.

SamT
06-11-2018, 04:13 PM
Generally, I let users enter Dates in any format they want, as long as IsDate recognizes it. All Dates in UserForm Controls and TextFrames are really just Strings of text anyway. I only convert the date Text to a Date Type as Code or Record keeping require. I only reformat Dates when Business Rules require. Ex: A DB Field requires Dates to be 6 characters.
I notice that you're using three 'TextBoxes' to handle the Dates; One for Date Entry, One for Date Storage, and one for Format Warning.

I suggest just the TextBox1 for both entry and display, a Variable to hold the actual Date, and an InValidDate Warning object of your choice from whatever PP offers. In MS Forms, I use a Label Control as the warning because they take the least memory. The only thing my warning says is

Please enter a valid Date in the format of 4 July, 76.

Click me to continue.
Note that I don't hold them to that format, they can use any format they choose, as long as IsDate recognizes it. My code for this looks something similar to

Dim ThatDate As Date

Private Sub txbxDate_Change()
If IsDate(txbxDate Then
ThatDate = CDate(txbxDate)
If ThatDate is in the Valid Range then
Exit Sub
Else
ThatDate = Null
.txbxDate = ""
ShowDateWarning
End If
Else
.txbxDate = ""
ShowDateWarning
End If

Private Sub ShowDateWarning()
With lblDateWarning
'Position over txbxDate
.top = 123
.Left = 456
'lblDateWarning is not visible at run time.
.Visible = True
End With
End Sub

Private Sub lblDateWarning_click()
lblDateWarning.Visible = False
txbxDate.SetFocus
End Sub

Private Sub UpdateDBField()
Access.SomeDB.SomeDate = Format(ThatDate, "ddmmyy")
End Sub



Please note that the above is NOT real code.

John Wilson
06-12-2018, 04:40 AM
It would be a lot easier to use an ActivX textbox at least for the input.

I'm guessing you are using an InputBox??

See if this does it (only one textbox needed)


Sub insertdate()
Dim osld As Slide
Dim strDate As String
Set osld = SlideShowWindows(1).View.Slide
Do
strDate = InputBox("Enter Date")
' TextBox 3 must exist!
If Not IsDate(strDate) Then osld.Shapes("TextBox 3").TextFrame.TextRange = "Invalid Date"
Loop While Not IsDate(strDate)
osld.Shapes("TextBox 3").TextFrame.TextRange = Format(strDate, "dd mmm yy")
End Sub