+ Reply to Thread
Results 1 to 4 of 4

Difficulty restricting characters

  1. #1
    Registered User
    Join Date
    06-20-2006
    Posts
    22

    Difficulty restricting characters

    I've used the following code to try and restrict certain characters to a textbox:

    Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc(".")
    If InStr(1, Me.TextBox1.Text, ".") > 0 Then
    KeyAscii = 0
    End If
    End Select
    End Sub

    The period is not the only character I'm trying to restrict but it is one of them. Whenever I've tried to use 'Select Case', the code never seems to do anything... at all. The Textbox2 is used as the savename so I can't allow any weird characters to get typed in.

    Can anyone help me out regarding this?

    Thanks,

    Kartune85

  2. #2
    Franz Verga
    Guest

    Re: Difficulty restricting characters

    "kartune85" <[email protected]> ha
    scritto nel messaggio
    news:[email protected]...
    >
    > I've used the following code to try and restrict certain characters to a
    > textbox:
    >
    > Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    > Select Case KeyAscii
    > Case Asc(".")
    > If InStr(1, Me.TextBox1.Text, ".") > 0 Then
    > KeyAscii = 0
    > End If
    > End Select
    > End Sub
    >
    > The period is not the only character I'm trying to restrict but it is
    > one of them. Whenever I've tried to use 'Select Case', the code never
    > seems to do anything... at all. The Textbox2 is used as the savename so
    > I can't allow any weird characters to get typed in.
    >
    > Can anyone help me out regarding this?
    >


    Hi Kartune,

    Instead of a text box to input the filename, you could try to use the Excel
    Save As window:

    Application.Dialogs(xlDialogSaveAs).Show


    --
    Hope I helped you.

    Thanks in advance for your feedback.

    Ciao

    Franz Verga from Italy



  3. #3
    Registered User
    Join Date
    06-20-2006
    Posts
    22

    Found a long way round

    With the SaveAs dialog, I still can't restrict certain characters. I've made the form for end-users so I have to try and make it easy to use and idi0t-proof.

    I ended up using:

    If InStr(1,TextBox2.Text,".") > 0 then
    MsgBox "Invalid characters, please re-enter."
    cancel = true
    Else...

    The only problem is that I repeated InStr(...) for each individual character and added them up in a variable. (SearchStr = InStr(...".") + InStr(..."/")...)

    Thanks anyway, but if anyone knows an easier way, I'm open for suggestions.

    Kartune85

  4. #4
    NickHK
    Guest

    Re: Difficulty restricting characters

    I'd go with Franz's suggestion; let the OS decide if the filename is valid
    or not.

    On Error resume next
    WB.SaveAs "C:hhhh/\\:::tt.txt.txt.xls"
    If Err.number>0 then
    'deal with the problem

    By the way a file name of MyExcelFile.txt.xls is a valid name

    NickHK

    "kartune85" <[email protected]> wrote
    in message news:[email protected]...
    >
    > With the SaveAs dialog, I still can't restrict certain characters. I've
    > made the form for end-users so I have to try and make it easy to use
    > and idi0t-proof.
    >
    > I ended up using:
    >
    > If InStr(1,TextBox2.Text,".") > 0 then
    > MsgBox "Invalid characters, please re-enter."
    > cancel = true
    > Else...
    >
    > The only problem is that I repeated InStr(...) for each individual
    > character and added them up in a variable. (SearchStr = InStr(...".") +
    > InStr(..."/")...)
    >
    > Thanks anyway, but if anyone knows an easier way, I'm open for
    > suggestions.
    >
    > Kartune85
    >
    >
    > --
    > kartune85
    > ------------------------------------------------------------------------
    > kartune85's Profile:

    http://www.excelforum.com/member.php...o&userid=35586
    > View this thread: http://www.excelforum.com/showthread...hreadid=569743
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1