Determining if a path is a network path VBA - ms-access

developing an application in Access. The access file will be located on a db however when users need to use it, I want them to copy it on there desktop. If they do run it off the G:\ drive (our networked folder), it should give them a message.
So are there Win API that will help me solve this?
I am going to put this code in the Form_Load event of a form.

If you want to prevent the users opening your database from the G:\ drive, you can do a simple check with code like this in your startup form:
Dim strMsg As String
If CurrentProject.Path Like "G:*" Then
strMsg = "Please copy this database file to your " & _
"local disk and open the copy instead of this one."
MsgBox strMsg
Application.Quit
End If
If you also want to prevent them opening the database from a different drive letter mapping or a UNC path, you could add a file such as NotFromHere.txt to the folder where your database file is stored.
Dim strMsg As String
Dim strFilePath
strFilePath = CurrentProject.Path & Chr(92) & "NotFromHere.txt"
If Len(Dir(strFilePath)) > 0 Then
strMsg = "Please copy this database file to your " & _
"local disk and open the copy instead of this one."
MsgBox strMsg
Application.Quit
End If

You can use the FileSystemObject DriveType property
http://msdn.microsoft.com/en-us/library/ea5ht6ax(VS.85).aspx
If you need the desktop folder, you might like to look at:
CreateObject("WScript.Shell").SpecialFolders("Desktop")

You might want to check out the excellent access auto FE updater as an automated way of copying down updates etc
http://autofeupdater.com/
I rolled my own very similar system before I found this one and it does make updating the FE so much easier

I think there's a call that will list the (physically) attached drives on a machine, you could call that and compare the drive specifier on the path the user gave against the list.

Related

I want to export from Access to Excel a Table I created in Access without Access asking me if I want to overwrite the existing file

Is it possible without VBA? I can send the file the first time with no problem. I can manually go in and delete the file then run Access - no problems. I want Access to run and then overwrite the existing Excel file without asking me if I want it overwritten.
I do not think is possible without VBA.
Delete the file before exporting.
Dim strFile As String
strFile = "your filepath here"
If Dir(strFile) <> "" Then Kill(strFile)
'export code here

Problem Importing Excel File into Access via VBA

I've created a multi-user Access Form that will essentially be open for most of the day on several peoples computers. On this form, I've created a button that will import an excel spreadsheet via VBA. The code is:
Option Compare Database
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE ExcelTable.* FROM ExcelTable;"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "ExcelTable", "C:\Users\JohnDoe\Documents\MyTable.xlsx", True, "Data!A6:DB" & numberofrows
DoCmd.SetWarnings True
End Sub
This code imports the data fine. However, users will frequently need to update the spreadsheet throughout the day. There are 2 problems that I am frequently running into:
If a user attempts to overwrite the original spreadsheet with an updated version, they receive an error message "Cannot access read-only document".
If the user attempts to open the spreadsheet, they receive the error message that the excel file is locked for editing.
I was under the impression that by running the above VBA code, the user is importing a static table (not a linked table), meaning that once the table is imported into Access, it's no longer referencing the original Excel file.
How do I get around these error messages?
The users will need to update the excel file several times throughout the day and right now, everyone has to completely exit Access in order to access the original excel file.

Launching a pdf file located in the current project path

I want users to be able to put the Project folder on any location of their choice. If I use:
Application.FollowHyperlink ("C:\Program Files(x86)\Project\reference.pdf")
The pdf document launches only if the user put the Project folder in the Program Files(x86) folder. Is there a way for access to refer to the current path instead? I have tried the below code with no luck.
Private Sub referencefile_Click()
Application.FollowHyperlink (".\reference.pdf")
End Sub
I have tried:
Application.FollowHyperlink (CurrentProject.Path & "\reference.pdf"),
NewWindow:=True
This works well with txt files but not with pdf files. Any idea?
After a Quick Check and ensuring that my pdf file was not corrupted.
Application.FollowHyperlink (CurrentProject.Path & "\reference.pdf"), NewWindow:=True
The above code works fine as long as the pdf file exits in the same location as the front end. If you have ever thought of eliminating the security warning that access gives when you manually enter a hyperlink to a control button's properties, this code may be a good solution.

Get path from OLE Object Link MS Access 2007

I have a couple tables in a database that use OLE Objects as links to files on network drives. I used the module from http://support.microsoft.com/kb/199066 to try to get the path from the OLE Objects but all I get is links to .ico or .exe rather than the actual path. If I double click on the Links field value the files open correctly from the network location.
The properties you seek is as following,
You should
Dim Ref As Reference
For Each Ref In References
MsgBox Ref.Name & " > " & Ref.FullPath
Next
Will give you names and paths as message.

Access 2007 - Display PDF content on a form

On Access 2007, is there a way to display the content of a PDF, even if it is just the first page, on a form? This PDF saved in a table as attachment.
Disclaimer: This answer will only work for PDF files stored outside of your database as separate file. They can be located over a network connection, but I do not know how to access them directly from your database table. This site gives a thorough guide to using the attachments, but doesn't show how to actually display them automatically. It is likely functionality not provided by Access.
You can display anything Internet Explorer can display with a Microsoft Web Browser Control.
Once you've added the control, you can navigate to whatever you want to display during the load or open event of the form.
For example, if the control is called WebBrowser0 then the following would work:
Private Sub Form_Load()
Me.WebBrowser0.Navigate2 "C:\example.pdf" 'Substitute the actual address here.
End Sub
This is an extremely versatile method for displaying other content within Access. You can find more information here.
The only two methods I know of for previewing a PDF (WebBrowswer as suggested by Daniel and the Adobe Active X control) require a file path to be passed to the control.
I recommend extracting the file from the attachment field and saving it to a temporary location such as C:\Documents and Settings\username\AppData. This can be found by using the vba Environ command.
Extracting the file is done with the SaveToFile method in the embedded DAO recordset (which is how attachments are stored in memory).
Example Code
Assume each record has a field called AttachedFile and each record has only one attached PDF. The form is using a WebBrowser control named PreviewBrowser to view the PDF
Private Sub Form_Current()
On Error GoTo ExitSub
Dim FormRS As DAO.Recordset
Set FormRS = Me.Recordset
Dim RecAtt As DAO.Recordset
If (Me.AttachedFile.AttachmentCount > 0) Then
Set RecAtt = FormRS.Fields("AttachedFile").Value
RecAtt.OpenRecordset
Dim Path As String
FilePath = Environ("APPDATA") & "\Preview.pdf"
If (Dir(FilePath) <> "") Then Kill FilePath
RecAtt.Fields("FileData").SaveToFile FilePath
Me.PreviewBrowser.Navigate2 FilePath
End If
ExitSub:
RecAtt.Close
End Sub
Of course the code needs to be a bit more complicated if there are multiple attachments to a given record, but that would be done by manipulating the RecAtt recordset.