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

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).

Related

Show/Hide FileExplorer in Access Form

I've been trying to use a combobox to show/hide a PDF viewer that I've added into a MS Access form.
When I use the form_current event, then the form only updates when I move between the data entries. When I use the afterupdate event, the same code does nothing at all.
Does anyone have a fix? The code I have used is below, which I have tried both the AfterUpdate event for the Browser and the Form_Current event for the whole form
Private Sub PDFT900_AfterUpdate() / Private Sub Form_Current()
Dim ESNComb As String
ESNComb = Me.ESNCombo.Column(1)
If ESNComb Like "9????" Then
Me.PDFT900.Visible = True
Else
Me.PDFT900.Visible = False
End If
End Sub
In the code below, I'm hiding and showing the Adobe PDF Reader ActiveX control named, "AcroPDF0". Since the Like operator returns true on an expression match and false on a mismatch or no match, it serves as a simple boolean switch for the visible property. I've used the (*) wild card instead of (?). It works {shrug}. See demonstration images below.
Private Sub ESNCombo_AfterUpdate()
'AcroPDF0.Visible = ESNCombo.Text Like "P*"
AcroPDF0.Visible = ESNCombo.Column(0) Like "P*"
AcroPDF0.src = acroPDFOSrc
End Sub
ComboBox List Items
"File Browser" Selected in ComboBox
Toggled ComboBox back to "PDFT900"

DocumentBeforePrint does not fire for Envelope print for VBA in Word

I am trying to switch users to use a different printer for envelopes in Word. If they create the envelope, then print it, it works great using DocumentBeforePrint. However, this event is NOT fired when using the Print button on the dialog Mailings --> Envelopes. Is there any event fired when this happens that I can catch?
thanks,
Mike
There is no event, as such, however...
It is possible to Display, Execute or Show Word's built-in dialog boxes. A number of the controls in these dialog boxes are exposed so that they can be set or read. And the button used to dismiss the dialog box returns a value that can be evaluated.
The list of exposed controls is documented here. The WdWordDialog enumerator for envelopes is wdDialogToolsCreateEnvelope. The properties listed are for both envelopes and labels, keep that in mind when sorting through the possibilities. Note that there is no IntelliSense for these properties. (For .NET people reading this, the properties are accessed via late-binding, meaning C# must use PInvoke in order to work with them.)
To read the user's input, place the properties after the method; to make "default setting", place the properties before the method.
Dismissing this dialog box returns the following values:
0 Cancel (or the "X" button)
1 Print
2 Add to Document:
Since you need to do something before the print job is sent, you probably need to use Display rather than Show. Display does not execute the dialog box when the user dismisses it. Instead, it's necessary to capture the settings, do something with them, then Execute the the dialog box.
For example, the following code displays the dialog box to the user, capture's the delivery address typed into that box, then handles the various return values.
Sub PrintEnvelopes()
Dim dlg As Word.Dialog
Dim retVal As Long
Dim recipAddress As String
Set dlg = Application.Dialogs(wdDialogToolsCreateEnvelope)
With dlg
retVal = .Display
recipAddress = .envaddress
End With
Select Case retVal
Case 1 'Print
With dlg
'Change the printer here
.envaddress = recipAddress
.Execute
End With
Case 0 'Cancel
Case 2 'Add to document
With dlg
.envaddress = recipAddress
.Execute
End With
End Select
End Sub
Turns out, there are events you can place in a module to intercept the Envelope tool launch (h/t http://www.gmayor.com/fax_from_word.htm). As such, I added the following to one of my modules, and it runs when Mailings-->Envelopes is selected, so I can switch the printer, load the dialog, then switch the printer back after the dialog is finished:
Sub ToolsCreateEnvelope()
Dim DoChangePrinter As Boolean
Dim OriginalPrinterName As String
DoChangePrinter = False
OriginalPrinterName = Application.ActivePrinter
CurrentPrinterName = OriginalPrinterName
//Change to use color if on B&W printer
If InStr(1, LCase(CurrentPrinterName), "b&w") Then
CurrentPrinterName = Replace(CurrentPrinterName, "B&W", "COLOR")
DoChangePrinter = True
End If
If (DoChangePrinter) Then ChangePrinter
Application.ActiveDocument.Envelope.DefaultOmitReturnAddress = True
//Show dialog
Dim oDlg As Dialog
Set oDlg = Dialogs(wdDialogToolsCreateEnvelope)
With oDlg 'Pop up the envelopes dialog
.extractaddress = True
.Show
End With
ActivePrinter = OriginalPrinterName 'Restore the original printer
End Sub

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.

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

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

Opening new form after right click on selected record in continuous form

In my access database before I was using a list box in the form for having list of items and I had below code for opening new form after right click on each selected item in list box.
Private Sub ItemList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
If Button = RIGHTBUTTON Then
DoCmd.OpenForm "frmShortcut_GenerateTask"
DoCmd.MoveSize udtPos.X * mp.TwipsPerPixelX, udtPos.Y * mp.TwipsPerPixelY
End If
End Sub
Now I am using a continuous form instead of list box and I have defined a [isselected) field for selecting each record in continuous form after clicking on that. Now my problem is how I have to write code for right clicking and opening new form.
I used the same code I had used for list box, but it does not work and nothing happened.
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
If Button = RIGHTBUTTON Then
DoCmd.OpenForm "frmShortcut_GenerateTask"
DoCmd.MoveSize udtPos.X * mp.TwipsPerPixelX, udtPos.Y * mp.TwipsPerPixelY
End If
End Sub
Private Sub P_Click()
On Error Resume Next
Me.IsSelected = Not Me.IsSelected
' Save the status
Me.Dirty = False
' Force conditional highlighting
P_ForceHighLight
' Update display in SF_Selected
Me.Parent("SF_Selected").Requery
ActiveControl.SelLength = 0
On Error GoTo 0
End Sub
I recommend either using a DoubleClick event in all of your textboxes and combos, or else putting a small button at one edge of your continuous form that allows the user to open records. Right-click is going to give you problems (so will labels and image controls) because this event doesn't cause (or ensure) that the Form's internal Recordset Bookmark property is actually moved to the record they right-clicked on. In my experience, only buttons, textboxes, and comboboxes will move the Bookmark to the record the user is trying to select.
To Test this, try putting code at the top of your different routines that shows you what record is selected:
MsgBox "RecordID = " & Me!RecordIDField
If you are consistently getting the correct ID, then your problem has to do with how your opening your new form, passing parameters or arguments, etc.