Label Hyperlinks on MS Access 2013 Reports - ms-access

I am having troubles with being able to click hyperlinks on reports in MS Access 2013. My database serves the function of a project log which tracks what projects our group is working on. The hyperlinks serve as an attachment to a summary file (usually PPT) for each project. Each project is a record in my backend table.
So far I have:
Stored the hyperlink as a string in a database table
Pull/edit/add hyperlink to a record via Access forms
Show the hyperlink and click on it via Access forms
Add the hyperlink on a report by referencing a hidden textbox which houses the path
The problem is that the hyperlink shows up on the report correctly and sometimes I am able to click on it, but the majority of the time I cannot click on it. If I scroll around the report, sometimes I can get the hyperlink active but it seems hit or miss.
Here is the code I used to apply the hyperlink on the report:
Private Sub Detail_Paint()
Dim strSource As String
If Report_rptCompleted.txtHL.Value <> "" Then
strSource = Report_rptCompleted.txtHL.Value
Report_rptCompleted.lblHL.Caption = Right(strSource, Len(strSource) - InStrRev(strSource, "\"))
Report_rptCompleted.lblHL.HyperlinkAddress = strSource
Report_rptCompleted.lblHL.ForeColor = vbBlue
Else
Report_rptCompleted.lblHL.Caption = "No Attachment"
Report_rptCompleted.lblHL.HyperlinkAddress = ""
Report_rptCompleted.lblHL.ForeColor = vbBlack
End If
End Sub
Any help on resolving this issue would be greatly appreciated

I ended up having to go a different route and use a text box instead of a label.
My solution was to drop the label and OnPaint event entirely. I formatted the textbox to show the file name then referenced the file path in the OnClick event with a FollowHyperlink command.
It is a good bit slower opening the link but works fine.

Related

MS Access 365 - Create SubForm Containing Linked Files

I've created a table and form to track sales opportunities. I'd like users to be able to "connect" associated files on our server to each opportunity. For example, they might like the opportunity to point to price quote.
Since attaching files to the database is a debatable move, I've opt to save the path and would like to use FollowHyperlink to navigate and open the file.
My strategy is to create a subform containing the links associated with a particular opportunity. The user could then click on the subform nick name in the subform to open the associated file.
By surfing the web, I've managed to create a macro allowing the user to store the selected file and path in a column called LinkLocation , assign the entry a nick name via a InputBox, and store the nickname in a column called LinkName. This macro is working as expected.
Edit: Code shared.
Sub test()
Dim f As Object
Dim strSQL As String
Dim strShorthand As String ' Short hand name for display in subform.
Dim strFullFilePath As String ' Full file path
Set f = Application.FileDialog(3)
f.allowMultiSelect = False
f.Show
strFullFilePath = f.SelectedItems(1)
strShorthand = InputBox("Enter the shorthand name here.")
strSQL = "INSERT INTO tblLinks (LinkLocation, LinkName) Values ('" &
strFullFilePath & "','" & strShorthand & " ');"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End Sub
I am having two problems.
When I click to create a new record in the subform linked to tblLinks, my macro runs and the data is correctly stored. However, the newly created record does not show up in the subform.
How do you create a double click event so FollowHyperlink properly engages and opens the file in question?
I cannot find an answer to either of these questions on the web.
I've written a fair amount of complex VBA for Excel, but Access is completely new to me. Any resources you can recommend are most welcome.
Thanks in advance.
Enter values into bound controls on form instead of running INSERT action and issue will go away. Instead of InputBox for LinkName, just enter into textbox on form. If intent is to enter a new item, make sure focus is on new record row. Then code behind subform:
Me.LinkLocation = strFullFilePath
Otherwise, have to Requery or Refresh form to display new record. Again, if code is behind subform, simply: Me.Requery.
As for double click event, you already know how to create VBA code so do the same for this event using FollowHyperlink command. Can be double click of a textbox or single click of command button. Format either to look like clickable hyperlink. Set textbox as locked so user cannot accidentally edit.

Coding a label or text box in access on a report

What I am trying to accomplish: I have a report built that feeds from a button on a form and the info selected on that form. What I need is a code that will allow the report to change the header caption based on the information selected from the print report button on the form. So essentially on my form I have three fields that are drop downs. When I make my selection on the first two fields and I want a report that lists only that information I hit the button that says print report and it works just fine. What I want it to do is when I hit the print report button it will open the report with the a specific title based on what selections were made from the form. So for example on my form I choose the branch from the drop down then I choose an organization from the drop down. When I hit the print report button I want that report to open with the Title of the branch and the organization. I want it to be able to change each time the selections are changed. Essentially update the information from the form to the title or caption of the report. Any suggestions? I found a code I thought would work but I cannot make it work when I tweak the information. I am fairly new to the coding and access world and would appreciate any help.
Here is the code I was trying to use:I was trying to figure out how to add snipping photos I took but don't know how. Here is the code I was trying to use I solved the problem by passing the new text for the Label.Caption in the "OpenArgs" parameter in the OpenReport command, and used the Open Event for the Report to install the new Caption. It works great!
Code:
>>> In Control Form Module
Private Sub Command0_Click()
Dim aWhere As String
Dim aStrArg As String
'Select a WHERE statement (from Global List) based on the Radio Box Group
aWhere = aDept(Me.Dpt_Chain.Value)
'Pass a String Argument to the Report to install in the Label
aStrArg = "TEST 22"
'Open the Report and pass it a string ...
DoCmd.OpenReport "Price_1", acViewPreview, , aWhere, acWindowNormal, aStrArg
End Sub
>>> In Report Code - Form opens with "TEST 22" in the Label
Private Sub Report_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.Controls("Dpt_Label").Caption = Me.OpenArgs
End If
End Sub
I tried to tweak it to work with mine but can't. this is not my information for my stuff but I thought by tweaking this code I could make my caption/title box on my report change each time different information was selected in my form.
Tested, and this worked for me:
Dim strString As String
strString = "Hallllo"
DoCmd.OpenReport "Report1", acViewReport, , , , strString
Private Sub Report_Open(Cancel As Integer)
Me.Controls("Label1").Caption = Me.OpenArgs
End Sub

Opening multiples a report in more than one tab (Access Database)

I have an access database that has a report. There is a button I click to open it, and everything works fine if I just want one report. However, if I want to open the same report twice, say to compare the two, it does not work. Instead of opening the second report in a separate tab, it just takes me to the tab that has the first report on it. Is there a way that I can make the report always open in a new tab?
You can achieve this quite easily, with a little VBA. You do need to add a module to the report and to the form/report with the button:
On the buttons on click event:
Public Sub MyButton_Click()
Dim rpt As New [Report_Labels and Remarks]
rpt.Visible = True
Set rpt.Myself = rpt
End Sub
On the report, in the module:
Public Myself As Report
Private Sub Report_Unload(Cancel As Integer)
Set Myself = Nothing
End If
Note that you might need to rewrite other code, like macros closing the report, DoCmd.Close and code fetching values from the report using the Reports collection, since there are multiple instances of the report.

MS Access - Data Entered in a Form Automatically saves when i close the form

I've been tasked with making updates to an MS Access Database and its forms.
Each form seems to be linked to a query. If I enter data into a text box on the form and then close the form without pressing the Save Record button a new record is still added to the Database which makes no sense to.
Any insight would be great, I'm a programmer but have little experience working with Access Forms and Databases.
Thanks.
Microsoft Access binds forms to data by default, and will automatically save data as soon as you either move between records or close the form you're working on. For the average user, this is actually a good thing because it makes it very difficult to lose data, even if you accidentally close the form after making an edit.
If this functionality isn't what you're looking for, I'd suggest removing the binding from the form, that is, set the Record Source property to blank, then manipulate all the data in code using unbound controls. It's a lot more fiddly, but it gives you a lot more control.
The other option would be to use the form's BeforeUpdate event to ask the user if they want to save their changes before allowing them to go through.
If your main concern is accidentally adding new records, set the Allow Additions property to false, and create new records programmatically.
A sample of a BeforeUpdate event procedure (the same code would work for the BeforeInsert event):
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim intAns As Integer
intAns = MsgBox("Are you sure you want to save this record?", vbQuestion + vbYesNo, "Save Record")
If intAns = vbNo Then Cancel = 1
End Sub
If you were to use the above method, and the user clicked "No", they would have to then click the ESC key to undo the changes they had entered. Otherwise it would keep showing the dialogue when they moved off the record.

Is it possible to prevent MS Access from changing the selected ribbon tab automatically when using a custom ribbon?

When a custom UI XML file is used to add several custom ribbon tabs in Access, the selected ribbon tab changes back to the first custom tab whenever a form is closed.
We load a custom ribbon programatically from VBA. I've create an accdb that reproduces the problem. The folder also includes an XML file that contains the ribbon definition. It must be in the same directory as the .accdb file.
The problem can easily be demonstrated:
open the database RibbonTest.accdb,
switch to Tab2 and open Form2 using the button on the ribbon and
close Form2.
Notice that Tab1 is now active.
Of course, in this small example db this problem seems very minor. However, we have a very large project with many custom tabs, each containing numerous groups and buttons. Our users are finding it very frustrating indeed that they keep losing their place on the ribbon every time they close a form.
We have investigated a workaround where we programatically store the selected tab and restore it when we think we need to. However, it is proving difficult to do this reliably. (There isn't an Office API for automating the ribbon like this, but this article helped.)
Has anyone else encountered this problem? Have you found a way to prevent the tab from changing automatically?
Edit: It seems that this problem was introduced with a fix implemented in Office 2010 SP1 . (Sorry, no link: don't think I can have more than two.) The problem is not present in the RTM version. The fix list for SP1 includes this: "Access does not activate or return the user to the correct Ribbon tab for a previously opened database object when the user returns to that object." It seems that they've tried to fix use of the Form.RibbonName property (which supports contextual ribbons), but have broken the default ribbon in the process.
This one line fixes the issue:
<tab id="tabBogus" label="Bogus" visible="false"></tab>
Just make it your first tab in <tabs>. Big thanks to Scott's Potential Workaround answer!! (Tried to Vote it up and/or comment, but just signed up so not enough reputation.) This saved hours (or days) of work versus the other complicated workaround! Thanks!
Potential Workaround
Something I stumbled across that's been working for me is to hide the first tab in the XML using the visible tag. I haven't tested it much, but I have a copy of the standard Home tab that is hidden (no idea if it needs to be a populated tab or not). It appears to me that since Access can't actually activate the hidden tab when you close a form, it remains on the currently selected one.
I don't know if this was fixed in Access 2013 or not, but hopefully the info isn't too late to be of use to someone.
It's A Bug!
MS support has accepted a bug submission for this, and commented regarding Office 2010 SP1, "The change that was implemented allows us to track the active tab for each database object (forms, reports, etc) using the tab’s TCID so that as you move between objects the active tab is restored. However custom tabs all use the same TCID value, so with this change the active tab for custom tabs will always move to the first custom tab."
We hope that they will release a hotfix to resolve this in the future.
Workarounds
The following information has proved useful for us in creating a workaround.
See the answer from Johanness above regarding the IRibbonUI.ActivateTab method. This was introduced in Office 2010.
There is no Office API (AFAIK) for getting the currently selected tab. Therefore we use code from this article helpful. We
create an array when we generate our ribbon containing the id value of each ribbon tab,
handle Form_Deactivate and use it to start a timer in another hidden form and also store the index of the selected tab,
in the Timer_Tick handler in the hidden form we disable the timer and look up the id value of the tab whose index we stored in Form_Deactivate, and
activate the tab using IRibbonUI.ActivateTab.
This article shows an interesting use of IRibbonUI.Invalidate and the getVisible callback to select a particular tab.
There seems to be a way to get the selected tab (as you mentioned and probably already have the code for, and as you can find here)
In the RibbonCode module:
Save the ribbonObject to a module-variable:
in the xml change the first line:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onload="OnRibbonLoad" >
and add this:
Private MyRibbon as IRibbonUI
Private ActiveRibbonTab as string
Sub OnRibbonLoad(ribbon As IRibbonUI)
Set MyRibbon = ribbon
End Sub
Sub RememberRibbonTab
ActiveRibbonTab=<Do the IAccessibleMagic here>
End
Sub RecallActiveTab
If ActiveRibbonTab<>"" then MyRibbon.ActivateTab(ActiveRibbonTab)
ActiveRibbonTab=""
End
Now in every form add
Private Sub Form_Close()
Remember_RibbonTab
End Sub
Private Sub Form_GotFocus()
RecallActiveTab
End Sub
Create an enumerator variable in a general code module corresponding to your ribbon tab numbers (ALT+Y)
' Used By Send Keys to Select Correct Ribbon Tab.
Enum eRibTabs
DataEntry = 1
Reporting = 2
StockAndParts = 3
AdminFinance = 4
DataImport = 5
OtherAdmin = 6
Admin = 7
LocalSystem = 8
End Enum
Create a form in ms Access called "zFrmRibbonSelect" and put an unbound text box called txtTabValue and then put the following code in your new form. (Suggest set form mode to hidden.
Private Sub Form_Current()
' Select Correct Tab Menu Item
Me.txtTabValue = Me.OpenArgs
End Sub
Private Sub Form_Close()
'Select Correct Tab Menu Item
Dim varTab As Variant
varTab = Me.txtTabValue
SendKeys "%Y" & varTab
SendKeys "{ESC}"
SendKeys "{ESC}"
End Sub
Private Sub Form_Timer()
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Set form timer interval to 500 and check on event for timer shows in property box etc.
On your report put the following code: (use the enumerator value you want.)
Private Sub Report_Close()
'Select Correct Tab Menu Item
DoCmd.OpenForm "zFrmRibbonSelect", , , , , acHidden, eRibTabs.StockAndParts
End Sub
For Forms that close use the following code int the form
Private Sub Form_Close()
'Select Correct Tab Menu Item
SendKeys "%Y" & eRibTabs.StockAndParts
SendKeys "{ESC}"
SendKeys "{ESC}"
End Sub
Actually one approach that works well is to reduce or eliminate the tabs in the forms you open. If you specify the ribbon (other tab in properties sheet), then when you switch between forms the ribbon displayed switches for you automatic and without code.
Now while this is not really a solution to your problem, the idea and concept here is that if you have to start writing a bunch of code to engage and switch to what tab then this really becomes difficult as you point out.
As noted for Access/office 2010 you can use code to set the active tab (this feature is not available in Access/office 2007).
So about the only suggest I have here is trying and limit most forms to one tab when possible. The other tip is that while in the menus might be grouped by "type of task" and thus you might have a menu that cascades down to a whole bunch of reports. Now with the ribbon when working on a invoice, then you have:
Create invoice
Balance invoice
Post invoice
Print invoice (a report).
So all of the above options are different things but are group in one ribbon to allow you to get one job done.
So the idea here is to group ribbon options not by type of option such as all reports, but group + create a ribbon based on doing ONE task that includes the options that the user requires for given task at the given time.
As noted, the above may not be a solution to your issue(s), but avoiding additional tabs often will solve a lot of these issues.