generate link based on field values in access - ms-access

We have an archive of reports that are automatically saved and named according to a test number (A) and request number (B), resulting in a directory similar to C:\archivefolder\B_A. I am creating a table in access that includes fields for A and B and I would like to add a third field that contains a hyperlink to the related report. My plan is to automatically generate the link by providing the first portion of the directory (C:\arvhivefolder) and then concatenate the remaining portion based on what is entered into the other two fields (A and B).
What I've done so far creates the correct directory and copies it into the hyperlink field, but the link won't work. If I copy and paste the the link into another field, "##" is added to the end of the line (C:\archivefolder\B_A##). When I delete the "##", the link works.
Can anyone help me with this? As far as I can tell, I'm renaming the link, but not changing the address/ directory.
Here is what I've done so far:
To create the full directory, I created a field, named ResultsLink, with a calculated data type where the expression is:
"C:\archivefolder\" & [B] & "_" & [A] & ".pdf"
I don't believe there is a way to generate a link from this calculated field, so I created another field with a hyperlink data type and named it ResultsPDF. Then I created a macro to copy the value from the ResultsLink field into the ResultsPDF field (it's a click-button for now, will change later):
Me.ResultsPDF.Value = ResultsLink.Value
If Me.Dirty Then Me.Dirty = False
When I click the button, the hyperlink field is updated with what appears to be an active link, but goes nowhere.

Thanks to June7 for posting the links in the comments. I changed my macro to include the pound signs and it works perfectly now:
Me.ResultsPDF.Value = "#" & ResultsLink.Value & "#"
If Me.Dirty Then Me.Dirty = False

Related

Microsoft Access SearchForRecord macro action useless?

I have been trying to incorporate a built in macro action (SearchForRecord) in MS Access, however cannot get it to work for the life of me. There is minimal resources available online for this operation, and I've noticed that other people have struggled with the same issue.
I made a test database just to see if I could get it to work in the most basic form. I made a table with 3 columns (ID, Name, Colour) - I turned the table into a tabular form using the Form Wizard. I created a text box with a search button.
I then made a macro operation:
SearchForRecord
Object Type: Form
Object Name (Name of the Form) "frmNewSearch"
Record: First
Where Condition: ="txtIDSearch = '" & [Forms]![frmNewSearch]![txtSearchBox] & "'"
I took the where condition syntax directly from https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/searchforrecord-macro-action
I set the button click event to the Macro that I made.
In theory, I enter the ID into the txtSearchBox and it should bring up the appropriate record within the same frmNewSearch form.
When I try this, nothing happens and it just sits on the first record. I am using MS Access 2016 - is the macro action maybe just not supported in this version?
If there is another way at approaching this it would be much appreciated!
Cheers
Referenced article states:
Note the equal sign (=) at the beginning of the expression, and the
use of single quotation marks (') on either side of the text box
reference:
="Description = '" & Forms![frmCategories]![txtDescription] & "'"
Have to actually type = sign into argument. (Yes, I hear your ranting and cursing, but that's life.)
I presume txtIDSearch is name of textbox. The criteria must use name of field, which you say is ID. If ID is number type, don't use apostrophe delimiters (apostrophe delimiters are used for text fields, # delimiter for date/time, nothing for number type). So result will show like:
Where Condition: = ="ID = " & [Forms]![frmNewSearch]![txtSearchBox]
or since code and controls are on same form, simply:
Where Condition: = ="ID = " & [txtSearchBox]
However, both fail if form is a subform. This is because form is not open independently in Forms collection. A reference incorporating parent form name fails as well. Use VBA code methods.

opening a file VBA from path data in query

I have a query in my MS ACCESS database that yields:
event (in this case, a goanna has been photographed)
photo name (e.g., IMG_0002.JPG)
path to file (e.g., c:\documents\random_place)
In the form I have built, I added a button, and built this event:
Private Sub Command17_Click()
Application.FollowHyperlink "C:\Documents\random_place\IMG_0002.JPG"
End Sub
No problem.
But what I really want is to populate the path to the file with information returned by the query (as opposed to having typed it in manually), since button I have there is static, and now all the events show photo IMG_0002.JPG, where in fact different records have different file names and even paths.
I guess I am looking for something like:
Application.FollowHyperlink paste{result from path},{result from filename}
if you see what I mean...
Assuming the query you mention is the form's RecordSource, code can reference fields/controls to build the file path\name in the button click event.
FollowHyperlink Me![path to file] & "\" & Me![photo name]

Creating a link to a file on a form but file address is stored on another table

I have made a form that is used as an inter-department sign-off sheet for controlled documents on the network. The database already includes a table with a hyperlink field for each document's location. The table also has a text field of the document id. My form is storing its information in a different table, but I am hoping that I can still use the hyperlinks from the first table instead of creating new links in the second table.
My google searching lead me to this forum post that showcased a single line using FollowHyperlink and DLookup:
Application.FollowHyperlink DLookup("Document_ID", "Documents", "Document_ID = '" & Me.DocumentID.Value & "'")
Document_ID is the field that holds the hyperlink.
Me.DocumentID is the textbox that the user types in and the code runs when this field is clicked.
I have tried multiple variations of the code including wrapping table fields in brackets "[]", using either the hyperlink field or the text field for the criteria, using Like instead of = (along with asterisks around Me.DocumentID). All of which result in a run-time error 2471:
The expression you entered as a query parameter produced this error:
'Document_ID'
Which makes me think that Dlookup doesn't like hyperlink fields as I can pull other fields just fine. What am I missing? Or is there a better way to reference hyperlinks in a different table?
Thanks to HansUp for pointing me in the right direction. Turns out that "Document_ID" didn't exist in the table. Instead the hyperlink field had a space between the underscore and ID ("Document_ ID") and I didn't catch it until copying the name from the table into vba where spaces are more pronounced.
The Dlookup would not work when using the hyperlink field in the criteria so the text field with the document id was used instead and changed my code to the following as .FollowHyperlink wouldn't take the whole field value (title#address)
Dim LinkText As String
LinkText = DLookup("[Document_ ID]", "Documents", "[DocID] = '" & Me.DocumentID & "'")
LinkText = Application.HyperlinkPart(LinkText, acAddress)
Application.FollowHyperlink LinkText
The control will now open the documents that exist (now to work on code to use for when the documents don't exist)

Subform field hyperlinks to open record in another form

I'm trying to replicate a feature in the Access 2007 "Issues" template/example database. When you open up the 'Issues List' form, and click on an ID, it behaves like a hyperlink and opens up that record in another form.
How can I replicate this? I'm not a big fan of using the Access 'create macro' feature and would prefer to use the VBA editor if possible.
Thanks in advance for your help.
I didn't examine that template database, but your description sounds like something you can handle with DoCmd.OpenForm.
Say your form includes a text box named txtID which is bound to a numeric field, ID, in the form's record source. Create a VBA procedure for the text box's click event to pass the current ID value as the OpenForm WhereCondition parameter. (This assumes the record source of the next form also includes that numeric ID field.)
Private Sub txtID_Click()
Const cstrForm As String = "YourNextFormName" ' <-- change this
DoCmd.OpenForm cstrForm, WhereCondition:="[ID]=" & Me.txtID
End Sub
If the data type of the ID field is text rather than numeric, include quotes around the value in the WhereCondition.
WhereCondition:="[ID]='" & Me.txtID & "'"
What you want to do is first set the text field you're interested to a hyperlink. You do this by setting Display As Hyperlink in the Format tab of the Property Sheet or you can do this via vba as following (although it seems to be bugged visually i.e. clickable link but not displayed as such)
myTextBox.DisplayAsHyperlink = acDisplayAsHyperlinkOnScreenOnly
After you do that, create a Click event. In the subroutine you can run the code to open your form filtered with the record number (or appropriate argument)
DoCmd.OpenForm "myFormName", acNormal, , "[ID]=" & Nz([Id], 0)

VBA to open MyComputer at a specific folder

Ive just inherited an MS Access 2003 system and need a bit of VBA to put behind a command button to open MyComputer at a specific folder to view the files there.
The folder name will come from a field on a form.
I did have
Application.FollowHyperLink "C:\" & Me![ref] (ref is in the format abcd-1234)
when its hard-coded it works fine, but i cant seem to get it to open when picking the foldername up from the form.
any hints? (other than binning access!)
thanks
See what the text looks like as you're submitting it to FollowHyperlink. You can insert a line like this in your code:
MsgBox "C:\" & Me![ref]
Perhaps it's not what you expect. It's always good to check.
What happens when it doesn't work. Do you see any error messages or any other symptoms which could help us nail this down?
My first thought was spaces in a folder name might create problems. But I don't think that's the answer because FollowHyperlink works fine for me in this example:
Application.FollowHyperlink "C:\Access\spaces in name\"
So the best I can offer is to see what you're asking FollowHyperlink to use. If that effort doesn't lead you to the answer, add a specific example which fails to your question.
This code always work for me:
Dim filePath = <"Insert the path of the directory to open inside of opening and closing parenthesis">
Application.FollowHyperlink filePath, vbNormalFocus
Normally, I store a few directories in a table inside the DBMS, which helps when linking hundreds of images to a database instead of embedding. For instance, I have a table called, "dbLocations." Inside this table, there are only two fields: 1) picLocation 2) Description.
The field picLocation has the value of the network path, i.e, C:\My Documents or G:\Whatever Directory or \\groups1\for UNC paths.
The field Description is what it implies, a description of the picLocation.
I use tables to store directory locations because they linking to files (.jpg, .png) stored on a network drive. As time evolves, directories can get changed (I move a folder to another location, or if the UNC changes, etc.).
If you hard code the location(s) over several subs or modules, you will need to change each one; which is very inefficient. So, in order to save time and a lot of headaches, I use the Domain Lookup function which allows me to only change the file location just once and in an easy to find place, namely, the dbLocations table.
In essence, I am looking up the value of the location inside the table where the picLocation matches the description of, Alert Pics. (I am creating a database that will be used to track Trespassers and other vagrant persons for work)
Dim filePath as String
filePath = DLookup("picLocation", "dbLocations", "[Description] = 'Alert Pics'")
Application.FollowHyperlink filePath, vbNormalFocus
With these three lines of simple code, you can navigate to a specific directory.