|
Public Sub subAddTableField(strTableName As String, strFieldName As
String, strFieldType As String, Optional strIndex As String)
Dim dbs As Database Dim strSQL As String Dim strTablesDatabase As String Dim tdfLinked As TableDef
On Error GoTo Error_subAddTableField
If funTableExists(strTableName) = False Then
GoTo Exit_subAddTableField End If
strTablesDatabase = funGetLinkedDBName(strTableName)
If strTablesDatabase = CurrentDb.Name Then
DoCmd.DeleteObject acTable, strTableName
End If
On Error GoTo Insert_Field
strSQL = "SELECT " & strTableName & "." & strFieldName & " FROM " & strTableName & ";"
Set dbs = CurrentDb
dbs.OpenRecordset strSQL, dbOpenSnapshot, dbSeeChanges
Exit_subAddTableField: On Error GoTo 0 Exit Sub
Insert_Field:
Set tdfLinked = dbs.TableDefs(strTableName) will fail.
DoCmd.DeleteObject acTable, tdfLinked.Name
DoCmd.TransferDatabase acImport, "Microsoft Access", strTablesDatabase,
acTable, strTableName, strTableName
strSQL = "ALTER TABLE " & strTableName & " ADD
COLUMN " & strFieldName & " " & strFieldType & " " & Nz(strIndex, "")
subRunUpdateQuery strSQL
DoCmd.TransferDatabase acExport, "Microsoft Access", strTablesDatabase,
acTable, strTableName, strTableName
DoCmd.DeleteObject acTable, tdfLinked.Name
subLinkToOneBETable strTableName, strTablesDatabase
GoTo Exit_subAddTableField
Error_subAddTableField:
MsgBox "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Module Name: modReleaseSetup" & vbCrLf & _
"Type: Module" & vbCrLf & _
"Calling Procedure: subAddTableField" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description
Resume Exit_subAddTableField Resume
End Sub
' Public Function funTableExists(strTblName As String) As Boolean
Dim dbs As Database
Dim tbl As TableDef Dim dbsExist As Object
On Error GoTo Error_funTableExists
funTableExists = False
Set dbs = CurrentDb
Set dbsExist = dbs.TableDefs
For Each tbl In dbsExist
If tbl.Name = strTblName Then
funTableExists
= True ' Set the function to true
GoTo Exit_funTableExists ' Quit if true
End If
Next tbl
Exit_funTableExists: On Error GoTo 0 Set dbsExist = Nothing ' Clean up Exit Function
Error_funTableExists:
MsgBox "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Module Name: modGeneric" & vbCrLf & _
"Type: Module" & vbCrLf & _
"Calling Procedure: funTableExists" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description
Resume Exit_funTableExists
Resume
End Function
Public Function funGetLinkedDBName(TableName As String)
Dim dbs As DAO.Database Dim varReturn As Variant
On Error GoTo Error_NoTable ' Handles table not found
Set dbs = CurrentDb() varReturn = dbs.TableDefs(TableName).Connect
On Error GoTo Error_funGetLinkedDBName
funGetLinkedDBName = Right(varReturn, Len(varReturn) - (InStr _
(1, varReturn, "DATABASE=") + 8))
Exit_funGetLinkedDBName: On Error GoTo 0 Exit Function
Error_NoTable:
funGetLinkedDBName = "0" GoTo Exit_funGetLinkedDBName
Error_funGetLinkedDBName:
MsgBox "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Module Name: modGeneric" & vbCrLf & _
"Type: Module" & vbCrLf & _
"Calling Procedure: funGetLinkedDBName" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description
Resume Exit_funGetLinkedDBName Resume
End Function
Public Sub subLinkToOneBETable(strTableName As String, strBEPath As
String)
Dim dbs As Database Dim tdf As TableDef Dim tdfLinked As TableDef Dim strPW As String
'-------------------------------------------------------------- ' Check it is a valid path
On Error GoTo Error_subLinkToOneBETable
If funFileExists(strBEPath) = False Then
Call MsgBox("The path provided for the back end database is incorrect.
Please ensure the correct path is provided. " _
& vbCrLf & "" _
& vbCrLf & "See your System Administrator." _
, vbCritical, "Missing Database")
GoTo Exit_subLinkToOneBETable
End If
Set tdf = Nothing Set dbs = Nothing
Set dbs = OpenDatabase(strBEPath, False, False) ' This will fail if
there is a password on the BE Set tdfLinked = dbs.TableDefs(strTableName)
If funCheck4Nothing(tdfLinked.Name) = True Then
strPW = InputBox("Please enter the database password." & _
" If you do not know the password, see your System Administrator.",
_
"Database Password") ' Ask for the password
Set dbs = OpenDatabase(strBEPath, False, False, ";pwd=" & strPW)
End
If
If funTableExists(tdfLinked.Name) Then
DoCmd.DeleteObject acTable, tdfLinked.Name
End
If
DoCmd.TransferDatabase acLink, "Microsoft Access", _ strBEPath, acTable, tdfLinked.Name, tdfLinked.Name
DoCmd.Hourglass False
Exit_subLinkToOneBETable: On Error GoTo 0 Exit Sub
Error_subLinkToOneBETable:
MsgBox "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Module Name: modGeneric" & vbCrLf & _
"Type: Module" & vbCrLf & _
"Calling Procedure: subLinkToOneBETable" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description
Resume Exit_subLinkToOneBETable
Resume
End Sub
Public Function funCheck4Nothing(var As Variant)
On Error GoTo Error_funCheck4Nothing
If IsNull(var) Or var = "" Or var = 0 Then
funCheck4Nothing = True
Else
funCheck4Nothing = False
End If
Exit_funCheck4Nothing:
On Error GoTo 0
Exit Function
Error_funCheck4Nothing:
MsgBox "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Module Name: modGeneric" & vbCrLf & _
"Type: Module" & vbCrLf & _
"Calling Procedure: funCheck4Nothing" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description
Resume Exit_funCheck4Nothing
Resume
End Function |