473,473 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Import multible text files into Access table

61 New Member
I am trying to import multiple text files into an Access table. I build a Form with a button, named "Import_Deals". In the On click Event trigger I entered the following code, but it don't import my files. can someone please help?

Thanks,
Mike
Expand|Select|Wrap|Line Numbers
  1. Private Sub Import_Deals_Click()
  2.  
  3.     Dim strPath As String
  4.     Dim strFile As String
  5.     Dim strTable As String
  6.     Dim strSpecification As String
  7.     Dim intImportType As AcTextTransferType
  8.     Dim blnHasFieldNames As Boolean
  9.     Dim objFileDialog As Office.FileDialog
  10.     Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFolderPicker)
  11.     ' Modify these values as needed
  12.     strTable = "EERS_Deals"
  13.     strSpecification = "EERS_Deals"
  14.     blnHasFieldNames = False
  15.     intImportType = acImportDelim
  16.     ' Let user select a folder
  17.     With objFileDialog
  18.         .AllowMultiSelect = True
  19.         .ButtonName = "Folder Picker"
  20.         .Title = "Folder Picker"
  21.         If (.SelectedItems.Count > 0) Then
  22.             Call MsgBox(.SelectedItems(1))
  23.         ElseIf .Show > 0 Then
  24.         End If
  25.     End With
  26.     If Right(strPath, 1) <> "\" Then
  27.         strPath = strPath & "\"
  28.     End If
  29.     ' Loop through the text files
  30.     strFile = Dir(strPath & "*.txt")
  31.     Do While strFile <> ""
  32.         ' Import text file
  33.         DoCmd.TransferText _
  34.             TransferType:=intImportType, _
  35.             SpecificationName:=strSpecification, _
  36.             TableName:=strTable, _
  37.             FileName:=strPath & strFile, _
  38.             HasFieldNames:=blnHasFieldNames
  39.         strFile = Dir
  40.     Loop
  41. End Sub
Aug 28 '15 #1
4 3003
Seth Schrock
2,965 Recognized Expert Specialist
Do you get an error message running this code or does nothing happen?
Aug 28 '15 #2
mburch2000
61 New Member
When I run it from the VBE window, the Macro MsgBox pops up apparently looking for a Macro. I finally realized that my settings under File/Option/Trust Center Settings/Macros Settings needed to default to Enable All Macros. I got something to work before I left, but I will not be able to test anything else for two days when I return. Thanks for your help. The above code still did not work like I wanted though. I really need to select all files in directory, then upload then into an existing table in Access.
Aug 29 '15 #3
cPmod
6 New Member
Hi Mike,

As far as I understood you simply want to process all text files in one directory or something like that processed, right?

Following code (it is excel vba, my apologies) did this job for me (I know it is probably a bit dirty coding but it worked like a charm converting a lot of pdf files to txt).

Expand|Select|Wrap|Line Numbers
  1.     Dim i As Integer
  2.     strTXT As String
  3.     Dim FSO As Object, objSFold As Object, objWks As Object, tmp As Object
  4.     Dim colPFiles As New Collection, colTFiles As New Collection
  5.     Dim Multi
  6.     Dim strManuf As String
  7.     Dim strFoldername As String
  8.  
  9.  
  10.  
  11.     While colPFiles.Count <> 0
  12.         colPFiles.Remove (colPFiles.Count)
  13.     Wend
  14.  
  15.     strFoldername = ""
  16.     strManuf = ""
  17.  
  18.     strFoldername = ThisWorkbook.Path & "\" & strManuf
  19.     Set objSFold = FSO.GetFolder(strFoldername)
  20.     Set objWks = ThisWorkbook.Sheets(1)                            
  21.     For Each tmp In objSFold.Files  ' read in all Filenames  
  22.         If Right(tmp.Path, 4) = ".pdf" Then colPFiles.Add tmp.Path  ' only *.pdf
  23.     Next tmp
  24.  
  25.     For i = 1 To colPFiles.Count
  26.     'Perform Task! -> the Files can be individually addressed one after another using 'colPFiles.Item(i)' 
  27.     Next
  28. 'MsgBox "Job finished!"
  29. End Sub
As I said, unfortunately I made this code work in excel vba and it therefore might need minor modifications (however, the data manipulation is done outside excel!) but it might guide you to the right path and you may modify that example for your task.

If you have further questions just let me know.

Cheers
Aug 29 '15 #4
ADezii
8,834 Recognized Expert Expert
The Code will never work in its current context. See Revised Code below along with a Demo Upload that should point you in the right direction.
Expand|Select|Wrap|Line Numbers
  1. Dim strPath As String
  2. Dim strFile As String
  3. Dim strTable As String
  4. Dim strSpecification As String
  5. Dim intImportType As AcTextTransferType
  6. Dim blnHasFieldNames As Boolean
  7. Dim objFileDialog As Office.FileDialog
  8.  
  9. Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFolderPicker)
  10.  
  11. '**** Modify these values as needed ****
  12. strTable = "EERS_Deals"
  13. strSpecification = "EERS_Deals"
  14. blnHasFieldNames = False
  15. intImportType = acImportDelim
  16. '***************************************
  17.  
  18. 'Let user select a folder
  19. With objFileDialog
  20.   .AllowMultiSelect = False     'Meaningless, but set anyway
  21.   .ButtonName = "Folder Picker"
  22.   .Title = "Folder Picker"
  23.      If .Show Then
  24.        strPath = .SelectedItems(1) & "\"
  25.  
  26.        'Loop through the text files
  27.        strFile = Dir(strPath & "*.txt")
  28.          Do While strFile <> ""
  29.            'For Testing purposes only
  30.            DoCmd.TransferText intImportType, , strTable, strPath & strFile, False
  31.          strFile = Dir
  32.          Loop
  33.      End If
  34. End With
Attached Files
File Type: zip Import Multiple Text Files_Revised.zip (27.4 KB, 141 views)
Aug 29 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: JMCN | last post by:
i had some problems trying to alter this code in order to import different text files into the access 97 program. it works properly however the users will be selecting different types of text...
5
by: AnDrE | last post by:
Hello I'm developing a application in VB .NET 2005 and I need to access text files. I'm using the System.IO.File library and I have this code: Dim oFile As System.IO.File Dim oRead As...
4
by: chimambo | last post by:
I have 2 problems: 1. I want to import a single text file into an access table using a Macro. I am however getting an error that I need to put a specification name argument. What does this mean?...
1
by: ehcy | last post by:
hello guys.. how can i display the <DIR> in my opened files from my text file with table?.. htmlspecialchars is useless.. it always "Warning: file_get_contents() expects parameter 1 to be string,...
1
by: jollyroger | last post by:
I have searched the web forums, and can't seem to find an answer to this particular problem I have. In an excel sheet, cells in one column have formatted text in the "wrapped" cells. For many of...
3
by: nspader | last post by:
Hello All, This seems like it should be simple but I cannot figure it out. I am trying to set up a button click to import an excel file to a temp table then run an append query to add it to an...
6
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
5
by: vvasude2 | last post by:
Hi all, How can i import text files with same specefication into one table I should be able to select multiple text files and import them automatically in the table. The import specefication...
18
by: rajeevs | last post by:
Hi All Again stuck with some issues I have a log file as attached sample with 2 records. What I am looking is to read the file line by line from MS ACCESS Database and import the records to 4...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.