PDA

View Full Version : Solved: Deactivate Commandbutton and Hide



Learner123
12-08-2010, 11:45 AM
Hi,

I have created a network of userforms but have run into a problem.

I am trying to have a commandbutton deactivate and hide after a certain point in the process.

I have uploaded a sample and would appreciate any help.

The problem occurs after userform4 is loaded and "next" is pressed. At this point, Userform1 reloads, but I was hoping to have "Frame1", along with the Commandbutton within it, deactivate and hide. I would like to only have the remaining "Options" visible.

Can someone please help? : pray2: : pray2: : pray2:

Simon Lloyd
12-08-2010, 02:53 PM
I've not looked at your example but you can set a public variable in a standard module (not a sub!) by putting this at the very top Public hb As integerIn your userform4 command button_click set hb = 1, in your userform_initialize for userform1 use :If hb = 1 then
Me.Commandbutton1.visible = false
End If and in the userform1 commandbutton_click event use hb = 0 so that your sure of seeing the button next time.

I'll leave you to work the rest out for the frame :)

Learner123
12-09-2010, 08:19 AM
Simon,

I am very greatful for your input but am confused on how to apply it. I am new to VBA and have been mingling with it for about a month or two. I know that I may be asking for too much, but is it possible to apply the suggested updates to the attachement and then have it uploaded. I will be able to make better sense of your posting by seeing it applied. It will teach me a whole lot.

Thank again for all your help! :bow:

Simon Lloyd
12-09-2010, 09:04 AM
I've checked your example and my instructions are very clear for your ability, the only thing you may not have come across is userform intialize so add this to userform1 code modulePrivate Sub userform_initialize()
If hb = 1 Then
Me.CommandButton1.Visible = False
End If
End Subjudging by your other creations and use of code you can manage the rest :)

Learner123
12-09-2010, 10:03 AM
I think your giving me more credit than I deserve. :doh:

I am trying to set hb=1 in userform 4 command button_click and have come up with the below code:


Private Sub CommandButton1_Click()
Dim hb As Integer
Set hb = 1
Next
Me.Hide
Load UserForm1
UserForm1.Show
Unload Me
End Sub

Is this right? I am getting an "object required" error message.

Simon Lloyd
12-09-2010, 11:00 AM
Thats because you didnt follow my instruction about putting the public variable in a standard module, but up to there its correct.

EDIT: you also need to get rid of NEXT

Learner123
12-09-2010, 12:26 PM
Sorry for being a pest, but the below code was moved from UserForm1 module and placed in a standard module, however issue/error still persists.:banghead:

Public hb As Integer

"Next" was also removed.

Am I missing something?

Simon Lloyd
12-10-2010, 12:25 AM
Can you tell me how you created the standard module. where it exists and how you placed that line of code?

Learner123
12-10-2010, 07:28 AM
I added the code to Module9 under the Modules folder within VBE. The code was placed at the very top (right under option explicit).

Also, I cross posted this thread on another site but experienced somewhat of a scolding. :boohoo(http://www.ozgrid.com/forum/showthread.php?t=148816). The administrators were not interested in posting back onto this site. However, they did include some additional code, which has been included in the attached.

I am still trying to learn where I am going wrong with this code. My managers are leaning on me heavily to figure this out.

I truly appreciate your help!

Simon Lloyd
12-10-2010, 08:53 AM
Userform1Option Explicit
Private Sub CommandButton1_Click()
Me.Hide
Load UserForm3
UserForm3.Show
hb = 0
Unload Me
End Sub
Private Sub UserForm_Activate()
Dim lng As Long
For lng = 1 To UserForm2.lngFrames
Me.Controls("Frame" & lng).Visible = True
Next
End Sub
Private Sub userform_initialize()
If hb = 1 Then
Me.CommandButton1.Visible = False
End If
End Sub
Although if you find that it's not hiding the button its because the userform isn't being closed so you would need to move If hb = 1 Then
Me.CommandButton1.Visible = False
End If
directly under Sub of userformactivate and delete userform initialize.

Userform4Option Explicit
Private Sub CommandButton1_Click()
hb = 1
Me.Hide
Load UserForm1
UserForm1.Show
Unload Me
End Sub
Private Sub UserForm_Activate()
Dim mng As Long
For mng = 1 To UserForm3.mngFrames * 2
Me.Controls("Frame" & mng).Visible = True
Next
End SubThose should allow you to perform your request in this thread (although from what is written here you should be able to work out how to hide the frame too).

Learner123
12-10-2010, 09:16 AM
You are awesome! Thank you!!!!! :hi: