db.Properties("StartUpShowDBWindow").Value = False
However, this will also only hide the NavPane the next time you open the current database. Additionally, if that database property has not already been created, then this may raise an error.
Public Sub DisplayNavPane(Optional IsVisible As Boolean = True)
DoCmd.SelectObject acTable, , True
If IsVisible = False Then DoCmd.RunCommand acCmdWindowHide
End Sub
The solution that did work used one of the following code segments, both of which I have seen in recent question solutions.
DoCmd.NavigateTo "acNavigationCategoryObjectType", "acNavigationGroupTables"
DoCmd.RunCommand acCmdWindowHide
DoCmd.NavigateTo "acNavigationCategoryObjectType", "acNavigationGroupTables"
DoCmd.Minimize
Public Function LinkTable(Filename As String, SourceTableName As String, _
Optional DestinationTableName As Variant = Null) As Boolean
Dim tdfDest As DAO.TableDef
Dim strDestName As String
On Error GoTo ProcError
strDestName = Nz(DestinationTableName, SourceTableName)
Set tdfDest = CurrentDb.CreateTableDef(strDestName)
tdfDest.Connect = ";DATABASE=" & Filename
tdfDest.SourceTableName = SourceTableName
CurrentDb.TableDefs.Append tdfDest
LinkTable = True
ProcExit:
On Error Resume Next
Set tdfDest = Nothing
Exit Function
ProcError:
MsgBox Err.Number & vbCrLf & Err.Description, , "LinkTable error!"
Debug.Print "LinkTable error", Err.Number, Err.Description
LinkTable = False
Resume ProcExit
Resume
End Function
The syntax changes a bit when linking to Excel worksheets because the connection string and source table names are referenced slightly differently. Additionally, the reference to the particular version of Excel may vary so you may need some error handling to loop through the possible Excel connection strings, depending on the version of Office you are using.
Public Function LinkExcel(Filename As String, SheetName As String, _
Optional DestinationTableName As Variant = Null) As Boolean
Dim tdfDest As DAO.TableDef
Dim strDestName As String
On Error GoTo ProcError
strDestName = Nz(DestinationTableName, SheetName)
Set tdfDest = CurrentDb.CreateTableDef(strDestName)
tdfDest.Connect = "Excel 12.0;HDR=YES;IMEX=2;ACCDB=YES;" _
& "DATABASE=" & Filename
tdfDest.SourceTableName = SheetName & "$"
CurrentDb.TableDefs.Append tdfDest
LinkExcel = True
ProcExit:
On Error Resume Next
Set tdfDest = Nothing
Exit Function
ProcError:
MsgBox Err.Number & vbCrLf & Err.Description, , "LinkExcel error!"
Debug.Print "LinkExcel error", Err.Number, Err.Description
LinkExcel = False
Resume ProcExit
Resume
End Function
I've had limited success with linking delimited text files because I have not figured out how to identify whether there is a delimiter other than the default (comma). Additionally, if the text fields in the file are not wrapped in quotes, then long numeric strings will be interpreted as numeric and will obviously cause errors if the value of the string exceeds the limits of the long number data type. However, for well-formatted CSV files with text delimiters, the following code has worked well for me.
Public Function LinkCSV(Filename As String, DestinationTableName As String) As Boolean
Dim tdfDest As DAO.TableDef
Dim strDestName As String
Dim strPath As String
Dim strFileFull As String, strFileShort
On Error GoTo ProcError
strPath = Left(Filename, InStrRev(Filename, "\") - 1)
strFileFull = Mid(Filename, InStrRev(Filename, "\") + 1)
'Drop the file extension
strFileShort = Left(strFileFull, InStrRev(strFileFull, ".txt") - 1)
strDestName = DestinationTableName
Set tdfDest = CurrentDb.CreateTableDef(strDestName)
tdfDest.Connect = "Text;DSN=" & strFileShort & " Link Specification;FMT=Delimted;" _
& "HDR=NO;IMEX=2;CharacterSet=437;" _
& "ACCDB=YES;DATABASE=" & strPath
tdfDest.SourceTableName = strFileFull
CurrentDb.TableDefs.Append tdfDest
LinkCSV = True
ProcExit:
On Error Resume Next
Set tdfDest = Nothing
Exit Function
ProcError:
MsgBox Err.Number & vbCrLf & Err.Description, , "LinkCSV error!"
Debug.Print "LinkCSV error", Err.Number, Err.Description
LinkCSV = False
Resume ProcExit
Resume
End Function
I have not had the need to link to fixed field width text files in any of my production applications. To do so, you can simply change the "FMT=Delimted" (no, that is not a misspelling, that is the way it appears in the connection string) with "FMT=Fixed". However, I cannot see how this would be helpful without knowing or being able to set the width of the columns at run-time.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
:)
I have also seen cases where theses two code blocks are NOT equivalent
Open in new window
AndOpen in new window
With the second code block -- even though it looks functionally equivalent -- failing.
Some of that failure can come as a result of scope
CurrentDb exists, presumably when the app opens
It has its listing of objects from the time of its creation
If you've done tabledef deletion/creation, CurrentDb won't reflect that
When you instantiate your own object, the engine round-trips and ensures that the most current collection of objects is in the CurrentDb it gives you for your object.
@JimDettman and I had had an interesting discussion of that a while back.
You clearly haven't tripped on it yet--which is good
Author
Commented:The db vs. Currentdb issue has been around for a while, but I think you've got it backwards. If my recollection is correct, the Currentdb reference will always force Access to refresh the various object collections, while the db version creates a static object based on the Currentdb when the db object is instantiated.
Whenever I'm doing loops in code that reference a database object, I will generally declare the db object and use it, but in cases like this, I don't think it is necessary.
Another point about using db vs Currentdb is that if you are working in the immediate window, use the db method, otherwise, references to various database objects will loose their scope.
Author
Commented:I hope this is useful, I know you seemed really frustrated with the NavPain during that thread we were working on together the other day.
Dale