Results 1 to 8 of 8

Thread: Need help getting data to excel from text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Little Rock AR
    Posts
    4

    Unhappy Need help getting data to excel from text file

    First off I am new to VB.NET, I took one course in college, but it is not helping me here... We did simple programs (1+1=2) type of things....


    I am trying to create an application that will read from a text tab delimited file and input certain data into an excel spreadsheet template.


    The text file has 6 columns (these titles of the columns do not appear in the text file)
    ID REF_ID RESPONSE LOCATION SURVEY_DATE SUBMIT_DATE
    Here is an example of the data......

    32993 6 1 AZ: Phoenix Retail Activation Center (RAC) 02/02/04 02/02/04

    I need code to basically say....

    If REF_ID = 2 and location = "HERE" and SURVEY_DATE is = to tbstart.txt then PLACE RESPONSE in excel document. Cells (14, 2)
    If REF_ID = 6 and location = "HERE" and SURVEY_DATE is = to tbstart.txt then PLACE RESPONSE in excel document. Cells (3, 2)

    And so on and so on....

    I have tried several different things and nothing is working.



  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    here is a routine I wrote in vb6 (I haven't done anything with it in .net yet). It takes a text file, reads a header section and that section instructs the program as to where to parse information and where to send it. Very much like what you're needing. feel free to alter this code to match your needs. Hope it helps.


    VB Code:
    1. Private Sub btnImport_Click()
    2.  
    3. '**********************************************************************************
    4. 'Variables
    5. Dim Handle As Integer
    6. Dim Start, Length, p, HeaderSize As Long
    7. Dim Tmp, Field, Filename, ConnStr As String
    8. Dim HeaderLines As Long
    9. Dim Point As Integer
    10. Dim i As Integer
    11. Dim db As New ADODB.Connection
    12. Dim rs As New ADODB.Recordset
    13.  
    14.  
    15.  
    16. 'set variables*********************************************************************
    17.     For i = 0 To FileSelect.ListCount - 1
    18.         If (FileSelect.Selected(i) = True) Then Filename = DirSelect.Path & "\" & FileSelect.List(i)
    19.         DoEvents
    20.     Next i
    21.  
    22.     HeaderLines = 0
    23.     HeaderSize = 0
    24.     Handle = FreeFile()
    25.  
    26.     If (Filename = "cvsale.src") Then
    27.         TableName = "sale" 'change this name
    28.     Else
    29.         TableName = "changeme"
    30.     End If
    31.  
    32. ConnStr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=admin;Initial Catalog=Everest;Data Source=PC1284"
    33.  
    34.     db.Open (ConnStr)
    35.     rs.Open "SELECT * FROM " + Table(0).TableName + ";", db, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic
    36. '**********************************************************************************
    37.  
    38. Open (Filename) For Input As #Handle
    39.  
    40.    'get header info****************************************************************
    41.    i = 0
    42.     Do While (Not EOF(Handle))
    43.         DoEvents
    44.         Line Input #Handle, Tmp
    45.         If (Left(Tmp, 1) = "%") Then
    46.             HeaderLines = HeaderLines + 1
    47.             HeaderSize = HeaderSize + Len(Tmp) + 2
    48.             Tmp = Mid(Tmp, 2) 'this takes out the '%' character
    49.             p = InStr(1, Tmp, " ") 'this reads from 1st char to the 1st 'space'
    50.             If (p) Then
    51.             'set the header info into variables
    52.                 CvcallHeader(i).FieldName = Trim(Mid(Tmp, 1, p - 1))
    53.                 CvcallHeader(i).Offset = Val(Mid(Trim(Mid(Tmp, p)), 1, InStr(1, Trim(Mid(Tmp, p)), " "))) + 1
    54.                 CvcallHeader(i).Length = Val(Right(Tmp, 3))
    55.             End If
    56.            
    57.             i = i + 1
    58.         Else
    59.             Exit Do
    60.         End If
    61. 'start getting data from file*******************************************************
    62.     Loop
    63.    
    64.     Do While (Not EOF(Handle))
    65.         DoEvents
    66.        
    67.         rs.AddNew
    68.             rs("TELEPHONE_MEMBER").Value = Mid(Tmp, CvcallHeader(0).Offset, CvcallHeader(0).Length)
    69.             rs("TELEPHONE_NUMBER").Value = Mid(Tmp, CvcallHeader(1).Offset, CvcallHeader(1).Length)
    70.             rs("MEMBER_NUMBER").Value = Mid(Tmp, CvcallHeader(2).Offset, CvcallHeader(2).Length)
    71.             rs("LISTED_NAME").Value = Mid(Tmp, CvcallHeader(4).Offset, CvcallHeader(4).Length)
    72.         rs.Update
    73.        
    74.         Line Input #Handle, Tmp
    75.         If (EOF(Handle)) Then
    76.             rs.AddNew
    77.                 rs("TELEPHONE_MEMBER").Value = Mid(Tmp, CvcallHeader(0).Offset, CvcallHeader(0).Length)
    78.                 rs("TELEPHONE_NUMBER").Value = Mid(Tmp, CvcallHeader(1).Offset, CvcallHeader(1).Length)
    79.                 rs("MEMBER_NUMBER").Value = Mid(Tmp, CvcallHeader(2).Offset, CvcallHeader(2).Length)
    80.                 rs("LISTED_NAME").Value = Mid(Tmp, CvcallHeader(4).Offset, CvcallHeader(4).Length)
    81.             rs.Update
    82.             Exit Do
    83.         End If
    84.     Loop
    85.    
    86.    
    87.    
    88.     Close (Handle)
    89.    
    90.    
    91.    
    92.     'Debug.Print (Name & vbCrLf & Start & vbCrLf & Length)
    93.    
    94. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Little Rock AR
    Posts
    4
    I have go the program reading now... but there is something wrong with my array and I can not figure it out.


    Code:
    Private Sub mnRetailWeekly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnRetailWeekly.Click
            Dim line As String, i As Integer
            'open file
            FileOpen(1, "C:\UnityReports\Weekly1.txt", OpenMode.Input)
            'read each line, seperate by :: and add to variable "line"
            Do While Not EOF(1)
                'temp = Split(LineInput(1), "\t")
                line = line & "::" & LineInput(1) & "::"
            Loop
            'close file
            FileClose(1)
            '-----------------------------------------
            '----- Open excel file
            '-----------------------------------------
            'setup excel doc
            xlApp = New Excel.Application()
            xlApp.Visible = True
            'location of template
            xlFile = "C:\UnityReports\Lakewood.xls"
            xlApp.Workbooks.Open(xlFile)
            '--------------------------------------------------------------------------------------
            '----- write data from tab delminated file to excel doc
            '--------------------------------------------------------------------------------------
            'write date of survey, based on date picked in calendar
            xlApp.Cells(1, 1) = "Weekly Unity Survey Summary for Week of: " & tbEnd.Text
            'split data from file by line,check date, send to function to write data
            '----------------------------------------
            '-----  Write v2 data to excel
            '---------------------------------------
            Dim v2_data As Array, temp_v2_data As Array, tempData As String
            v2_data = Split(line, "::")
            For i = 0 To UBound(v2_data)
                If (Len(v2_data(i)) > 0) Then
                    tempData = v2_data(i)
                    temp_v2_data = Split(tempData, vbTab)
                    If (temp_v2_data(2) = compare_date) Then
                        WriteWeekly(temp_v2_data)
                    End If
                End If
            Next
        End Sub
    The red area is where my program stops to be dbuged. I dont know whats wrong.

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    what error does it give you?

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    VB Code:
    1. Dim v2_data As Array, temp_v2_data As Array, tempData As String

    actually, this MAY help: instead of declaring v2_data as an array type, try this:
    VB Code:
    1. dim v2_data() as string
    , and the same for
    VB Code:
    1. temp_v2_data() as string
    . that declares them as arrays. Ive never used (or even seen) the array data type (obviousely it exists ) but that MAY be the problem because you're trying to work with string data.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Little Rock AR
    Posts
    4
    I have tried that and I still get the same error message
    " Index was outside the bounds of the array"

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    For i = 0 To UBound(v2_data) -1

    try that out. Since it's zero based, you will need to subtract one to keep it in bounds. can't garuntee that'll fix it but after looking over your code and the error, that makes logical sense to me now.

  8. #8
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045
    Alloyd7:

    Feb was a long time ago ... did you find your solution?

    Of course, if you split a string into an array of substrings, if there is a null string, that element of the array is not created and references to it are out of bounds.

    All you needed was a "redim" to the appropriate number elements for the array.

    redim temp_v2_data(n)

    where "n" is the appropriate or maximum number of elements in the array. This forces the array to have an element "n" even though it contains a null.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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