How to enable/disable buttons in excel at once based on criteria? - vba

I have an excel file with full command buttons, I want to disable some buttons with one click.
I'll appreciate your help.
Thanks

With the details you provided in your question, I would say that what you look for is:
By default, all the buttons are saved with ButtonX.Enabled = False (some of them might default with the Enabled = True property if they are meant to be available to all the users);
When the user inputs username and password in the form, you might want to run a code like this:
If userNameBox.Text = "Ahmad" And passwordBox.Text = "1234" Then
Button1.Enabled = True
Button3.Enabled = True
Button5.Enabled = True
'and so on with the buttons you want to activate
End If
Of course, this should be the code of the event-macro related to the submission of the Username/Password user form, let's say:
Private Sub StartUserFormButton_Click()
'code here
End Sub

Related

How do I change the _Click() event on a MS Access Form?

I am hoping to be able to streamline my UI. I want to have a set of command buttons change the _On_Click() event based on a user selection. For example:
Main topic selections: cmd1:"Membership Reports", cmd2: "Administration Reports", cmd3: "Other Reports - TBD"
If the user selects cmd1 then the subtopic buttons properties change to allow the user to open reports in that category.
Sub Topic Selections: cmd4: "All Members", cmd5: "Active Members", etc.
If the user selects cmd2: then the on_Click event would change to open reports in the "Administration Reports" group.
Thanks in advance for your help.
I would use 3 main Toggle Buttons and put hem into an Option group frame (Let's call it FrameMain). Set the Option Value for the buttons as 1,2,3. Create as many regular buttons a you have sub topics (let's call them cmd1_1, cmd1_2, cmd2_1....) and set theirs property Visible to False and Tag to Sub. Now create event FrameMain_AfterUpdate:
Private Sub FrameMain_AfterUpdate()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Sub" Then
ctl.Visible = False
End If
Next
Select Case Me.FrameMain
Case 1
cmd1_1.Visible = True
cmd1_2.Visible = True
Case 2
cmd2_1.Visible = True
cmd2_2.Visible = True
Case 3
cmd3_1.Visible = True
cmd3_2.Visible = True
End Select
End Sub
You can create now On_Click() event for all your sub-buttons to open the report you want.
You can also use a Switch board (search the Internet how to create it).

How to enable the textbox content after enteringthe password for the textbox?

I am trying to fire the event GotFocus() on the text box Txttravel which is disabled. I want that in case we want to edit this textbox- we have to enter the password and then the textbox is enabled and we can edit it. Help to achieve it. I tried below code. But it is not trigerring when the textbox is in disabled state.
Private Sub Txttravel_GotFocus()
Dim Password As String
Password = InputBox("Enter password to edit this field:", "Password")
If Password = "Cloudbu#hcl" Then
Txttravel.Enabled = True
Me.Range("A1").Select
End If
Txttravel.Enabled = False
End Sub
First, a control can get the focus only if it's visible and enabled as said by Microsoft here.
Alternative 1 : Selecting another control
As an alternative I recommend you just select another control in your form if the password is wrong and not disabling your textbox.
Try changing your code to this
Private Sub Txttravel_Enter()
Dim Password As String
Password = InputBox("Enter password to edit this field:", "Password")
If Password = "Cloudbu#hcl" Then
Txttravel.SetFocus
Else
'Select another control in your form here
TextBox2.SetFocus
End If
End Sub
Alternative 2 proposed by #KostasK : Use of the Locked property
As proposed by KostasK in the comments, you can lock the textbox.
Private Sub Txttravel_Enter()
Dim Password As String
Password = InputBox("Enter password to edit this field:", "Password")
If Password = "Cloudbu#hcl" Then
Txttravel.Locked = False
Else
Txttravel.Locked = True
End If
End Sub
Note: Make sure to place this code in your form with: Right clic on your form > View Code

Making a form that users enter a value into text boxes a certain number of times

I have a series of text boxes that are hidden, except one which users enter a value into. After a button is pressed, this text box disappears and the rest appear. From there, I want users to be able to enter different values into each text box. These values will be saved as variables. From there, users press a button and enter new values into the text boxes, until values have been entered into the text boxes for each number up to the first value entered into the hidden text box.
I'm not sure how else to explain this, but please let me know if you have any questions.
Correct me if im wrong. i think you want an input in different textbox?
Like textbox1 = 1, textbox2 = 2 and so on. If this is right then try this.
Lets try this to your 3 textbox for an example.
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
If TextBox1.Visible = True Then
If Trim(TextBox1.Text) <> "" Then
TextBox1.Visible = False
TextBox2.visible = True
Else
MsgBox("Required")
'or user error provider.
End If
Exit Sub
End If
If textbox2.visible = True Then
If Trim(TextBox2.Text) <> "" Then
TextBox2.Visible = False
TextBox3.visible = True
Else
MsgBox("Required")
'or user error provider.
End If
Exit Sub
End If
'and so on
End Sub
hope this is what you want. just basic but maybe usefull :)
This is the workflow of what you want to accomplish in JavaScript. VB.Net has similar functions to Visibility. https://msdn.microsoft.com/en-us/library/ms748821(v=vs.85).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
In the visual editor you can create buttons and add Click events to them, this is basic so I won't tell you how to do it. Google if you can't.
var options = document.getElementsByClassName("options");
var optionsSetOne = document.getByClassName('optionsSetOne');
This will select all your options. Now that you have all your options selected and in a variable called options you can play around with the visibility, as the user progresses thought the buttons.
First I would set all my options to hidden with:
options.style.visibility = 'hidden';
After the users progresses and clicks the button, I will make the first set of the options visible. With:
$('#buttonOne').click(function(){
optionsSetOne.style.visibility = 'visible';
});
This is the basic workflow of what you want to accomplish, you have to figure out how to save all the information the user gives to you.

VBA code when refering a SubReport from a Main Report in Access

I am setting an instruction (VBA) in On Load Event on a report in MS Access.
When I open the report,the code works perfect.
But when i try to embeded the report as a subreport on a main report, the code doesnt work.
I think the problem is that I should be referering the field different(Me.Service1...), since I am trying to call the field now from a main report, but i havent found the right syntaxis.
This is the code i wanna embeed on my main report:
Private Sub Report_Load()
If Me.Service1 = "Scanmachine" Then
Me.Vessel.Visible = True
Me.Label400.Visible = True
Else
Me.Vessel.Visible = False
Me.Label400.Visible = False
End If
End Sub
Any suggestions?
Indeed, you need to change the relative reference of your sub report controls. Over the course of my Access database development work, I have used this Access MVPs resource, even bookmarked the webpage (though it uses forms, the same naming setup applies to reports).
Consider the following, adjusting names accordingly and run this on the main report's OnOpen() event:
Private Sub Report_Load()
If Me![subreportcontrolname].Report!Service1 = "Scanmachine" Then
Me![subreportcontrolname].Report!Vessel.Visible = True
Me![subreportcontrolname].Report!Label400.Visible = True
Else
Me![subreportcontrolname].Report!Vessel.Visible = False
Me![subreportcontrolname].Report!Label400.Visible = False
End If
End Sub

MS-Access hidding and showing a button conditionally through vba

I am developing a quality control system for my company, and I i want to connect it to a label printer, which is already taken care off, the problem now is with the button itself.
I want the print label button to only be enabled and visible after the whole check was made, what i've got now is this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Button_label.Visible = False
Button_label.Enabled = False
End Sub
Private Sub Motor_OK_Change()
Dim ok As Boolean
ok = Motor_OK.Value
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
End Sub
It does work on conceiling the button, but it fails when trying to enable it again and making it visible upon making the check. It's important to refer that I have tried using If ok = True instead of If ok= 1, I dont know how important that is.
Thanks
You are not testing for both conditions (ok=0), perhaps the following will work:
Replace:
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
With:
Button_label.Visible = ok
Button_label.Enabled = ok
New Solution:
Instead of using the Form Before_Update event, use Form_Current event. If there is only 1 record, you can also use the Form_Load event. I would also suggest that you remove the Visible property and use only the Enabled one so users can see their is print functionality.
Private Sub Form_Current()
'button_Label.Visible = False
button_Label.Enabled = False
End Sub
Instead of using the Motor_OK Before_Updateevent, use After_Update event
Private Sub Motor_OK_AfterUpdate()
Dim ok As Boolean ' defaults to False
If IsNumeric(Motor_OK.Value) Then ok = True ' remove this statement if Motor_OM is
' alpha numeric.
If Not IsNull(Motor_OK.Value) Then ok = True ' otherwise following statement
' to avoid NULL error
'button_Label.Visible = ok
button_Label.Enabled = ok
End Sub
Let me know if I can be of further help.