Results 1 to 5 of 5

Thread: Command Buttons - RESOLVED

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    Resolved Command Buttons - RESOLVED

    I have 2 command buttons on one form lets call this form1. On form2 I have a drive, directory, and file listbox. Lets say I click on command button1, it opens form2 and allows me to select the drive, directory, and file. I want it to write back to form1 in a text box. If I click command button2 I want it to do the same thing but write back to form1 to a different text box. What I am asking is how to determine which button is clicked on form1 and carry that over to form2?

    Thanks.
    Last edited by odamsr; Jun 1st, 2005 at 05:16 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Command Buttons

    Quote Originally Posted by odamsr
    I have 2 command buttons on one form lets call this form1. On form2 I have a drive, directory, and file listbox. Lets say I click on command button1, it opens form2 and allows me to select the drive, directory, and file. I want it to write back to form1 in a text box.
    VB Code:
    1. 'from the Filelist box on Form2
    2. Private Sub File1_Click()
    3. Form1.Text1.Text = File1.Filename
    4. End Sub
    Quote Originally Posted by odamsr
    If I click command button2 I want it to do the same thing but write back to form1 to a different text box.
    VB Code:
    1. 'from Form1
    2. Private Sub Command2_Click()
    3. Text2.Text = Form2.File1.Filename
    4. End Sub
    Note: Make sure a file is selected before clicking on Command2.

    Is this what you wanted?

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Command Buttons

    Instead of keeping track of which button was clicked maybe you should keep track of which text box that you want to be updated.

    I assume that Form2 has an OK button and that you want to show it modally. Now let's say that Command1 on Form1 will update Text1 and Command2 will update Text2. Then add the following code to Form2:
    VB Code:
    1. Private mtxtBox As TextBox
    2.  
    3. Public Sub ShowMe(txtBox As TextBox)
    4.     Set mtxtBox = txtBox
    5.     Me.Show vbModal
    6. End Sub
    7.  
    8. Private Sub cmdOK_Click()
    9.     'update the text box.
    10.     mtxtBox.Text = Drive1.Drive & Dir1.Dir & "\" & File1.File 'or whatever
    11.     Unload Me
    12. End Sub
    In the CommandX_Click of Form1 use code simular to this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Call Form2.ShowMe(Text1)
    3. End Sub
    4.  
    5. Private Sub Command2_Click()
    6.     Call Form2.ShowMe(Text2)
    7. End Sub

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Command Buttons

    You may set some flag (boolean, string, integer, ...) on Form1 and check it while on Form2:
    VB Code:
    1. 'Form1
    2. Option Explicit
    3.  
    4. Public strButton As String
    5.  
    6. Private Sub Command1_Click()
    7.     '...
    8.     strButton = "Button1"
    9.     '...
    10. End Sub
    11.  
    12. 'Form2
    13.  
    14. 'somewhere in the code:
    15. If Form1.strButton = "Button1" Then ...

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    Re: Command Buttons

    Thanks everyone. I used your code, Joacim and it worked like a charm. Thanks to everyone.

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