Results 1 to 3 of 3

Thread: Help use the same function on multiple command buttons

  1. #1
    Guest

    Question

    Seemingly simple. I have a form with 10 Command buttons, they all are used to Browse for a dir path. Each button returns the choosen dir to it's associated text box. So this is what I want, rather than copy the code for each button(10) I'd like to call a Public Function from each button, therfore only writing the code once.

    Here is my problem. How/What variable do I pass to the function, and how do I get it to return to the proper textbox? Have I confused you? Here is my Code for the Browse Button:

    Option Explicit

    Private Type BrowseInfo
    pIDLRoot As Long
    pszDisplayName As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
    End Type
    Const BIF_RETURNONLYFSDIRS = 1
    Const MAX_PATH = 260
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

    Public Function Browse()

    On Error GoTo Err_Browse

    Dim lpIDList As Long
    Dim udtBI As BrowseInfo
    Dim iNull As Integer
    Dim sPath As String

    lpIDList = SHBrowseForFolder(udtBI)

    If lpIDList Then
    sPath = String$(MAX_PATH, 0)
    'Get the path from the IDList
    SHGetPathFromIDList lpIDList, sPath
    'free the block of memory
    CoTaskMemFree lpIDList
    iNull = InStr(sPath, vbNullChar)
    If iNull Then
    sPath = Left$(sPath, iNull - 1)
    End If
    End If

    Me.txtDir1 = sPath 'This is where I need the help

    Exit_Browse:
    Exit Sub

    Err_Browse:
    MsgBox Err.Description
    Resume Exit_Browse

    End Sub

    Here I would call the Function

    Private Sub Browse1_Click()
    Browse
    End Sub

    The Browse Buttons a labled Browse1-10
    The Text Boxes are labled txtDir1-10

    The code is marked with this caption where I belive I need the help
    'This is where I need the help

    I know I need to pass variables but how?

    TIA

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    From the sound of it you would be best off making a control array of buttons and a corresponding control array of text boxes. To make a control array, try to copy and paste a control (ie a textbox, label, button, listbox, you get the idea) and it should ask you if you want to create a control array. Click yes and keep pasting as many controls as you need.

    The benefit of this is two-fold: Firstly you can refer to each element in a control array by controlname(index) where index is an integer which corresponds to the index property of one of the controls. This means you can use the index for the button that gets clicked as the index for the textbox it applies to. Secondly, all controls in a control array share their code, so you only have to write the code once into the cmdButton_click event and it will use that code for all of the buttons.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh but if for some reason you don't want to use control arrays, then to answer your question simply pass in a reference to an object of type TextBox:

    Code:
    Private Sub cmdButton_click
    
    Dim theBoxToBePassed As TextBox
    Set theBoxToBePassed = Text1
    DoStuff theBoxToBePassed
    
    End Sub
    
    Private Sub DoStuff(theBox As TextBox)
    
    'here you can refer to the textbox like this:
    theBox.Text = "That command button affects this text box"
    
    End Sub
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

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



Click Here to Expand Forum to Full Width