Results 1 to 23 of 23

Thread: [RESOLVED] Data From Text File To Database Table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Resolved [RESOLVED] Data From Text File To Database Table

    Hello again!

    I have a problem. I have an adodc that is connected to a a table in the database. CommandType -> adCmdTabe...... and then i select the table that i want! The problem is that i have a text file which has important data that i want to store to a table in the database. The order inside the txt file is like this:

    1st row-> 05/29/2007 14:43:36 242 6.58 0.98 1660 31.3 2.50 2.58 2.64 2.68

    2nd row-> 05/29/2007 14:45:40 240 3.03 0.85 668 31.3 1.19 1.22 1.22 1.23

    3rd row->....................


    740 row ->...................

    I have made a table in SQL server with 11 fields. Can anybody post some kind of code so i can read the file(EOF) and save every data into the proper field without using any array in the client(VB 6)? The fields in the database are with the same order as in the file(date time ........)!

    Thanks!

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Data From Text File To Database Table

    Something like sample below may work for you - just kim that it is not tested.
    Code:
    Private Sub Command1_Click()
    '================================
    Dim strConString As String
    Dim adoConn As ADODB.Connection
    Dim strLine As String
    Dim arValues() As String
    Dim i As Integer
    Dim strSql As String
    
        strConString = "valid connection string goes here"
        Set adoConn = New ADODB.Connection
        adoConn.Open strConString
        
        Open some_file For Input As #1
            Line Input #1, strLine
            arValues() = Split(strLine, Space(1))
            
            strSql = "Insert Into MyTable" & vbNewLine
            strSql = strSql & "    (Field1, Field2, Field3, Field4)" & vbNewLine 'and so on for each remaining field
            strSql = strSql & "Values" & vbNewLine
            strSql = strSql & "    (" & vbNewLine
            strSql = strSql & "    '" & arValues(0) & "'," & vbNewLine 'date
            strSql = strSql & "    '" & arValues(1) & "'," & vbNewLine 'time
            strSql = strSql & "     " & arValues(2) & "," & vbNewLine 'first numeric value
            strSql = strSql & "     " & arValues(3) & "" & vbNewLine  'next numeric value
            'and so on for each remaining value
            strSql = strSql & "    )"
            
            adoConn.Execute strSql
            
        Close #1
        
        adoConn.Close
        Set adoConn = Nothing
    
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    Thanks i will try it and let you know! If someone has another example please post it.

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Data From Text File To Database Table

    That's quite good, what RB posted

    Except, you should be using FreeFile() instead of fixed numbers when working with files. Eg:
    Code:
    1. Dim intFF As Integer
    2.  
    3. intFF = FreeFile
    4.  
    5. Open some_file For Input As #intFF
    6. '...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    I have some problems with the code you posted. In the arValues(0) i have the first date ->05/29/2007 but then in the arValues(1) i have all the rest ->14:43:36 242 6.58 0.98 1660 31.3 2.50 2.58 2.64 2.68. And then i also get a message subscript out of range in the arValues(2). I aslo tryed to store only the date without any other number but that didn't workef either.

    What do you think should i change in the code??? Any ideas??

    This is how it should be stored in the database table:

    Date Time Voltage Amber.................and so on
    05/29/2007 14:43:36 242 6.58

    Every data in it's proper field!

    Thanks!

  6. #6

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    I undertand. The date and time is seperated by 1 space character but all other are separated by 4 and 5 space characters. Isn't there any way that i can make it work?

    Thanks!

  8. #8

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    I'm converting a hexadecimal packet of data into decimal. I'm trying to make the space bettwen the fields with 1 space character. If you have any other idea please tell me. i 'll let you know!

    Thanks!

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    I HAve created a file with eleven numbers (1 2 3 4 5 6 7 8 9 10 11) and made it this way. Why does it not work??? I get some strange error"multiple steps error or something" Strange?? Any ideas?

    vb Code:
    1. Dim strLine As String
    2. Dim arValues() As String
    3. Dim i As Integer
    4.  
    5. ReDim arValues(0 To 10)
    6.  
    7. MsgBox (Adodc1.Recordset.Fields.Count)
    8.    
    9.     Open "C://One.txt" For Input As #1
    10.         Line Input #1, strLine
    11.         arValues() = Split(strLine, Space(1))
    12.    
    13.     With Adodc1
    14.        .Recordset.AddNew
    15.      
    16.       For i = 1 To 11
    17.         .Recordset.Fields(i) = arValues(i - 1)
    18.       Next i
    19.    
    20.        .Recordset.Update
    21.     End With
    22.    
    23.     Close #1

  11. #11

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    So the way i tryed to do it is wrong. Ok let's leave it. I used your code with the simple text file(1 2 3 4 5 6 7 8 9 10 11) that i made and it still doesn't work. All have one space character between them. What is there to change?

    Thanks!

  13. #13

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    Nothing much because i don't undertand the code very well. This is what i did:

    Code:
    Dim strConString As String
    Dim adoConn As ADODB.Connection
    Dim strLine As String
    Dim arValues() As String
    Dim i As Integer
    Dim strSql As String
    
        strConString = Adodc1.ConnectionString
        Set adoConn = New ADODB.Connection
        adoConn.Open strConString
        
        Open "C://One.txt" For Input As #1
            Line Input #1, strLine
            arValues() = Split(strLine, Space(1))
            
            strSql = "Insert Into MyTable" & vbNewLine
            strSql = strSql & "    (Field1, Field2, Field3, Field4)" & vbNewLine 'and so on for each remaining field
            strSql = strSql & "Values" & vbNewLine
            strSql = strSql & "    (" & vbNewLine
            strSql = strSql & "    '" & arValues(0) & "'," & vbNewLine 'date
            strSql = strSql & "    '" & arValues(1) & "'," & vbNewLine 'time
            strSql = strSql & "     " & arValues(2) & "," & vbNewLine 'first numeric value
            strSql = strSql & "     " & arValues(3) & "" & vbNewLine  'next numeric value
            strSql = strSql & "     " & arValues(4) & "," & vbNewLine
            strSql = strSql & "     " & arValues(5) & "," & vbNewLine
            strSql = strSql & "     " & arValues(6) & "," & vbNewLine
            strSql = strSql & "     " & arValues(7) & "," & vbNewLine
            strSql = strSql & "     " & arValues(8) & "," & vbNewLine
            strSql = strSql & "     " & arValues(9) & "," & vbNewLine
            strSql = strSql & "     " & arValues(10) & "," & vbNewLine
            'and so on for each remaining value
            strSql = strSql & "    )"
            
            adoConn.Execute strSql
            
        Close #1
        
        adoConn.Close
        Set adoConn = Nothing

  15. #15
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Data From Text File To Database Table

    OK,
    Field1, Field2, Field3, Field4 must be replaced with actual fields names that you created (you said you have 11 of them).

    arValues(3) & "" >> should really be arValues(3) & ","

    The very last value in the sql should not end with comma so replace what you have with this:

    arValues(10) & vbNewLine

    Although vbNewLine is not necessry I personally prefer to have it - it makes final sql much more readable when it needs to be debugged.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    I will make the changes and see what happens! I'll let you know!

    Thanks!

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    Yes it works with the simple text file! Very Good! Now i will see with the other one that has 4 and 5 spaces between the fields. Let's see what happens.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    Yes the problem is now with the spaces. It doesn't work with the important text file. Is there any way to replace spaces of 2 or bigger with 1 space character in the strLine so that the strLine contains it in the proper form???

  19. #19
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Data From Text File To Database Table

    You can try using Replace function to replace Space(2) (and so Space(3 or 4)) with Space(1) few times before string finally gets splitted on a single space.
    You can even do that in the loop:
    Code:
    Dim i%
    
            Line Input #1, strLine
            
            For i = 1 To 10
                strLine = Replace(strLine, Space(2), Space(1))
            Next i
            
            arValues() = Split(strLine, Space(1))
            '...

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    Oh i made a mistake. Between the date and time there is 1 space character. But between all others there is a Tab space between them and not space characters! Sorry! How can i make it work???

  21. #21

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    322

    Re: Data From Text File To Database Table

    Ok it works! If i have any other problem i will post it here!

    Thanks a lot for your help!

  23. #23

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width