Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pleasant message as alternative to RunTime Error

Posted on 2013-06-24
1
Medium Priority
?
304 Views
Last Modified: 2013-06-25
A few months ago an EE expert helped me with some code to establish a link to a file stored in a network folder. I don't have access to the folder, but some of the users have been receiving a runtime error 52: 'Bad file name or number'. No idea what that means. As an alternative to that message, I was thinking it might be better to give them a message box with a better explanation. Since someone helped me with the code, I don't know exactly what each line is doing. Would appreciate any help or suggestions.

Thanks, Dale

Public Function Relink()
Dim str As String, path As String, fname As String, tbl As String, strlnk As String, str1 As String, str2 As String
Dim l1 As Integer, l2 As Integer
Dim db As DAO.Database

fname = "stocklist.csv"
path = "\\000.00.00.00\Forecast"
tbl = "Stocklist"
str = path & "\" & fname
If Dir(str) & "" = "" Then
    path = "e:\tmp"
    str = path & "\" & fname
    If Dir(str) & "" = "" Then
        MsgBox "File not found", vbOKOnly
        Exit Function
    End If
End If
Set db = CurrentDb
strlnk = db.TableDefs("Stocklist").Connect
l1 = InStr(1, strlnk, "DATABASE") + 8
str1 = Left(strlnk, l1)
str2 = Mid(strlnk, l1 + 1)
l2 = InStr(1, str2, ";")
If l2 = 0 Then
    str2 = path
Else
    str2 = path & Mid(str2, l2)
End If
db.TableDefs(tbl).Connect = str1 & str2
db.TableDefs(tbl).RefreshLink
Set db = Nothing
End Function

Open in new window

0
Comment
Question by:Dale Logan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 39273306
You should add error handling code to manage this:

Public Function Relink()

On Error GoTo Err

Dim str As String, path As String, fname As String, tbl As String, strlnk As String, str1 As String, str2 As String
Dim l1 As Integer, l2 As Integer
Dim db As DAO.Database

fname = "stocklist.csv"
path = "\\000.00.00.00\Forecast"
tbl = "Stocklist"
str = path & "\" & fname
If Dir(str) & "" = "" Then
    path = "e:\tmp"
    str = path & "\" & fname
    If Dir(str) & "" = "" Then
        MsgBox "File not found", vbOKOnly
        Exit Function
    End If
End If
Set db = CurrentDb
strlnk = db.TableDefs("Stocklist").Connect
l1 = InStr(1, strlnk, "DATABASE") + 8
str1 = Left(strlnk, l1)
str2 = Mid(strlnk, l1 + 1)
l2 = InStr(1, str2, ";")
If l2 = 0 Then
    str2 = path
Else
    str2 = path & Mid(str2, l2)
End If
db.TableDefs(tbl).Connect = str1 & str2
db.TableDefs(tbl).RefreshLink
Set db = Nothing

Exit Function

Err:
  Select Case Err.Number
    Case 51
	  Msgbox "The system was not able to locate the file located below located at \\000.00.00.00\Forecast\stocklist.cvs. Please review this location to insure the file is present.", vbOkOnly
	Case Else
	  '/ Handle other errors here
  End Select
End Function

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question