Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55

    Changing .DAT to .CSV to Automatically Import into Tables

    Hi, I relatively new to the forum and have a question.



    I have 3 files on a network share; FW_H1PD_OE-STRP1508252345220956.dat, FW_IMGC_RS-STRP1508252345220954.dat, & FW_ITXM_RS-STRP1508252345220955.dat which have a timestamp created daily, the numbers after STRP. I want to change them to; FW_H1PD_OE-STRP.cvs, FW_IMGC_RS-STRP.csv, & FW_ITXM_RS-STRP.csv.

    Then, I would like to import the files with the new names using the same import specs, move the original files to another folder and delete a 3 of them when I close the database.

    Your help is appreciated, Thanks
    Jay

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    you dont have to rename them to import them.
    Do you NEED them renamed for some other reason? they import fine as they are.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    put the code below into a module.
    (press ctl-G, on the menu bar, click INSERT, MODULE. paste the code, save as module1)

    to run the code,
    create a new macro
    for the action type RUNCODE
    for the function name type: LoadAllTextFiles()
    save

    Code:
    '----------------
    function LoadAllTextFiles()
    '----------------
         ScanAllFilesInDir "C:\folder\"
    end function 
    
    
    '----------------
    Public Sub ScanAllFilesInDir(ByVal pvDir)
    '----------------
    Dim vFil, vTargT
    Dim i As Integer
    Dim fso
    Dim oFolder, oFile
    Dim  vTbl
    
    On Error GoTo errImp
    If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(pvDir)
    
    For Each oFile In oFolder.Files
        vFil = pvDir & oFile.Name
        If InStr(vFil, ".dat") > 0 Then      'ONLY DO .DAT FILES
               Import1TextFile vFil
                  'DELETE THE FILE HERE
              kill vFil
        End If
    Next
    
    Set fso = Nothing
    Set oFile = Nothing
    Set oFolder = Nothing
    MsgBox "Done"
    Exit Sub
    
    errImp:
    MsgBox Err.Description, vbCritical, "clsImport:ImportData()" & Err
    Exit Sub
    Resume
    End Sub
    
      'the source file is copied to a generic file everytime
    '----------------
    Public Sub Import1TextFile(ByVal pvFile)
    '----------------
    Dim vDir, vTarg
    
      'make a copy
    vDir = "c:\ImportFiles\"
    MakeDir vDir
    vTarg = vDir & "File2Import.txt"
    
       'import data
    FileCopy pvFile, vTarg
    DoCmd.TransferText acExportDelim, "specName", "table", vTarg, True
    End Sub
    
    
    '----------------
    Public Sub MakeDir(ByVal pvDir)
    '----------------
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(pvDir) Then fso.CreateFolder pvDir     'MkDir pvDir
    Set fso = Nothing
    End Sub

  4. #4
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    No, renaming not need if I can import with the original name. Thought I had to as .dat file aren't support, I thought. I look at your next reply.

  5. #5
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    ranman256,
    Sorry, I'm still a novice with Macro and Modules and not sure what I'm sure where to replace everything. I created the Module and Macro, changed the function to; ScanAllFilesInDir "N:\REDSIII\STRIPEReceive\". But then I'm stuck.

  6. #6
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    Also, I need to strip off the first 15 characters of each file name, add the .csv, to match my import spec, do I?

    For example; This FW_H1PD_OE-STRP1508242345310739.dat is changed to FW_H1PD_OE-STRP.csv

  7. #7
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    Also, I need to only import these 3 files daily. Each file has it's own spec and respective table. FW_H1PD_OE-STRP.csv is imported to the [H1PD XRAY Data] table, FW_IMGC_RS-STRP.csv to [IMCG IMAGE Data], and FW_ITXM_RS-STRP.csv to [ITXM BLOOD Data].

  8. #8
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522

    then build 1 macro that imports THOSE 3 import commands

    TransferText acImportDelim, 'SPECname1', "H1PD XRAY Data", "c:\folder\FW_H1PD_OE-STRP.csv" ,true
    TransferText acImportDelim, 'SPECname2', "IMCG IMAGE Data", "c:\folder\FW_IMGC_RS-STRP.csv" ,true
    TransferText acImportDelim, 'SPECname3', "ITXM BLOOD Data", "c:\folder\FW_ITXM_RS-STRP.csv" ,true



  9. #9
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    I'm assuning I can place this in the sample that you sent me, because I need to check that location for those 3 original files, right? So, I need a statement that looks for FW_H1PD_OE-STRP* to create a FW_H1PD_OE-STRP.csv so I can run the TransferText statement, correct?

    Sorry, still learning. Thanks for all your help!

  10. #10
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    yes, but you MUST have the EXACT file path /name,
    if it keeps changing, then you must use the code: Import1TextFile(), i sent above.

    modified to:

    Code:
      'the source file is copied to a generic file everytime
    '----------------
    Public Sub Import1TextFile(ByVal pvFile)
    '----------------
    Dim vDir, vTarg
    
    
      'make a copy
    vDir = "c:\ImportFiles\"
    MakeDir vDir
    vTarg = vDir & "File2Import.txt"
    
    
       'import data
    FileCopy pvFile, vTarg
    
    
    SELECT CASE TRUE
       case instr(pvFile,"FW_H1PD_OE-STRP")>0 
          vSpecFile = "XRAYspec"
          vTbl = "tXray"
    
    
       case instr(pvFile,"FW_IMGC_RS-STRP")>0 
          vSpecFile = "IMGspec"
          vTbl = "tIMAGE"
    
    
       case instr(pvFile,"FW_ITXM_RS-STRP")>0 
          vSpecFile = "BLOODspec"
          vTbl = "tBLOOD"
    
    
       CASE ELSE
          exit sub
    end select
    
    DoCmd.TransferText acExportDelim, vSpecFile , vTbl, vTarg, True
    End Sub


  11. #11
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    The directory and files never change. The Directory is N:\REDSIII\STRIPEReceive\. The files are always; FW_H1PD_OE-STRP1508252345220956.dat, FW_IMGC_RS-STRP1508252345220954.dat, & FW_ITXM_RS-STRP1508252345220955.dat with only the timestamp part of the file name changing for each. So FW_H1PD_OE-STRP1508252345220956.dat ot FW_H1PD_OE-STRP.csv will work using inport spec Import-FW_H1PD_OE-STRP to [H1PD XRAY Data] table.

  12. #12
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    I'm having trouble learning how to do this. So, lets try this again.

    In C:\REDSIII\STRIPEReceive\, I have 3 files; FW_H1PD_OE-STRP1508252345220956.dat, FW_IMGC_RS-STRP1508252345220954.dat, & FW_ITXM_RS-STRP1508252345220955.dat with only the timestamp part of the file name changing for each. The location of the folder and files never never change, only the timestamp part of the file name changing for each.

    I would like to autimatically load these daily files into there respective tables using there respective import specs. The import specs are looking for files called; FW_H1PD_OE-STRP.csv, FW_IMGC_RS-STRP.csv, folder\FW_ITXM_RS-STRP.csv

    I would like to, either through a loop or by creating copies of these files from the .dat to .csv files so I can use the existing import specs. I would like to rename them, move the original .dat files to C:\REDSIII\ImportedFiles directory and delete the .csv file after importng.

    Ideally I would like to do this via a macro/module code, but having difficulty do so as I'm new to Access, Macro and Modules. I can do this manually, but would really like to create a automation process since these daily files will be coming until 5/31/16.

    Can you please help? Sorry if I'm not catching what has been shared so far.

    Thanks again!!

  13. #13
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    I guess I'm not getting any more help with this. I'll just keep doing the daily manual thing.

  14. #14
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    Well, i'm still at it. I think I'm entering this right, but only get a Done wihen stepping through.
    Here's my code; All 3 files; FW_H1PD_OE-STRP1508252345220956.dat, FW_IMGC_RS-STRP1508252345220954.dat, & FW_ITXM_RS-STRP1508252345220955.dat are in the directory daily.
    '----------------
    Function LoadAllTextFiles()
    '----------------
    ScanAllFilesInDir "C:\REDSIII\STRIPEReceive"
    End Function

    '----------------
    Public Sub ScanAllFilesInDir(ByVal pvDir)
    '----------------
    Dim vFil, vTargT
    Dim i As Integer
    Dim fso
    Dim oFolder, oFile
    Dim vTbl
    On Error GoTo errImp
    If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(pvDir)
    For Each oFile In oFolder.Files
    vFil = pvDir & oFile.Name
    If InStr(vFil, ".dat") > 0 Then 'ONLY DO .DAT FILES
    Import1TextFile vFil
    'DELETE THE FILE HERE
    Kill vFil
    End If
    Next
    Set fso = Nothing
    Set oFile = Nothing
    Set oFolder = Nothing
    MsgBox "Done"
    Exit Sub
    errImp:
    MsgBox Err.Description, vbCritical, "clsImport:ImportData()" & Err
    Exit Sub
    Resume
    End Sub
    'the source file is copied to a generic file everytime
    '----------------
    Public Sub Import1TextFile(ByVal pvFile)
    '----------------
    Dim vDir, vTarg
    'make a copy
    vDir = "C:\REDSIII\ImportedFiles\"
    MakeDir vDir
    vTarg = vDir & "File2Import.txt"

    'import data
    FileCopy pvFile, vTarg

    Select Case True
    Case InStr(pvFile, "FW_H1PD_OE-STRP") > 0
    vSpecFile = "H1PD-XRaySpec"
    vTbl = "H1PD XRAY Data"

    Case InStr(pvFile, "FW_IMGC_RS-STRP") > 0
    vSpecFile = "IMGC-ImageSpec"
    vTbl = "IMGC IMAGE Data"

    Case InStr(pvFile, "FW_ITXM_RS-STRP") > 0
    vSpecFile = "ITxM-BloodSpec"
    vTbl = "ITXM BLOOD Data"

    Case Else
    Exit Sub
    End Select
    DoCmd.TransferText acExportDelim, vSpecFile, vTbl, vTarg, True
    End Sub
    '----------------
    Public Sub MakeDir(ByVal pvDir)
    '----------------
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(pvDir) Then fso.CreateFolder pvDir 'MkDir pvDir
    Set fso = Nothing
    End Sub

  15. #15
    jjz0512 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    55
    Oh, I keep getting 'The test file specification 'H1PD-XRaySpec' does not exist. You cannot import, export, oir link using the specification.

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 06-13-2015, 11:05 AM
  2. Changing Environ field automatically in report.
    By usmanghani_2654 in forum Access
    Replies: 2
    Last Post: 02-15-2015, 11:32 AM
  3. automatically refresh import from file.
    By skan in forum Access
    Replies: 6
    Last Post: 09-08-2014, 11:12 AM
  4. Replies: 19
    Last Post: 08-28-2014, 01:13 AM
  5. Replies: 97
    Last Post: 05-24-2012, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums