PDA

View Full Version : VBA userforms, control is command button is visible



francozola25
08-27-2008, 01:34 PM
Hi

I am using a few VBA userforms for excel. I was wondering is there a way that say if a have a textbox1 in Userform1 empty then display a command button on Userform2 OK, unload Me

If however the textbox1 is not empty then a command button will display on Userform2 say PrintDoc

Bob Phillips
08-27-2008, 01:54 PM
Me.cmdOK.Visible = Userform1.TextBox1.Text = ""
Me.cmdPrintDoc.Visible = Userform1.TextBox1.Text <> ""

francozola25
08-27-2008, 01:58 PM
Many Thanks for that.

francozola25
08-28-2008, 06:25 AM
I have tried the following


With UserForm2
If UserForm1.lstDirectoryFiles.Text = "" Then
UserForm2.CommandButton1.Visible = True
UserForm2.CommandButton2.Visible = False
Else
If UserForm1.lstDirectoryFiles.Text <> "" Then
UserForm2.CommandButton1.Visible = False
UserForm2.CommandButton2.Visible = True
End If
End If


i am calling this within a module. It doesn't seem to do anything, just displays CommandButton1 even if lstDirectoryFiles is left emty or not???

mikerickson
08-28-2008, 06:37 AM
Perhaps, in UserForm1
Private Sub TextBox1_Change()
With Me.TextBox1
UserForm2.CommandButton1.Caption = IIf(.Text = vbNullString, "Unload", "Print")
End Sub
in userform2
Private Sub CommandButton1_Click()
With Me.CommandButton1
If .Caption = "Print" Then
Rem print code
Else
Unload Me
End If
End With
End Sub

francozola25
08-28-2008, 07:16 AM
Thanks for that

The caption has changed but the Unload Me doesn't seem to work for okay.

Also can i call a function from the print.

francozola25
08-28-2008, 07:37 AM
Sorry mikerickson

I am trying to have something like this but i am wondering would i need CommandButton1_Click()


Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'Unload Me

With Me.CommandButton1()
If .Caption = "Print" Then
Dim strURL As String
strURL = strDirectory & UserForm1.lstDirectoryFiles & "." & strFileType
Call ShellExecute(0&, vbNullString, strURL, vbNullString, vbNullString, 0) 'Change 0 to vbNormalFocus to view.
PrintURL = ShellExecute(0&, "print", strURL, vbNullString, vbNullString, 0) 'Change 0 to vbNormalFocus to view.
Else
Unload Me
End If
End With

mikerickson
08-28-2008, 08:31 PM
I work on a Mac so the library function you are declaring is outside my experience.

But, Functions return values, they don't do things like print or unload. That routine looks more like a Sub than a Function.

The CommandButton1_Click() event routine is needed to control when the sub is run. Without it, how would the user tell Excel to print or unload?