Solved

Pleasant message as alternative to RunTime Error

Posted on 2013-06-24
1
296 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now