Allowing to type anything in TextBox except for:

  • Hello friends,


    I am wondering is it possible to have a text box (e.g. TextBox1) in a word document that would allow any input except for:
    1. Blank spaces.
    2. NA.


    And can the macro prevent the user from saving or closing the document before filling the textbox? (by showing a popup box telling which textbox needs to be filled).


    Thank you for your time and efforts.

  • Re: Allowing to type anything in TextBox except for:


    Don't know if this helps:

  • Re: Allowing to type anything in TextBox except for:


    Nice, thanks can you also include not allowing the user to enter "NA". And I don't mean NA in a word like "NATURAL".


    Thank you for your time,
    Nawaf

  • Re: Allowing to type anything in TextBox except for:


    Thank you all for your kind responses =)

  • Re: Allowing to type anything in TextBox except for:


    I must admit, I am still stuck. I did try to add your code to the code in the attachment but I failed.


    And for some reason, I cannot have spaces between words in the textbox that I created.


    Your help is highly appreciated
    Nawaf

  • Re: Allowing to type anything in TextBox except for:


    This will check for various entries. Include a command button to save the document. When a user clicks on the save button the various checks will be made and if everything is ok it will save the document otherwise it will prompt the user accordingly. Hope this is of some help.


  • Re: Allowing to type anything in TextBox except for:


    Thank you so much for all your responses. It works!
    Only one thing it is a little slow in responding when typing, why is that?

  • Re: Allowing to type anything in TextBox except for:


    Not sure why it's running slow for you - just checked it on my machine using your attachment but with the revised code and a Save command buttton and it seems to respond exactly as it should.

  • Re: Allowing to type anything in TextBox except for:


    Ok, I see the problem now. You've inserted the code into the Textbox1_Change() procedure. What will happen is that everytime you enter or change text in the box the file will be saved.


    To prevent this, create a command button either below or to the side of your text box and put the code in the cmdSave_Click() procedure and remove the Textbox1_Change() procedure. Now the save operation will only happen when your user has entered data correctly and clicked on the Save button.


    I've updated your file and attached it so you can hopefully see what I mean

  • Re: Allowing to type anything in TextBox except for:


    Would it be possible without the button?
    Again thanks for your time.

  • Re: Allowing to type anything in TextBox except for:


    Nawaf,
    I'm assuming that you want to save the document via the normal File | Save menu system rather than having a button on the document page? If so then I think the only way to check the textbox is to do it every time a character is entered, which would mean that you couldn't check for NA or include spaces in the text. Rather than include a text box in the document have you considered using VBA's UserForms to allow your user to enter data which you can then validate. If you're not familiar with using VBA in this way let me know and I'll put something together for you.


    john

  • Re: Allowing to type anything in TextBox except for:


    John I am sorry I was away on a short leave.
    Yes I would really appreciate your help. I am improving my skills in VBA but I not there yet.


    Please note, I do plan to have several more boxes, I only used one to make my question understood.


    Thank you sir.

  • Re: Allowing to type anything in TextBox except for:


    Hi Nawaf,
    Hope you enjoyed your leave.


    I'll do what I can to help but before I start can you give me more of an idea of what you are trying to achieve. If possible upload a copy of how you want your document to look and let me know roughly what information you need your users to input and where you want the data inserting etc.
    John

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!