Solved

Pleasant message as alternative to RunTime Error

Posted on 2013-06-24
1
298 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
1 Comment
 
LVL 84

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Attachment field in SQL 3 28
Need to Populate a MS Access Table via VBA Code 3 30
deduplicating based on criteria 2 21
Access 2003 query lost it's only join 7 27
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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 how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

778 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