Solved

Pleasant message as alternative to RunTime Error

Posted on 2013-06-24
1
301 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:dlogan7
[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 500 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

729 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