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.
Related
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]
I have an Access table where each item has attached a Visio file (.vsd).
In my Access form, I would like to see the file. I don't care if it is an editable Visio file, a preview or just an image.
I have built a VBA code that let me load the Visio file from a Directory. But I need to load the file from a table.
Here my VBA code.
Private Sub Carica_Dati()
Dim path As String
path = "C:\Users\VisioFlow_001.vsd"
With Me.VisioObject ' name of the OLE Object where I want to put the Visio file
.Class = "Visio.Drawing.11"
.OLETypeAllowed = acOLELinked
.SourceDoc = path ' HERE I WANT TO LOAD THE FILE FROM A TABLE OF THE DB
.Enabled = True
.Locked = False
.Action = acOLECreateLink
.SizeMode = acOLESizeZoom
End With
End Sub
Here a preview of the form.
UPDATE
Here a picture to show how the file is attached to the table.
Since attachment fields in Access aren't very consistent, directly loading them into an OLE object is not an option, unless you're willing to do sophisticated things
Microsofts documentation on attachments can be found here
My observations on attachments: the binary data field contains one of the following:
Some characters I can't identify + the file type + the file data appended to it
Some characters I can't identify + the file type + a compressed version of the file data appended to it
Microsoft, in all it's wisdom, has supplied us with a way to save the original file to the disk, but hasn't supplied us with a way to remove those initial characters and the file type from the actual file data, or an easy way to identify if the file is compressed or not (you can check the file type with the table supplied in the link to check if it should be).
In conclusion, you're probably off best either replacing your attachment field with an OLE object in the database, or writing the attachment files to disk before displaying them.
If you use an OLE object field, and load them in as long binary data (not through the GUI), you can easily achieve the behaviour you seek without writing the file to disk, since the binary data is available without any extra characters.
To write an attachment file to disk:
Dim rsForm As DAO.Recordset2
Dim rsFiles As DAO.Recordset2
Set rsForm = Me.Recordset
Set rsFiles = rsForm.Fields("attachment_column").Value
If Not rsFiles.EOF Then
Dim fileLocation As String
fileLocation = Environ("TEMP") & rsFiles.Fields("FileName").Value
rsFiles.Fields("FileData").SaveToFile fileLocation
'Your existing code to display the OLE object here
End If
You do not want to use the Attachment feature. Its purpose is different than what you are attempting.
Put the images into their own stand alone folder outside of the database.
In the table that holds the records for your main form - you need a new field which holds the path & image file name. This is a text field. (If the path segment is uniform for all one can insert that elsewhere via code rather than store it in this field.)
Then in form design - use the image control. This control (all controls) have a source property - that will change with each record using that field that holds the path & file name.
Do a bing/google on the topic of changing an image with every record - the set up isn't intuitive necessarily. Note that older editions did things differently so be sure you get relatively recent advice.
Then when you are using the form and change records - the image will change.
Note after having typed all this.... I have no idea if the visio file type works - I know that jpg and bmp do... so first sanity check a simple fixed image with that file type to see if it works ...
My incomplete file path is stored in Me!txtFilePath. It contains a complete path to the folder's location and the first 9 out of ~30 characters of the folder name. Those first 9 characters are guaranteed to be unique. Currently I have a button that executes the code below when clicked:
Shell "explorer """ & Me!txtFilePath & "", vbNormalFocus
How would I make sure it opens the right folder given that I don't have the last several characters of the folder name? Thanks!
edit: I am trying to open a network folder that I don't have a "drive" for.
I'm not sure why exactly this solution is the one that works for me whereas the suggestions did not, but I found the answer. Most of it was solved by the commenters to my original post - thanks to all of you.
Basically, I had to set a string variable equal to the Dir value. Dir only returns the ending portion of the folder name, not the entire path, so when opening the file I needed to combine it with the known beginning of the path name. I also had to use Application.FollowHyperlink instead of Shell. I don't know why and I don't know if there are any drawbacks. Maybe Shell explorer only works for files, not folders.
Private Sub btnJobFile_Click()
Dim strStartFilePath As String
Dim strEndFilePath As String
strStartFilePath = "\\JobFolders" & Me!txtFilePath
strEndFilePath = Dir(strStartFilePath & Me.JobNum & "*", vbDirectory)
Application.FollowHyperlink strStartFilePath & strEndFilePath
End Sub
I'm playing around with splitting access databases. It appears that each table contains a hard link to the backend file in the linked table manager.
But if I wanted to send the file pair to someone through email to look at, the hard links will break. Right now we're at separate offices just testing changes to the program. This isn't in production. I don't want them to get into the development mode and edit the linked table manager because it would be too hard.
Is there a way to tell access to simply look in the front end file's directory for the backend file? Is there a way to force a prompt on the front end to let them choose the location of the backend file?
Thanks!
The simple solution is to on startup check if the table link(s) point to the current directory, and if not, then you run re-link code. That way the pair of files will work if you move the pair to a different folder or re-name the folder.
The above is a common setup and EVEN recommend for single user applications that SHOULD be split and benefit by being split.
So on startup, check the path of a linked table. I use the following code to return the path of currently linked tables.
Function strBackEndPath() As String
' returns the path name to the back end
' and includes tralinig \
Dim mytables As TableDef
Dim strTempBack As String
Dim strFullPath As String
strFullPath = ""
For Each mytables In CurrentDb.TableDefs
If Left(mytables.Connect, 10) = ";DATABASE=" Then
strFullPath = Mid(mytables.Connect, 11)
Exit For
End If
Next mytables
strBackEndPath = Left(strFullPath, InStrRev(strFullPath, "\"))
End Function
With above, then on startup I can go:
If CurrentProject.path & "\" <> strBackEndPath Then
' call re-link code
End If
And there are TONS of re-link code examples floating around but here is a link to some re-linking code.
http://access.mvps.org/access/tables/tbl0009.htm
Thus if the links don’t point to the back end database in the same folder, then re-linking will ONLY occur once and after that only the above “test” to ensure that front end and back end are are linked.
If the folder is renamed or as noted a user moves the files to a different location then again the re-link will occur.
Right-click on the front-end tables and select Linked Table Manager. From there, you can browse for the location of the back-end tables
I'm using access 2007 and this behaviour can be replicated as follows.
1) Create new access database accdb file.
2) Open database and create new vba module.
3) Create 1st subroutine sub1:
Sub sub1()
Msgbox Err.Description
End Sub
4) Create 2nd subroutine sub2:
Sub sub2(Description as String)
Msgbox Description
End Sub
At this point everything is normal.
5) But if I go and change sub2 so that 'Description' reads 'description', i.e. change 'D' to 'd' like so:
Sub sub2(description as String)
Msgbox description
End Sub
This also has a knock-on effect and changes sub1 too! So that sub1 now reads:
Sub sub1()
Msgbox Err.description
End Sub
Why has 'Err.Description' changed to 'Err.description' ?
This behaviour seems to have no effect on the actual functionality of the code, so no problem there. The big issue I have is that I'm exporting my vba modules as text files and putting them under SVN control. And just recently a whole load of pointless 'changes' have been committed to the repository because of this.
Any ideas on how to stop this from happening?
Sorry. That is a hard-coded "feature" of VBA. See similar question here: How does one restore default case to a variable in VBA (Excel 2010)?
The way I've worked around that with source control is to run my repository through a script that does the following:
Revert all modified files with vba code extensions (creating backup .orig files)
Do a case-insensitive compare of the .orig files to their counterparts
If there are no changes (outside of case changes) delete the .orig file
For the remaining .orig files (the ones with actual changes) delete the counterpart file and remove the .orig extension
What this does is effectively hide files where the only changes are to the case (a constant problem when working with VBA files, as you're experiencing). It does not hide case changes in a file that has had other modifications done to it. It's far from a perfect solution but it's the best I've come up with.
Additionally remember that in VBA, variable names are not case sensitive. So Description and description are the same within the same scope.