VBA PowerPoint TextBox Validation
Hi, i have a text box on a slide, where a user enters a sentance, i would like to prevent the user from entering a , value into.
So far i am using the following code:
Code:
If Slide15.TextBox1.Value = "," Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
End If
This displays an error if only a , value is entered, but i need an error to be displayed if the user enters a comma value as part of a sentance, for example "Hi, thank you".
Also, i have code running after this, which takes the user to the next slide, but i only want this part of the code to be run if there is no , value in the textbox.
Any help would be much appreciated.
Thank You
Re: VBA PowerPoint TextBox Validation
Code:
If instr(Slide15.TextBox1.Value,",") Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
End If
Re: VBA PowerPoint TextBox Validation
Hi, thank you that works great.
Now, if there is a , value typed in, i want the code to stop there and not carry on to the bit where the user is taken to the next slide, so that the user can remove the , value from the text box.
Code:
If InStr(Slide15.TextBox1.Value, ",") Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
End If
With Application
.Presentations(1).SlideShowSettings.Run
With SlideShowWindows(1).View
.GotoSlide 16
End With
End With
End Sub
How would i do that.
Thank You.
Re: VBA PowerPoint TextBox Validation
Hi, i figured out how to do it by adding Exit Sub
Code:
If InStr(Slide15.TextBox1.Value, ",") Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
Exit Sub
End If
Is there any way in which i could Set the Focus onto the text box where a comma has been typed in, so if the user has typed in a comma in TextBox1 the user will be returned to TextBox1. Also would it be able to highlight the text in the textbox.
Thanks
Re: VBA PowerPoint TextBox Validation
I believe you have access to the textBox events if the textbox is in on a slide (I'm on a mac currently, so I can't check this right now). If so, you can put this code into the KeyPress event for TextBox1, it will prevent them from typing in a comma during entry:
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 44 Then
Beep
KeyAscii = 0
End If
End Sub
Re: VBA PowerPoint TextBox Validation
Hi, no that is not working, thanks for the suggestion. i cannot access the text box events on the slide.
This works perfectly:
Code:
If InStr(Slide15.TextBox1.Value, ",") Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
Exit Sub
End If
I just need the text box with the , to be the one activated, and the text to be highlighted, so that the user can make changes and then proceed.
Thanks
Re: VBA PowerPoint TextBox Validation
How have you added the textbox? If it is via Menu View~~Toolbars~~>control toolbox then what mark suggested will work or an alternative mentioned below will also work... But then you have to be in Slide Show mode to type in the textbox...
Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
If KeyCode = 188 Then KeyCode = 0
End Sub
However ifyou are adding a textbox from the Drawings toolbar then you can use the code mentioned in your last post or modify the below which will also work...
Code:
Sub Sample()
If InStr(1, ActivePresentation.Slides(15).Shapes("Text Box 1").TextFrame.TextRange, ",") Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
Exit Sub
End If
'~~> your code here to run if no comma
End Sub
To highlight the text see this slight modification...
Code:
Sub Sample()
If InStr(1, ActivePresentation.Slides(15).Shapes("Text Box 1").TextFrame.TextRange, ",") Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
'~~> Highlight the text...
ActivePresentation.Slides(15).Shapes("Text Box 1").TextFrame.TextRange.Select
Exit Sub
End If
'~~> your code here to run if no comma
End Sub
Re: VBA PowerPoint TextBox Validation
Hi, i tried
Code:
ActivePresentation.Slides(15).Shapes("TextBox1").TextFrame.TextRange.Select
i get an error saying, TextFrame (unknown member): Invalid request. This type of shape cannot have a TextRange.
Thanks
Re: VBA PowerPoint TextBox Validation
It looks like the KeyPress event doesn't fire for a textbox on a slide. Other events do, including KeyDown, so this should work for you:
Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 188 Then
Beep
KeyCode = 0
End If
End Sub
Re: VBA PowerPoint TextBox Validation
Hi, that bit is fine, as i am using
Code:
If InStr(Slide15.TextBox1.Value, ",") Then
MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
Exit Sub
End If
i just want the text to be highlighted in the TextBox, so that the user knows easily which text box the , is in and then can make changes.
Thank You.
Re: VBA PowerPoint TextBox Validation
1 Attachment(s)
Re: VBA PowerPoint TextBox Validation
Hi, i am unable to upload my original presentation, but i have created an example presentation which demonstrates what i want to do.
I have uploaded it.
Thanks