+ Reply to Thread
Results 1 to 16 of 16

trying to change a txtbx in module

Hybrid View

  1. #1
    Registered User
    Join Date
    09-24-2003
    Posts
    62

    trying to change a txtbx in module

    I have a project with many forms and textboxes. I am trying to create a module that will add a "." (Chr45) in the value.

    this is the code on the userform. the textbox name changes throughout the project

    Private Sub TextBox4_Exit(ByVal Cancel As msforms.ReturnBoolean)
    
    Run TxtBx_Change
    
    end sub
    this is what i have in the module. I suspect it has to do with knowing the actual name of the textbox?
    the code below is not working, could anyone help?

    Private Sub TxtBx_Change()
    Dim CtlTxt As TextBox
    Dim Str1 As String
    Dim Str2 As String
    
    Str1 = Mid(CtlTxt.Value, 1, 2)
    Str2 = Mid(CtlTxt.Value, 3, 4)
    
    If CtlTxt.TextLength = 4 Then
        CtlTxt.Value = Str1 & Chr(45) & Str2
    End If
    
    End Sub
    it is a basic code i am just starting to work with. I will be expanding the code as soon as i figure out this simple task
    Last edited by Zygoid; 12-30-2008 at 06:28 AM.

  2. #2
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Hi

    One option is to pass in a parameter to your sub which tells the TxtBx_Change routine which textbox to run code against - this will have the added benefit of making the routine reusable for each textbox:

    Private Sub TextBox4_Exit(ByVal Cancel As msforms.ReturnBoolean)
    
    TxtBx_Change Me.TextBox4   'dont use Run
    
    end sub
    
    Private Sub TxtBx_Change(ctrl As Control)
    'Dim CtlTxt As TextBox === now unnecessary
    Dim Str1 As String
    Dim Str2 As String
    
    Str1 = Mid(Ctrl.Value, 1, 2)
    Str2 = Mid(Ctrl.Value, 3, 4)
    
    If Ctrl.TextLength = 4 Then
        Ctrl.Value = Str1 & Chr(45) & Str2
    End If
    
    End Sub
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  3. #3
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    thanks for the reply richard.

    I understand what ur saying and will change.

    was hoping to be able to identify the control that called the module.

  4. #4
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    i am getting an error on that line in the event

    "sub or function not defined"

  5. #5
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Quote Originally Posted by Zygoid View Post
    i am getting an error on that line in the event

    "sub or function not defined"
    Where have you defined the TxtBx_Change sub? If it is in a separate module to the Userform then you need to remove the Private keyword from the start of the sub name.

  6. #6
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    the userform and module are in the same project. is that what you mean?

  7. #7
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Quote Originally Posted by Zygoid View Post
    the userform and module are in the same project. is that what you mean?
    If they are in separate modules (and it sounds like they are) then you need to remove the Private keyword from the Sub name (just of TxtBx_Change, not the TextBox4_Exit routine).

  8. #8
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    sorry about the second post, i had to sign out and when i came back, did a search for my name and the string wasn't listed. I did not realize i had to use advance search.

    They are in seperate modules and i removed "private" from the txtbx_change.

    getting an error "wrong number of arguments or invalid propoerty assignment"

    TxtBx_Change Me.TextBox4

  9. #9
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Can you post the full code you are using. Also, if in a standard module, can you change the sub declaration to:

    Sub TxtBx_Change(ctrl As MSForms.Control)

  10. #10
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    right now, what i have above is all the code I have done. I was testing to see if it was possible to create a standard module (Txtbx_change) that could be call from a userform exit event (textbox4_exit) and the standard module identify the userform control that called it.

    The userform has 20 textboxes with exit events i was hoping to write the code once in a standard module and call it, rather than copy/paste the code in each userform exit event and edit it to match the textbox.

  11. #11
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    the 20 textboxes are where users input numbers with decimals (chr(45)) but they always don't put the deimals in the right place. I am coding in the decimals automatically, which i can do under each exit event.

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Zygoid,

    Place this macro in a standard module and then add calls to each of the TextBox_Exit events.

    Main Macro
    Sub CheckDecimal(ByRef TxtBox As MSForms.TextBox)
    
      Num = TxtBox.Value
      
      If Len(Num) = 4 Then
        TxtBox.Value = Left(Num, 2) & "." & Right(Num, 2)
      End If
      
    End Sub
    TextBox Exit Code
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        CheckDecimal ActiveControl
    End Sub
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 12-28-2008 at 03:11 PM.

  13. #13
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    You could alternatively just hook into another event to check all the text box values at the same time? Eg if the user subsequently needs to hit a command button to submit the form's contents then you could use the button's click event to execute a loop which runs thru all the textboxes on the userform and checks & amends the values as appropriate.

  14. #14
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    I am still getting error "wrong number of arguments.." with
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    TxtBx_Change ActiveControl
    
    End Sub

  15. #15
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Please could you post the current sub header of the procedure TxtBx_Change.

    Richard

  16. #16
    Registered User
    Join Date
    09-24-2003
    Posts
    62
    Ahhh, your abosolutely right Richard!! I did not have the sub header correct.

    "(crtl As MSForms.Control)" was my typo

    Works fine now. Thanks alot!!

    I don't feel particularly bright right now

+ 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