Parsing textbox names as a variable in a sub

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pipeme
    New Member
    • Aug 2008
    • 6

    #1

    Parsing textbox names as a variable in a sub

    Hi

    I got a grid of textboxes which all need to do the same function on their doubleclick event, however the code they call needs to copy the data in the textbox to another textbox.

    I don't really want to repeat the code all 32 times so am looking to create a standard sub function which can be called. That's easy enough, i am just unaware of how to make this function parse a variable depending on which textbox was clicked.

    Here is what the code looks like as a private sub (sorry if it is bad coding, I haven't done this in YEARS)

    Code:
    [B]'*** check if the txtbox is null
        If IsNull(txtGrid1.Value) Then
        '*** Depending on which temp field has data, copy it to the txtbox
            If IsNull(txtStudent1temp) Then
                txtGrid1 = txtStudent2temp
                txtStudent2temp = Null
            Else
                txtGrid1 = txtStudent1temp
                txtStudent1temp.Value = Null
            End If
            txtGrid1.BackColor = 16777215
        Else
        '*** if the txtbox is not null, copy it's data to the temp field
            If IsNull(txtStudent1temp) Then
                txtStudent1temp = txtGrid1
                txtGrid1 = Null
            Else
                txtStudent2temp = Null
                txtStudent2temp = txtGrid1
                txtGrid1 = Null
            End If
            txtGrid1.BackColor = 13434828
        End If[/B]

    Now I want to move this code into a public sub function BUT make the txtGrid1 value change depending which txtbox in the grid is double clicked.
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    pass the textbox in the parameters for the function.

    [code=vba]
    public sub SubroutinesName (byref txt as textbox)
    'your code in your post goes here
    'except you use txt instead of TextGrid1
    end sub
    [/code]

    now all you need is to put the function call into the doubleclick event for each control and pass the appropriate textgrid# control

    Comment

    • pipeme
      New Member
      • Aug 2008
      • 6

      #3
      Originally posted by Delerna
      pass the textbox in the parameters for the function.

      [code=vba]
      public sub SubroutinesName (byref txt as textbox)
      'your code in your post goes here
      'except you use txt instead of TextGrid1
      end sub
      [/code]

      now all you need is to put the function call into the doubleclick event for each control and pass the appropriate textgrid# control

      Thanks, that worked a treat!

      Comment

      Working...