Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:01:41+00:00 2026-06-12T17:01:41+00:00

I have written three different Private Sub functions, all of which do the same

  • 0

I have written three different Private Sub functions, all of which do the same thing Private Sub txt_noRooms_KeyPress and Private Sub txt_length_KeyPress ensure that the user can only type in values 1 to 9 into the referenced text fields whereas Private Sub txt_studentNo_KeyPress allows the user to type in values 0 to 9 into the referenced text field.

Is there any way I can merge these three functions and have them still support the referenced cells and maintain the same conditions? I want to do this for the purpose of making the code more efficient.

Private Sub txt_noRooms_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Restrict entry to txt_noRooms
Select Case KeyAscii
    Case Asc("1") To Asc("9")
    Case Asc("-")
        If InStr(1, Me.txt_noRooms.Text, "-") > 0 Or Me.txt_noRooms.SelStart > 0 Then
            KeyAscii = 0
        End If
    Case Asc(".")
        If InStr(1, Me.txt_noRooms.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select
End Sub


Private Sub txt_length_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)   
' Restrict entry to txt_length
Select Case KeyAscii
    Case Asc("1") To Asc("9")
    Case Asc("-")
        If InStr(1, Me.txt_length.Text, "-") > 0 Or Me.txt_length.SelStart > 0 Then
            KeyAscii = 0
        End If
    Case Asc(".")
        If InStr(1, Me.txt_length.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select
End Sub


Private Sub txt_studentNo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' Restrict entry to txt_studentNo
Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Asc("-")
        If InStr(1, Me.txt_studentNo.Text, "-") > 0 Or Me.txt_studentNo.SelStart > 0 Then
            KeyAscii = 0
        End If
    Case Asc(".")
        If InStr(1, Me.txt_studentNo.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:01:42+00:00Added an answer on June 12, 2026 at 5:01 pm

    You can set one private function and call it from each keypress:

      Private Sub txt_noRooms_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    ' Restrict entry to txt_noRooms
    If bclearfield(CLng(KeyAscii), Me.txt_noRooms.Text, Me.txt_noRooms.selStart,"1")  Then KeyAscii = 0
    End Sub
    
    
    Private Sub txt_length_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    ' Restrict entry to txt_length
    If bclearfield(CLng(KeyAscii), Me.txt_length.Text, Me.txt_length.selStart,"1") Then KeyAscii = 0
    End Sub
    
    Private Sub txt_studentNo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    ' Restrict entry to txt_studentNo
    If bclearfield(CLng(KeyAscii), Me.txt_studentNo.Text, Me.txt_studentNo.selStart,"0") Then KeyAscii = 0
    End Sub
    
    Private Function bClearField(KeyAscii As Long, sText As String, lSelStart As Long,sLowLimit as string) As boolean
    
    bClearField=false
    Select Case KeyAscii
        Case Asc(sLowLimit ) To Asc("9")
        Case Asc("-")
            If InStr(1, sText, "-") > 0 Or lSelStart > 0 Then
                bClearField=true
            End If
        Case Asc(".")
            If InStr(1, sText, ".") > 0 Then
                bClearField=true
            End If
        Case Else
            bClearField=true
    End Select
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three different classes written in python. They contain several methods which I
I have written the following method that prevents all keys from being pressed: private
I have three arrays (it's written in C but that is not important, it
I have a file download script that I have written, which reads files from
I'm refactoring a bunch of classes, written by different people, which do not have
Below are three methods I have written for my RichTextBox derived class, which is
I have written this code inside a servlet to delete certain records from three
I have three projects written with VB.NET (2005) and have to convert them to
I have an application written in pure/basic Java without GUI. I have three classes
I have a three tier system, SQL Server backend, hand written data access layer,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.