By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,939 Members | 611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,939 IT Pros & Developers. It's quick & easy.

How import text file to MS Access vba

P: 1
Down my code is working.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub PR_Click()
  4. Dim intfileH   As Integer
  5.   Dim strBuf     As String
  6.   Dim rst        As DAO.Recordset
  7.   Dim strRec, vBuf, strRecDetail
  8.  
  9.   intfileH = FreeFile()
  10.   Open "D:\pr.txt" For Input As #intfileH
  11.   strBuf = Input(LOF(intfileH), #intfileH)
  12.   Set rst = CurrentDb.OpenRecordset("Table3")
  13.  
  14.   vBuf = Split(strBuf, "")
  15.   For Each strRec In vBuf
  16.      If strRec <> "" Then
  17.         strRecDetail = Split(strRec, vbCrLf)
  18.         With rst
  19.            .AddNew
  20.            !Hcod = Split(strRecDetail(0), ":")(1) 
  21.            !Nhour = Mid(strRecDetail(1), 27, 5)
  22.            !Ehour = Split(strRecDetail(1), "-")(1)
  23.            .Update
  24.         End With
  25.      End If
  26.   Next
  27.  
  28.   rst.Close
  29.   Close (intfileH)
  30.  
  31. End Sub
The code is working.
Apr 6 '17 #1
Share this Question
Share on Google+
4 Replies


jforbes
Expert 100+
P: 1,107
You might want to check out this thread: Text file to access Table
Apr 6 '17 #2

NeoPa
Expert Mod 15k+
P: 31,419
I can't make out what the question means but your code shows you need to read Require Variable Declaration.
Apr 6 '17 #3

P: 5
If your goal is to store some or all data that is in a text file in an Access database, you should import the contents of the file into a new table or append the data to an existing table. You can view and edit the imported data, and the changes you make to the data do not affect the source text file. Have a look on Microsoft blog which may help you.
Apr 14 '17 #4

ADezii
Expert 5K+
P: 8,623
If you could Post some Sample Data, or even Dummy Data representing the File's Structure, I'm sure we can be more helpful.
Apr 15 '17 #5

Post your reply

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