populating file names and locations to a table

G

Guest

I have a table called Inventory.
I have 2 fields in the table 1. {file name} and the other {File location}

I have a form called {Flistfiles}

My question: I can call up the computers drive and files using the following
code.

Dim strFilter As String
Dim lngflags As Long
Dim varFileName As Variant

strFilter = "All Files (*.*)" & vbNullChar & "*.*" _
& vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"

lngflags = tscFNPathMustExist Or tscFNFileMustExist _
Or tscFNHideReadOnly

varFileName = tsGetFileFromUser( _
fOpenFile:=True, _
strFilter:=strFilter, _
rlngflags:=lngflags, _
strDialogTitle:="Please choose a file...")

If IsNull(varFileName) Then
Else
Me![File Location] = varFileName
End If

cmdAddSmall_End:
On Error GoTo 0
Exit Sub

cmdAddSmall_Err:
Beep
MsgBox Err.Description, , "Error: " & Err.Number _
& " in file"
Resume cmdAddSmall_End
End Sub

This puts the complete path and file name into my text box called {file
location} which the form is based on Tinventory table.

Now what I would like to do is have 1 command button that when I select to
the drive and folder would list all the file names and there locations and
just populate the complete table, thus; saving me from doing them one at a
time.
If possible what code would be needed. Thanks,
 
G

Guest

Thanks Tom,

I'm sorry I didn't post the module in so you could see how the code worked.

Here it Is.
Option Compare Database
Option Explicit


'.=========================================================================
'.Browse Files Module
'.Copyright 1999 Tribble Software. All rights reserved.
'.Phone : (616) 455-2055
'.E-mail : (e-mail address removed)
'.=========================================================================
' DO NOT DELETE THE COMMENTS ABOVE. All other comments in this module
' may be deleted from production code, but lines above must remain.
'--------------------------------------------------------------------------
'.Description : This module calls directly into comdlg32.dll to allow user
'. to select a filename using the Windows Common Dialog. The
'. user may browse for a file, or enter a file name directly.
'.Written By : Carl Tribble
'.Date Created : 04/05/1999 09:56:31 AM
'.Rev. History :
' Comments : Normally, to use the Common Dialog you need to physically
' place the ActiveX control onto a form and then use code
' behind the form to implement its functionality. This
' module allows you to incorporate the functionality of the
' File Open/Save part of the Common Dialog without the
' ActiveX control itself. This module is completely self-
' contained. Simply copy it into your database to use it.
'.-------------------------------------------------------------------------
'.
' ADDITIONAL NOTES:
'
' This module only provides the Open/Save file dialog, not the other
' CommonDialog interfaces (ColorChooser, Help, PrintDialog, etc.)
'
' If you want your user to browse for folder names (paths) you must use
' the module basBrowseFolders instead.
'
' TO STREAMLINE this module for production programs, you should remove:
' 1) Unnecessary comments
' 2) Flag Constants which you do not intend to use.
' 3) The test procedure tsGetFileFromUserTest
'
'--------------------------------------------------------------------------
'
' INSTRUCTIONS:
'
' ( For a working example, open the Debug window )
' ( and enter tsGetFileFromUserTest. )
'
'.All the arguments for the function are optional. You may call it with no
'.arguments whatsoever and simply assign its return value to a variable of
'.the Variant type. For example:
'.
'. varFileName = tsGetFileFromUser()
'.
'.The function will return:
'. the full path and filename selected or entered by the user, or
'. Null if an error occurs or if the user presses Cancel.
'.
'.Optional arguments may include any of the following:
'. rlngFlags : one or more of the tscFN* constants (declared below)
'. Combine multiple constants like this:
'. tscFNHideReadOnly Or tscFNFileMustExist
'. strInitialDir : the directory to display when dialog opens
'. strFilter : a string containing any filters you want to use. Each
'. part must be separated by the vbNullChar. -example below
'. lngFilterIndex: a 1-based index indicating which filter to start with.
'. strDefaultExt : Extension to use if user does not enter one.
'. strFileName : Default File to display in the File Name text box.
'. strDialogTitle: Caption to display in the dialog's title bar.
'. fOpenFile : Boolean-True for the Open dialog, False for Save dialog.
'
' FILTER EXAMPLE: The filter must be a string containing two parts for each
' filter. The first part is the Description, it is what the user will see
' in the Files of Type box, e.g. "Text Files (*.txt)". The second part is
' the actual filter, e.g. "*.txt". Each part and each filter must be
' separated by the vbNullChar. For example, to provide two filters, one for
' *.mdb files, and one for all files, use a statement like this:
'
' strFilter = "Access (*.mdb)" & vbNullChar & "*.mdb" _
' & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"
'
' Then pass your strFilter variable as the strFilter argument for the call
' to tsGetFileFromUser()
'
'.--------------------------------------------------------------------------
'.

Private Declare Function ts_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (tsFN As tsFileName) As Boolean

Private Declare Function ts_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (tsFN As tsFileName) As Boolean

Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

Private Type tsFileName
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

' Flag Constants
Public Const tscFNAllowMultiSelect = &H200
Public Const tscFNCreatePrompt = &H2000
Public Const tscFNExplorer = &H80000
Public Const tscFNExtensionDifferent = &H400
Public Const tscFNFileMustExist = &H1000
Public Const tscFNPathMustExist = &H800
Public Const tscFNNoValidate = &H100
Public Const tscFNHelpButton = &H10
Public Const tscFNHideReadOnly = &H4
Public Const tscFNLongNames = &H200000
Public Const tscFNNoLongNames = &H40000
Public Const tscFNNoChangeDir = &H8
Public Const tscFNReadOnly = &H1
Public Const tscFNOverwritePrompt = &H2
Public Const tscFNShareAware = &H4000
Public Const tscFNNoReadOnlyReturn = &H8000
Public Const tscFNNoDereferenceLinks = &H100000

Public Function tsGetFileFromUser( _
Optional ByRef rlngflags As Long = 0&, _
Optional ByVal strInitialDir As String = "", _
Optional ByVal strFilter As String = "All Files (*.*)" & vbNullChar &
"*.*", _
Optional ByVal lngFilterIndex As Long = 1, _
Optional ByVal strDefaultExt As String = "", _
Optional ByVal strFileName As String = "", _
Optional ByVal strDialogTitle As String = "", _
Optional ByVal fOpenFile As Boolean = True) As Variant

On Error GoTo tsGetFileFromUser_Err
Dim tsFN As tsFileName
Dim strFileTitle As String
Dim fResult As Boolean

' Allocate string space for the returned strings.
strFileName = Left(strFileName & String(256, 0), 256)
strFileTitle = String(256, 0)

' Set up the data structure before you call the function
With tsFN
.lStructSize = Len(tsFN)
.hwndOwner = Application.hWndAccessApp
.strFilter = strFilter
.nFilterIndex = lngFilterIndex
.strFile = strFileName
.nMaxFile = Len(strFileName)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = strDialogTitle
.flags = rlngflags
.strDefExt = strDefaultExt
.strInitialDir = strInitialDir
.hInstance = 0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
.lpfnHook = 0
End With

' Call the function in the windows API
If fOpenFile Then
fResult = ts_apiGetOpenFileName(tsFN)
Else
fResult = ts_apiGetSaveFileName(tsFN)
End If

' If the function call was successful, return the FileName chosen
' by the user. Otherwise return null. Note, the CancelError property
' used by the ActiveX Common Dialog control is not needed. If the
' user presses Cancel, this function will return Null.
If fResult Then
rlngflags = tsFN.flags
tsGetFileFromUser = tsTrimNull(tsFN.strFile)
Else
tsGetFileFromUser = Null
End If

tsGetFileFromUser_End:
On Error GoTo 0
Exit Function

tsGetFileFromUser_Err:
Beep
MsgBox Err.Description, , "Error: " & Err.Number _
& " in function basBrowseFiles.tsGetFileFromUser"
Resume tsGetFileFromUser_End

End Function

This code is working fine for me. I can point at the file location and then
select the file vs searching for it. However, I seen database examples out
there that would pull all the file names into a list box. I was thinking I
could pull all the filenames and have it automatically fill in all the
records. Example under a directory on hard drive C:\Inventory there are 10
files

So when I called the code above (or any other sugguested code) and navigated
to the Inventory directory and selected it, It would fill 10 records in the
table.

I guess I might have not explained myself correctly.

Someone mentioned the dir function, but only confused me.

Thanks Tom

Tom Wickerath said:
Hi Brian,

Perhaps you can use a modified version of this code sample to provide the
basis for what you are wanting to accomplish:

How to find the location or locations of a file in Microsoft Access
http://support.microsoft.com/?id=210613


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

BrianPaul said:
I have a table called Inventory.
I have 2 fields in the table 1. {file name} and the other {File location}

I have a form called {Flistfiles}

My question: I can call up the computers drive and files using the following
code.

Dim strFilter As String
Dim lngflags As Long
Dim varFileName As Variant

strFilter = "All Files (*.*)" & vbNullChar & "*.*" _
& vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"

lngflags = tscFNPathMustExist Or tscFNFileMustExist _
Or tscFNHideReadOnly

varFileName = tsGetFileFromUser( _
fOpenFile:=True, _
strFilter:=strFilter, _
rlngflags:=lngflags, _
strDialogTitle:="Please choose a file...")

If IsNull(varFileName) Then
Else
Me![File Location] = varFileName
End If

cmdAddSmall_End:
On Error GoTo 0
Exit Sub

cmdAddSmall_Err:
Beep
MsgBox Err.Description, , "Error: " & Err.Number _
& " in file"
Resume cmdAddSmall_End
End Sub

This puts the complete path and file name into my text box called {file
location} which the form is based on Tinventory table.

Now what I would like to do is have 1 command button that when I select to
the drive and folder would list all the file names and there locations and
just populate the complete table, thus; saving me from doing them one at a
time.
If possible what code would be needed. Thanks,
 
G

Guest

Hi Brian,

I'm going to assume that this question has been resolved in your later
posting:

http://www.microsoft.com/office/com...cess&mid=485ed782-78b7-4a6c-a9b6-2110114ff397


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

BrianPaul said:
Thanks Tom,

I'm sorry I didn't post the module in so you could see how the code worked.

Here it Is.
Option Compare Database
Option Explicit


'.=========================================================================
'.Browse Files Module
'.Copyright 1999 Tribble Software. All rights reserved.
'.Phone : (616) 455-2055
'.E-mail : (e-mail address removed)
'.=========================================================================
' DO NOT DELETE THE COMMENTS ABOVE. All other comments in this module
' may be deleted from production code, but lines above must remain.
'--------------------------------------------------------------------------
'.Description : This module calls directly into comdlg32.dll to allow user
'. to select a filename using the Windows Common Dialog. The
'. user may browse for a file, or enter a file name directly.
'.Written By : Carl Tribble
'.Date Created : 04/05/1999 09:56:31 AM
'.Rev. History :
' Comments : Normally, to use the Common Dialog you need to physically
' place the ActiveX control onto a form and then use code
' behind the form to implement its functionality. This
' module allows you to incorporate the functionality of the
' File Open/Save part of the Common Dialog without the
' ActiveX control itself. This module is completely self-
' contained. Simply copy it into your database to use it.
'.-------------------------------------------------------------------------
'.
' ADDITIONAL NOTES:
'
' This module only provides the Open/Save file dialog, not the other
' CommonDialog interfaces (ColorChooser, Help, PrintDialog, etc.)
'
' If you want your user to browse for folder names (paths) you must use
' the module basBrowseFolders instead.
'
' TO STREAMLINE this module for production programs, you should remove:
' 1) Unnecessary comments
' 2) Flag Constants which you do not intend to use.
' 3) The test procedure tsGetFileFromUserTest
'
'--------------------------------------------------------------------------
'
' INSTRUCTIONS:
'
' ( For a working example, open the Debug window )
' ( and enter tsGetFileFromUserTest. )
'
'.All the arguments for the function are optional. You may call it with no
'.arguments whatsoever and simply assign its return value to a variable of
'.the Variant type. For example:
'.
'. varFileName = tsGetFileFromUser()
'.
'.The function will return:
'. the full path and filename selected or entered by the user, or
'. Null if an error occurs or if the user presses Cancel.
'.
'.Optional arguments may include any of the following:
'. rlngFlags : one or more of the tscFN* constants (declared below)
'. Combine multiple constants like this:
'. tscFNHideReadOnly Or tscFNFileMustExist
'. strInitialDir : the directory to display when dialog opens
'. strFilter : a string containing any filters you want to use. Each
'. part must be separated by the vbNullChar. -example below
'. lngFilterIndex: a 1-based index indicating which filter to start with.
'. strDefaultExt : Extension to use if user does not enter one.
'. strFileName : Default File to display in the File Name text box.
'. strDialogTitle: Caption to display in the dialog's title bar.
'. fOpenFile : Boolean-True for the Open dialog, False for Save dialog.
'
' FILTER EXAMPLE: The filter must be a string containing two parts for each
' filter. The first part is the Description, it is what the user will see
' in the Files of Type box, e.g. "Text Files (*.txt)". The second part is
' the actual filter, e.g. "*.txt". Each part and each filter must be
' separated by the vbNullChar. For example, to provide two filters, one for
' *.mdb files, and one for all files, use a statement like this:
'
' strFilter = "Access (*.mdb)" & vbNullChar & "*.mdb" _
' & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"
'
' Then pass your strFilter variable as the strFilter argument for the call
' to tsGetFileFromUser()
'
'.--------------------------------------------------------------------------
'.

Private Declare Function ts_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (tsFN As tsFileName) As Boolean

Private Declare Function ts_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (tsFN As tsFileName) As Boolean

Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

Private Type tsFileName
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

' Flag Constants
Public Const tscFNAllowMultiSelect = &H200
Public Const tscFNCreatePrompt = &H2000
Public Const tscFNExplorer = &H80000
Public Const tscFNExtensionDifferent = &H400
Public Const tscFNFileMustExist = &H1000
Public Const tscFNPathMustExist = &H800
Public Const tscFNNoValidate = &H100
Public Const tscFNHelpButton = &H10
Public Const tscFNHideReadOnly = &H4
Public Const tscFNLongNames = &H200000
Public Const tscFNNoLongNames = &H40000
Public Const tscFNNoChangeDir = &H8
Public Const tscFNReadOnly = &H1
Public Const tscFNOverwritePrompt = &H2
Public Const tscFNShareAware = &H4000
Public Const tscFNNoReadOnlyReturn = &H8000
Public Const tscFNNoDereferenceLinks = &H100000

Public Function tsGetFileFromUser( _
Optional ByRef rlngflags As Long = 0&, _
Optional ByVal strInitialDir As String = "", _
Optional ByVal strFilter As String = "All Files (*.*)" & vbNullChar &
"*.*", _
Optional ByVal lngFilterIndex As Long = 1, _
Optional ByVal strDefaultExt As String = "", _
Optional ByVal strFileName As String = "", _
Optional ByVal strDialogTitle As String = "", _
Optional ByVal fOpenFile As Boolean = True) As Variant

On Error GoTo tsGetFileFromUser_Err
Dim tsFN As tsFileName
Dim strFileTitle As String
Dim fResult As Boolean

' Allocate string space for the returned strings.
strFileName = Left(strFileName & String(256, 0), 256)
strFileTitle = String(256, 0)

' Set up the data structure before you call the function
With tsFN
.lStructSize = Len(tsFN)
.hwndOwner = Application.hWndAccessApp
.strFilter = strFilter
.nFilterIndex = lngFilterIndex
.strFile = strFileName
.nMaxFile = Len(strFileName)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = strDialogTitle
.flags = rlngflags
.strDefExt = strDefaultExt
.strInitialDir = strInitialDir
.hInstance = 0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
.lpfnHook = 0
End With

' Call the function in the windows API
If fOpenFile Then
fResult = ts_apiGetOpenFileName(tsFN)
Else
fResult = ts_apiGetSaveFileName(tsFN)
End If

' If the function call was successful, return the FileName chosen
' by the user. Otherwise return null. Note, the CancelError property
' used by the ActiveX Common Dialog control is not needed. If the
' user presses Cancel, this function will return Null.
If fResult Then
rlngflags = tsFN.flags
tsGetFileFromUser = tsTrimNull(tsFN.strFile)
Else
tsGetFileFromUser = Null
End If

tsGetFileFromUser_End:
On Error GoTo 0
Exit Function

tsGetFileFromUser_Err:
Beep
MsgBox Err.Description, , "Error: " & Err.Number _
& " in function basBrowseFiles.tsGetFileFromUser"
Resume tsGetFileFromUser_End

End Function

This code is working fine for me. I can point at the file location and then
select the file vs searching for it. However, I seen database examples out
there that would pull all the file names into a list box. I was thinking I
could pull all the filenames and have it automatically fill in all the
records. Example under a directory on hard drive C:\Inventory there are 10
files

So when I called the code above (or any other sugguested code) and navigated
to the Inventory directory and selected it, It would fill 10 records in the
table.

I guess I might have not explained myself correctly.

Someone mentioned the dir function, but only confused me.

Thanks Tom

Tom Wickerath said:
Hi Brian,

Perhaps you can use a modified version of this code sample to provide the
basis for what you are wanting to accomplish:

How to find the location or locations of a file in Microsoft Access
http://support.microsoft.com/?id=210613


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

BrianPaul said:
I have a table called Inventory.
I have 2 fields in the table 1. {file name} and the other {File location}

I have a form called {Flistfiles}

My question: I can call up the computers drive and files using the following
code.

Dim strFilter As String
Dim lngflags As Long
Dim varFileName As Variant

strFilter = "All Files (*.*)" & vbNullChar & "*.*" _
& vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"

lngflags = tscFNPathMustExist Or tscFNFileMustExist _
Or tscFNHideReadOnly

varFileName = tsGetFileFromUser( _
fOpenFile:=True, _
strFilter:=strFilter, _
rlngflags:=lngflags, _
strDialogTitle:="Please choose a file...")

If IsNull(varFileName) Then
Else
Me![File Location] = varFileName
End If

cmdAddSmall_End:
On Error GoTo 0
Exit Sub

cmdAddSmall_Err:
Beep
MsgBox Err.Description, , "Error: " & Err.Number _
& " in file"
Resume cmdAddSmall_End
End Sub

This puts the complete path and file name into my text box called {file
location} which the form is based on Tinventory table.

Now what I would like to do is have 1 command button that when I select to
the drive and folder would list all the file names and there locations and
just populate the complete table, thus; saving me from doing them one at a
time.
If possible what code would be needed. Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top