J
john.mctigue
Hello group,
I have been asked (as a non-programmer!) to develop a database of
blood cancers. As a guide I have a similar cancer registry database I
can refer to. I see this as forming the basis for what I am to
develop and have been going through it, including the code to try and
'decipher' it. Unfortunately the original developer has left the
organisation.
The 'model' database has a front end containing queries, forms and
reports and a back end containing other queries and the tables. There
is a button on the front end main switchboard with the caption 'reLink
Tables'. I am trying to understand the purpose of this button.
Having looked at the code behind this (see below) I simplistically
thought that it would open a file open dialog (line 00289) to allow
the user to locate the database backend and relink the tables, at the
user's discretion. However, the file open dialog is not displayed.
Instead I get the message "Sorry, you must locate the database backend
to open Cancer Registry." If I click this button a second time, oh
dear, on occassion (but not always) I get "Microsoft Office Access has
encountered a problem and needs to close". Something isn't quite
right, I think.
The front end and back end database are in the same directory. The
'reLink Tables' button executes a macro named linkage with the single
action RunCode and argument function name 'relinktables()'.
The following may be relevant:
Access 2003 SP3
00001 Option Compare Database
00002
00003 Option Explicit
00004
00005
00006
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
00007 '
RefreshTableLinks '
00008
'
'
00009 ' This module contains functions that refresh the
links '
00010 ' to the database tables if they aren't
available. '
00011
'
'
00012
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
00013
00014
00015 Global Const APPLICATIONTITLE = "Cancer Registry"
00016 Global Const BACKEND_DATABASE = "CaRegData.mdb"
00017
00018
00019 Type OPENFILENAME
00020 lStructSize As Long
00021 hwndOwner As Long
00022 hInstance As Long
00023 lpstrFilter As String
00024 lpstrCustomFilter As String
00025 nMaxCustomFilter As Long
00026 nFilterIndex As Long
00027 lpstrFile As String
00028 nMaxFile As String
00029 lpstrFileTitle As String
00030 nMaxFileTitle As String
00031 lpstrInitialDir As String
00032 lpstrTitle As String
00033 flags As Long
00034 nFileOffset As Integer
00035 nFileExtension As Integer
00036 lpstrDefExt As String
00037 lCustData As Long
00038 lpfnHook As Long
00039 lpTemplateName As String
00040 End Type
00041
00042 Declare Function GetOpenFileName Lib "comdlg32.dll" Alias
"GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As Long
00043 '
00044
00164 Function RelinkTables() As Integer
00165
00166 ' Tries to refresh the links to the database. Returns True
if successful.
00167
00168 Dim StrFileName As String, StrError As String
00169
00170 Const MaxTables = 11
00171 Const NonExistentTable = 3011
00172 Const NotData = 3078
00173 Const NotFound = 3024
00174 Const AccessDenied = 3051
00175 Const ReadOnlyDatabase = 3027
00176 Const AppTitle = APPLICATIONTITLE
00177
00178 StrFileName = FetchDatabase()
00179 If StrFileName = "" Then
00180 DoCmd.Beep
00181 StrError = "Sorry, you must locate the database backend
to open " & AppTitle & "."
00182 GoTo Exit_Failed
00183 End If
00184
00185 ' Fix the links.
00186 If RefreshLinks(StrFileName) Then
00187 RelinkTables = True
00188 DoCmd.Beep
00189 MsgBox "Front end linked to back end database
successfully"
00190 Exit Function
00191 End If
00192
00193 ' If it failed, display an error.
00194 DoCmd.Beep
00195 Select Case Err
00196 Case NonExistentTable, NotData
00197 StrError = "File '" & StrFileName & "' does not contain
the required database tables."
00198 Case Err = NotFound
00199 StrError = "You can't run " & AppTitle & " until you
locate the database."
00200 Case Err = AccessDenied
00201 StrError = "Couldn't open " & StrFileName & " because it
is read-only or located on a read-only share."
00202 Case Err = ReadOnlyDatabase
00203 StrError = "Can't relink tables because " & AppTitle & "
is read-only or is located on a read-only share."
00204 Case Else
00205 StrError = Error
00206 End Select
00207
00208 Exit_Failed:
00209 MsgBox StrError
00210 RelinkTables = False
00211
00212 End Function
00213 '
00214
00260 Function FetchDatabase() As String
00261
00262 ' Displays the Open dialog box for the user to locate the
database.
00263 ' Returns the full path/file to database.
00264
00265 Dim File As OPENFILENAME, X As Integer, nForm As Integer
00266 Dim temp As String, SearchPath As String
00267 Dim dbs As Database
00268
00269 temp = Space(256)
00270
00271 On Error GoTo FetchDatabaseError
00272
00273 Set dbs = CurrentDb()
00274 SearchPath = ExtractDir("" & dbs.Name)
00275
00276 nForm = Forms.Count - 1
00277
00278 File.lStructSize = Len(File)
00279 File.hwndOwner = Forms(nForm).Hwnd
00280 File.lpstrFilter = "MS Access Databases [*.mdb]" &
vbNullChar & BACKEND_DATABASE & vbNullChar & vbNullChar
00281 File.lpstrFile = BACKEND_DATABASE & Space(255 -
Len(BACKEND_DATABASE))
00282 File.nMaxFile = 255
00283 File.lpstrFileTitle = Space(255)
00284 File.nMaxFileTitle = 255
00285 File.lpstrInitialDir = SearchPath
00286 File.lpstrTitle = "Database Backend Data File Location"
00287 File.flags = 0
00288
00289 X = GetOpenFileName(File)
00290 If X = 0 Then Exit Function ' Abort on error or Cancel
00291
00292 ' Extract the filename
00293 temp = Trim(File.lpstrFile)
00294 FetchDatabase = Left(temp, Len(temp) - 1) ' Trim ending
vbNullChar
00295
00296 Exit Function
00297
00298 FetchDatabaseError:
00299 MsgBox "Error :" & Error
00300 Exit Function
00301
00302 End Function
00303 '
00304
00305 Function ExtractDir(StrIn As String) As String
00306
00307 Dim temp As String, I As Integer
00308
00309 temp = StrIn
00310
00311 For I = Len(StrIn) To 1 Step -1
00312 If Len(temp) <= 3 Then Exit For
00313
00314 If Mid(temp, I, 1) = "\" Then
00315 temp = Left(temp, I - 1)
00316 Exit For
00317 End If
00318 Next
00319
00320 ExtractDir = temp
00321
00322 End Function
00323 '
I have been asked (as a non-programmer!) to develop a database of
blood cancers. As a guide I have a similar cancer registry database I
can refer to. I see this as forming the basis for what I am to
develop and have been going through it, including the code to try and
'decipher' it. Unfortunately the original developer has left the
organisation.
The 'model' database has a front end containing queries, forms and
reports and a back end containing other queries and the tables. There
is a button on the front end main switchboard with the caption 'reLink
Tables'. I am trying to understand the purpose of this button.
Having looked at the code behind this (see below) I simplistically
thought that it would open a file open dialog (line 00289) to allow
the user to locate the database backend and relink the tables, at the
user's discretion. However, the file open dialog is not displayed.
Instead I get the message "Sorry, you must locate the database backend
to open Cancer Registry." If I click this button a second time, oh
dear, on occassion (but not always) I get "Microsoft Office Access has
encountered a problem and needs to close". Something isn't quite
right, I think.
The front end and back end database are in the same directory. The
'reLink Tables' button executes a macro named linkage with the single
action RunCode and argument function name 'relinktables()'.
The following may be relevant:
Access 2003 SP3
00001 Option Compare Database
00002
00003 Option Explicit
00004
00005
00006
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
00007 '
RefreshTableLinks '
00008
'
'
00009 ' This module contains functions that refresh the
links '
00010 ' to the database tables if they aren't
available. '
00011
'
'
00012
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
00013
00014
00015 Global Const APPLICATIONTITLE = "Cancer Registry"
00016 Global Const BACKEND_DATABASE = "CaRegData.mdb"
00017
00018
00019 Type OPENFILENAME
00020 lStructSize As Long
00021 hwndOwner As Long
00022 hInstance As Long
00023 lpstrFilter As String
00024 lpstrCustomFilter As String
00025 nMaxCustomFilter As Long
00026 nFilterIndex As Long
00027 lpstrFile As String
00028 nMaxFile As String
00029 lpstrFileTitle As String
00030 nMaxFileTitle As String
00031 lpstrInitialDir As String
00032 lpstrTitle As String
00033 flags As Long
00034 nFileOffset As Integer
00035 nFileExtension As Integer
00036 lpstrDefExt As String
00037 lCustData As Long
00038 lpfnHook As Long
00039 lpTemplateName As String
00040 End Type
00041
00042 Declare Function GetOpenFileName Lib "comdlg32.dll" Alias
"GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As Long
00043 '
00044
00164 Function RelinkTables() As Integer
00165
00166 ' Tries to refresh the links to the database. Returns True
if successful.
00167
00168 Dim StrFileName As String, StrError As String
00169
00170 Const MaxTables = 11
00171 Const NonExistentTable = 3011
00172 Const NotData = 3078
00173 Const NotFound = 3024
00174 Const AccessDenied = 3051
00175 Const ReadOnlyDatabase = 3027
00176 Const AppTitle = APPLICATIONTITLE
00177
00178 StrFileName = FetchDatabase()
00179 If StrFileName = "" Then
00180 DoCmd.Beep
00181 StrError = "Sorry, you must locate the database backend
to open " & AppTitle & "."
00182 GoTo Exit_Failed
00183 End If
00184
00185 ' Fix the links.
00186 If RefreshLinks(StrFileName) Then
00187 RelinkTables = True
00188 DoCmd.Beep
00189 MsgBox "Front end linked to back end database
successfully"
00190 Exit Function
00191 End If
00192
00193 ' If it failed, display an error.
00194 DoCmd.Beep
00195 Select Case Err
00196 Case NonExistentTable, NotData
00197 StrError = "File '" & StrFileName & "' does not contain
the required database tables."
00198 Case Err = NotFound
00199 StrError = "You can't run " & AppTitle & " until you
locate the database."
00200 Case Err = AccessDenied
00201 StrError = "Couldn't open " & StrFileName & " because it
is read-only or located on a read-only share."
00202 Case Err = ReadOnlyDatabase
00203 StrError = "Can't relink tables because " & AppTitle & "
is read-only or is located on a read-only share."
00204 Case Else
00205 StrError = Error
00206 End Select
00207
00208 Exit_Failed:
00209 MsgBox StrError
00210 RelinkTables = False
00211
00212 End Function
00213 '
00214
00260 Function FetchDatabase() As String
00261
00262 ' Displays the Open dialog box for the user to locate the
database.
00263 ' Returns the full path/file to database.
00264
00265 Dim File As OPENFILENAME, X As Integer, nForm As Integer
00266 Dim temp As String, SearchPath As String
00267 Dim dbs As Database
00268
00269 temp = Space(256)
00270
00271 On Error GoTo FetchDatabaseError
00272
00273 Set dbs = CurrentDb()
00274 SearchPath = ExtractDir("" & dbs.Name)
00275
00276 nForm = Forms.Count - 1
00277
00278 File.lStructSize = Len(File)
00279 File.hwndOwner = Forms(nForm).Hwnd
00280 File.lpstrFilter = "MS Access Databases [*.mdb]" &
vbNullChar & BACKEND_DATABASE & vbNullChar & vbNullChar
00281 File.lpstrFile = BACKEND_DATABASE & Space(255 -
Len(BACKEND_DATABASE))
00282 File.nMaxFile = 255
00283 File.lpstrFileTitle = Space(255)
00284 File.nMaxFileTitle = 255
00285 File.lpstrInitialDir = SearchPath
00286 File.lpstrTitle = "Database Backend Data File Location"
00287 File.flags = 0
00288
00289 X = GetOpenFileName(File)
00290 If X = 0 Then Exit Function ' Abort on error or Cancel
00291
00292 ' Extract the filename
00293 temp = Trim(File.lpstrFile)
00294 FetchDatabase = Left(temp, Len(temp) - 1) ' Trim ending
vbNullChar
00295
00296 Exit Function
00297
00298 FetchDatabaseError:
00299 MsgBox "Error :" & Error
00300 Exit Function
00301
00302 End Function
00303 '
00304
00305 Function ExtractDir(StrIn As String) As String
00306
00307 Dim temp As String, I As Integer
00308
00309 temp = StrIn
00310
00311 For I = Len(StrIn) To 1 Step -1
00312 If Len(temp) <= 3 Then Exit For
00313
00314 If Mid(temp, I, 1) = "\" Then
00315 temp = Left(temp, I - 1)
00316 Exit For
00317 End If
00318 Next
00319
00320 ExtractDir = temp
00321
00322 End Function
00323 '