Results 1 to 12 of 12

Thread: VBA PowerPoint TextBox Validation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    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

  2. #2
    Lively Member
    Join Date
    Feb 2010
    Posts
    85

    Re: VBA PowerPoint TextBox Validation

    Code:
    If instr(Slide15.TextBox1.Value,",") Then
        MsgBox "Invalid Character Entered. You Cannot Enter , Value.", vbOKOnly, "ERROR"
    End If

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    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
    Last edited by Khadafi; Mar 21st, 2010 at 07:24 AM.

  5. #5
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    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

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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
    Last edited by Siddharth Rout; Mar 22nd, 2010 at 03:49 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    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
    Last edited by Khadafi; Mar 23rd, 2010 at 01:53 PM.

  9. #9
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    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

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    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.

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VBA PowerPoint TextBox Validation

    Can you upload your PPT?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Posts
    76

    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
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width