Import text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Darkside12
    New Member
    • Jan 2008
    • 13

    #1

    Import text

    Hi,

    What's the quickest way of importing text into SQL Server when importing data using Access Project (.adp)?

    By the way, I can't use DoCmd.TransferT ext as the files don't have headers.

    Current Code:

    Code:
     
    Private Sub ImpData_Click()
    Dim cncurrent As ADODB.Connection
    Set cncurrent = CurrentProject.Connection
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    'sql to open table connection
    Dim strsql As String
    strsql = "Select * from [T Table]"
    'opening the record set
    rs.Open strsql, cncurrent, adOpenDynamic, adLockOptimistic
    Dim column(10) As String ' holder for the text data
    Dim linedata As String
    Dim i As Integer
    Open Me.Path For Input As #1 ' open file for input
    Do Until EOF(1) 'scan through file inputing data to the dataset from the Array
    Do Until i = 10
    Input #1, linedata ' input a line of data from the text file
    column(i) = linedata
    i = i + 1
    Loop
    i = 0
    rs.AddNew
    Do Until i = 10
    rs.Fields(i) = column(i)
    i = i + 1
    Loop
    rs.Update
    i = 0
    Loop
    End Sub
    This code works fine but the text files that will be imported are around 50Mb each. It takes a long time for this code to get through 50Mb!!!!

    Regards,

    Dark
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    I use DoCmd.TransferT ext for tab delimited data without column headings. Have you tried an import specification?

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by Darkside12
      Hi,

      What's the quickest way of importing text into SQL Server when importing data using Access Project (.adp)?

      By the way, I can't use DoCmd.TransferT ext as the files don't have headers.

      Current Code:

      Code:
       
      Private Sub ImpData_Click()
      Dim cncurrent As ADODB.Connection
      Set cncurrent = CurrentProject.Connection
      Dim rs As ADODB.Recordset
      Set rs = New ADODB.Recordset
      'sql to open table connection
      Dim strsql As String
      strsql = "Select * from [T Table]"
      'opening the record set
      rs.Open strsql, cncurrent, adOpenDynamic, adLockOptimistic
      Dim column(10) As String ' holder for the text data
      Dim linedata As String
      Dim i As Integer
      Open Me.Path For Input As #1 ' open file for input
      Do Until EOF(1) 'scan through file inputing data to the dataset from the Array
      Do Until i = 10
      Input #1, linedata ' input a line of data from the text file
      column(i) = linedata
      i = i + 1
      Loop
      i = 0
      rs.AddNew
      Do Until i = 10
      rs.Fields(i) = column(i)
      i = i + 1
      Loop
      rs.Update
      i = 0
      Loop
      End Sub
      This code works fine but the text files that will be imported are around 50Mb each. It takes a long time for this code to get through 50Mb!!!!

      Regards,

      Dark
      I'm a little confused by the logic, but would something like this work?
      Code:
      Dim cncurrent As ADODB.Connection
      Dim rs As ADODB.Recordset
      Dim strsql As String
      Dim linedata As String
      Dim i As Integer
      
      Set cncurrent = CurrentProject.Connection
      Set rs = New ADODB.Recordset
      
      'SQL to open table connection
      strsql = "Select * from [T Table]"
      
      'Opening the Recordset
      rs.Open strsql, cncurrent, adOpenDynamic, adLockOptimistic
      
      Open Me.Path For Input As #1 ' open file for input
      
      i = 0
      
      Do Until EOF(1)
        Do Until i = 10
          Input #1, linedata ' input a line of data from the text file
            rs.AddNew
              rs.Fields(i) = linedata
            rs.Update
            i = i + 1
        Loop
          i = 0
      Loop
      
      Close #1
      
      rs.Close
      Set rs = Nothing

      Comment

      • Darkside12
        New Member
        • Jan 2008
        • 13

        #4
        Hello again,

        Thanks for the responses,

        Originally posted by ChipR
        I use DoCmd.TransferT ext for tab delimited data without column headings. Have you tried an import specification?
        ChipR,

        Great suggestion, as was my first thought when I was trying to over come this. Unfortunately Microsoft seem to have forgotten to add the import specification feature to MS Access Project ;-)


        Originally posted by ADezii
        I'm a little confused by the logic, but would something like this work?
        Code:
        Dim cncurrent As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim strsql As String
        Dim linedata As String
        Dim i As Integer
        
        Set cncurrent = CurrentProject.Connection
        Set rs = New ADODB.Recordset
        
        'SQL to open table connection
        strsql = "Select * from [T Table]"
        
        'Opening the Recordset
        rs.Open strsql, cncurrent, adOpenDynamic, adLockOptimistic
        
        Open Me.Path For Input As #1 ' open file for input
        
        i = 0
        
        Do Until EOF(1)
          Do Until i = 10
            Input #1, linedata ' input a line of data from the text file
              rs.AddNew
                rs.Fields(i) = linedata
              rs.Update
              i = i + 1
          Loop
            i = 0
        Loop
        
        Close #1
        
        rs.Close
        Set rs = Nothing
        ADezii,

        Your code is way better than mine and should cut down on the time it takes to process the file. (Thank You!!!)

        But I was looking for a different method for importing the text file. Something other than reading the text file into a recordset (as per my code) or transfer text which I can't do due to the lack of a import specification function in Access Project (.adp).

        I'd be really greatful for any ideas.

        p.s. is there a way to access the SQL Server import Export wizard from VBA?
        Just had that thought.

        Thanks again for your responces,

        Regards,

        Dark

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          How about Plan B, DTS (Data transformation Services), which can bring the Text Based Data directly into SQL Server? I'll provide a brief overview of the process:
          1. Programs
          2. Microsoft SQL Server
          3. Import/Ex[port Data
          4. DTS Import/Export Wizard
          5. Set the Data Source = Text File
          6. Select a File name (*.txt)
          7. Specify certain Parameters regarding the Source File Format
          8. Specify the Column Delimiter
          9. Select the Destination SQL Server, Database, and means of Authentication
          10. Select Views to copy and Column Mappings if required
          11. Save/Execute the DTS Package

          Comment

          • Darkside12
            New Member
            • Jan 2008
            • 13

            #6
            Originally posted by ADezii
            How about Plan B, DTS (Data transformation Services), which can bring the Text Based Data directly into SQL Server? I'll provide a brief overview of the process:
            1. Programs
            2. Microsoft SQL Server
            3. Import/Ex[port Data
            4. DTS Import/Export Wizard
            5. Set the Data Source = Text File
            6. Select a File name (*.txt)
            7. Specify certain Parameters regarding the Source File Format
            8. Specify the Column Delimiter
            9. Select the Destination SQL Server, Database, and means of Authentication
            10. Select Views to copy and Column Mappings if required
            11. Save/Execute the DTS Package
            Hi Again ADezii,

            Yup, I can use DTS via remote access to import data (and do regularly) as I'm admin. Unfortunately the rest of my team don't have that kind of access.

            Is there any way of controling DTS from VBA?

            Regards,

            Dark

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by Darkside12
              Hi Again ADezii,

              Yup, I can use DTS via remote access to import data (and do regularly) as I'm admin. Unfortunately the rest of my team don't have that kind of access.

              Is there any way of controling DTS from VBA?

              Regards,

              Dark
              Here is some sample code that I dug up that will execute a DTS Package via VBA. To execute a package from Visual Basic is a relatively simple task using the DTS object model. Before you start using the object model you must add a the appropriate reference to your project. From the Project menu select References and check the "Microsoft DTSPackage Object Library".

              Here is a very simple example that uses integrated security to load the package MyPackage and then execute it:
              Code:
              Private Sub SimpleExecutePackage()
              Dim oPKG As New DTS.Package
                oPKG.LoadFromSQLServer "MyServer", , , _
                DTSSQLStgFlag_UseTrustedConnection, , , , "MyPackage"
                oPKG.Execute
                oPKG.UnInitialize
                Set oPKG = Nothing
              End Sub
              Before going any further the one drawback to using Visual Basic is that it is apartment threaded and DTS is free threaded. This can cause exception access violations. The simple way top overcome this is to ensure all steps execute on the main package thread. To do this set the ExecuteInMainTh read property as illustrated below:
              Code:
              ' Set Exec on Main Thread
              For Each oStep In oPKG.Steps
                oStep.ExecuteInMainThread = True
              Next
              Whilst the simple example above will do the job of executing your package, you will probably want some more information about the status of execution and any errors that occurred. The first option is to check the execution result (ExecutionResul t property) for each step. If this indicates failure (DTSStepExecRes ult_Failure) then you can go on to retrieve the full error number, source and description using the GetExecutionErr orInfo method for the step.

              Comment

              • ChipR
                Recognized Expert Top Contributor
                • Jul 2008
                • 1289

                #8
                Can you import excel files? I think most delimited text could be put in an excel spreadsheet and saved with an automated process.

                Comment

                • Darkside12
                  New Member
                  • Jan 2008
                  • 13

                  #9
                  Originally posted by ChipR
                  Can you import excel files? I think most delimited text could be put in an excel spreadsheet and saved with an automated process.

                  Hi Again,

                  Sorry for the late responce.

                  ChipR,

                  I could have imported a spread sheet but unfortunately the text files were about 500,000 lines + in size. Might have still worked if I had office 2007 to hand in the office but we're still stuck on 2000 here.

                  Originally posted by ADezii
                  Here is some sample code that I dug up that will execute a DTS Package via VBA. To execute a package from Visual Basic is a relatively simple task using the DTS object model. Before you start using the object model you must add a the appropriate reference to your project. From the Project menu select References and check the "Microsoft DTSPackage Object Library".

                  Here is a very simple example that uses integrated security to load the package MyPackage and then execute it:
                  Code:
                  Private Sub SimpleExecutePackage()
                  Dim oPKG As New DTS.Package
                    oPKG.LoadFromSQLServer "MyServer", , , _
                    DTSSQLStgFlag_UseTrustedConnection, , , , "MyPackage"
                    oPKG.Execute
                    oPKG.UnInitialize
                    Set oPKG = Nothing
                  End Sub
                  Before going any further the one drawback to using Visual Basic is that it is apartment threaded and DTS is free threaded. This can cause exception access violations. The simple way top overcome this is to ensure all steps execute on the main package thread. To do this set the ExecuteInMainTh read property as illustrated below:
                  Code:
                  ' Set Exec on Main Thread
                  For Each oStep In oPKG.Steps
                    oStep.ExecuteInMainThread = True
                  Next
                  Whilst the simple example above will do the job of executing your package, you will probably want some more information about the status of execution and any errors that occurred. The first option is to check the execution result (ExecutionResul t property) for each step. If this indicates failure (DTSStepExecRes ult_Failure) then you can go on to retrieve the full error number, source and description using the GetExecutionErr orInfo method for the step.
                  Adezii,

                  I found the same code as you ;-)

                  In the end I didn't use it and went for some code that was on msdn. I got the code to work fine but........... you needed to reference a SQL Server dll. Looked for our copy of SQL Server (So I could install the client tools on the workstations that would run the ap) and couldn't find the Disk :-(

                  Gave up, built the whole solution in VB.NET without a backend database. It takes no more than a minute to plow through 500,000 records.

                  Thanks again for all your help with this.

                  Dark

                  Comment

                  Working...