opening a file VBA from path data in query - ms-access

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]

Related

Access VBA Macro to run pass through query

I have a pass through query built in Teradata set to export data to an Excel spreadsheet. I'm trying to automate it, but when I run the macro or open the query, a window pops up asking for the data source. I have an ODBC connection created and I'm thinking there has to be a way to make the macro pass the data source name so it will run without interaction.
Edit: Adding Macro as requested
Function AutoExec()
On Error GoTo AutoExec_Err
DoCmd.OutputTo acOutputQuery, "Performance Interval Data", "ExcelWorkbook(*.xlsx)", _
"filepath\filename.xlsx", False, "", , acExportQualityPrint
DoCmd.Quit acExit
AutoExec_Exit:
Exit Function
AutoExec_Err:
MsgBox Error$
Resume AutoExec_Exit
End Function
Couple of concerns, (can't validate any of this right now as I do not currently have access to Access for testing), but it looks like:
You're trying to OutputTo a query, to the best of my knowledge that
is not feasible.
Your file path is setup as filepath\filename.xlsx unless that is the actual location and name of your Excel sheet, something seems
wrong there to me.
I don't really think this macro relates to an ODBC of any sort in its current state.
But, you should at least start with fixing the filepath issue. That should be the full path to your Excel file and the full name of the file as well. (i.e. C:\TEMP\TestExcelSheet.xlsx)
All that being said, you may want to just go with something like this (although its a little difficult to tell if this is what you actually want or not):
'Export Excel file from Query
DoCmd.TransferSpreadsheet acExport, , "acOutputQuery", _
"C:\TEMP\TestExcelSheet.xlsx", True
NOTE: "acOutputQuery" should be the actual name of your passthrough query, "C:\TEMP\TestExcelSheet.xlsx" would be your destination path, and True adds the query's headers into the sheet, False to ignore the headers.

generate link based on field values in 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

In access 2007, when you split the database into two files, can you tell it to look in the frontend directory for the backend file?

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

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.

Microsoft office can't find the object "A_Z Schaltfläche"

I migrated a database from access 2003 to access 2007.
It seems to work but when I clicked the buttons from A-Z I'm getting this error message:
"Microsoft office can't find the object 'A_Z Schaltfläche'
If 'A_Z Schaltfläche is a new macro or macro group, make sure you have saved it and that you have typed it's name correctly"
I didn't make a new macro but I deleted a word in one of the tables which I think causes the problem: "Like [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium]"
I found it under the "Record Source" and under one field, that expression is written in the criteria field. If I don't delete this I'm getting a box which says: "Enter Parameter Value" Formulare!Frm_Fahrzeugdaten!Schaltflächenkriterium
My skills in VBA is not really so good, and I need some help how to correct the problem
Thanks for your help.
=======
additional info's:
When I open the VBA code under the Form_Fahrzeugen, here is what I saw:
Private Sub Auswahl_Click()
On Error GoTo Auswahl_Click_Err
' Programmablauf
Me.Filter = "[A_Fahrzeugtyp] like '*'"
Me.Namen_Filter.Value = 27
Me.Schaltflächenkriterium = "*"
Schaltflächenkriterium = "*"
Me.Requery
Me.lfd_Name.Requery
DoCmd.GoToRecord acDataForm, "Frm_Fahrzeugdaten", acGoTo, 1
Me.lfd_Name.Selected(1) = True
Me.A_Inventarnummer.SetFocus
GoTo Auswahl_Click_End
Auswahl_Click_Err:
Call ErrorHandler("Form_Frm_Fahrzeugdaten", "Auswahl_Click", Error$, Err, Erl)
Resume Next
Auswahl_Click_End:
'Ausführungen vor den verlassen der Routine
End Sub
Does it have something to do with the macro name? This is the macro name by the way: "A_Z Schaltfläche"
If I go to the design view of "A_Z Schaltfläche" this is what I got, the conditions are marked yellow I'm not really sure if this is a good sign though:
Thanks
==============
Updates about my problem:
I was able to find the solution of my problem. It was the version of my microsoft office which was causing it. The original database was written in German and when I did the migration, I migrated it to the english version of Access 2007. The reason why it can't find the object because of the name "Ereignisprozedur" in German and "Event procedure" in English. I changed it to Event Procedure because the error says:
"Microsoft Office Access can't find the object "Ereignisprozedur"
If Ereignisprozedur is a new macro or macro group, make sure you have saved it and that you have typed its name correctly
Access was unable to locate the macro or VBA function. If you are trying to call a macro, make sure that the name of the macro and the name of the macro group are spelled correctly.
If you are trying to call a user-defined VBA function, be sure to use the following syntax:
=FunctionName() or =FunctionName(argument1,argument2,...)
Make sure that the function is either:
Defined in the code for the form or report.
- or -
A public function that is in a module (not a class module)."
And the word "Formulare" to Forms. Then the program works.
I can't understand why microsoft programs are language independent???
Do you mean you deleted a word in one of the Queries? What it is is a reference to a form called Frm_Fahrzeugdaten and a control (field, column) called Schaltflächenkriterium. It is under criteria, so the query is saying:
Select such and such where this field (column) is Like this form and this control
It is usual to have
Like "*" & [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium] & "*"
Or
Like [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium] & "*"
That is, with wild cards (*)
[Schaltflächenkriterium] seems to me to be the name of an edit control (textbox) in form [Frm_Fahrzeugdaten] for the user to filter a record set.
If you deleted the textbox control, just re-insert it and name it correctly.