Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 5th, 2004, 04:29 PM
Bigtallim Bigtallim is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Indiana
Posts: 23 Bigtallim User rank is Corporal (100 - 500 Reputation Level)Bigtallim User rank is Corporal (100 - 500 Reputation Level)Bigtallim User rank is Corporal (100 - 500 Reputation Level)Bigtallim User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 50 m 17 sec
Reputation Power: 0
Send a message via AIM to Bigtallim
Question conversion: *.*.txt to *.*.xls or *.*mdb

My client has an MS-Dos program that outputs the following data, I would like to take this data and input it into Microsoft Access(r) so I can manipulate the data and make reports. I have tried to convert it to .xls (Excell) I was able to do so by rows but not by columns. "A" column conatined the entire row is there a way to get it to divide it into columns as it is seperated by columns in a text file.

attached is a demo of the data

I appreciate all your help!!
Attached Files
File Type: txt demo.txt (982 Bytes, 307 views)

Reply With Quote
  #2  
Old January 6th, 2004, 12:29 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
You could do this with ADO (requires a reference to Microsoft Active Data Objects x.x) with some code like this:
Code:
Private Sub Command1_Click()
Dim conImport As ADODB.Connection
Dim lngFileNumber As Long
Dim strLine As String
Dim varFields As Variant

' Create and open database object.
Set conImport = New ADODB.Connection
' Change this to reflect your server and database names.
conImport.Open "Provider=SQLOLEDB; Data Source=IS-DEV1; Initial Catalog=PaulsTestData; Integrated Security=SSPI;Persist Security Info=False"

' Get a valid file handle.
lngFileNumber = FreeFile
' Open file (change this path for your text file).
Open "e:\temp\test.txt" For Input As #lngFileNumber

' Grab lines of text until end of file.
Do While Not EOF(lngFileNumber)
' Grab a line from the file.
Line Input #lngFileNumber, strLine
' Split the line into fields based on the space character.
varFields = Split(strLine, " ")
' Insert the fields into the database table (Change the table and field names below to match yours).
' NOTE that numeric field values don't need to be surrounded by single quotes and both of the fields below are being treated as string/text values.
conImport.Execute "INSERT INTO Test3 (Field1, Field2) VALUES ('" & varFields(0) & "', '" & varFields(1) & "')"
Loop

' Close the text file.
Close #lngFileNumber

' Close and destroy database object.
conImport.Close
Set conImport = Nothing

MsgBox "Finished!", vbInformation, "Import"

End Sub

Basically what this does is go through a text file line by line breaking each line into two fields based on the space character and then inserting the fields into a SQL Server 7 table using a SQL INSERT statement via the execute method of the connection object.

Reply With Quote
  #3  
Old January 6th, 2004, 12:30 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
Maybe u need it:Copy data from an Excel spreadsheet into an Access database.
Using Excel as a server, open the spreadsheet. Use this code to find the largest rows and columns used.
Code:
    max_row = excel_sheet.UsedRange.Rows.Count
    max_col = excel_sheet.UsedRange.Columns.Count
Use ADO to open the database. 

For each row in the Excel spreadsheet, loop through the row's columns composing an SQL INSERT statement. Use the ADO Connection object to execute the statement and create the record. 
 
 
 
Private Sub cmdLoad_Click()
Dim excel_app As Object
Dim excel_sheet As Object
Dim max_row As Integer
Dim max_col As Integer
Dim row As Integer
Dim col As Integer
Dim conn As ADODB.Connection
Dim statement As String
Dim new_value As String

    Screen.MousePointer = vbHourglass
    DoEvents

    ' Create the Excel application.
    Set excel_app = CreateObject("Excel.Application")

    ' Uncomment this line to make Excel visible.
'    excel_app.Visible = True

    ' Open the Excel spreadsheet.
    excel_app.Workbooks.Open FileName:=txtExcelFile.Text

    ' Check for later versions.
    If Val(excel_app.Application.Version) >= 8 Then
        Set excel_sheet = excel_app.ActiveSheet
    Else
        Set excel_sheet = excel_app
    End If

    ' Get the last used row and column.
    max_row = excel_sheet.UsedRange.Rows.Count
    max_col = excel_sheet.UsedRange.Columns.Count

    ' Open the Access database.
    Set conn = New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & txtAccessFile.Text & ";" & _
        "Persist Security Info=False"
    conn.Open

    ' Loop through the Excel spreadsheet rows,
    ' skipping the first row which contains
    ' the column headers.
    For row = 2 To max_row
        ' Compose an INSERT statement.
        statement = "INSERT INTO Books VALUES ("
        For col = 1 To max_col
            If col > 1 Then statement = statement & ","
            new_value = Trim$(excel_sheet.Cells(row, _
                col).Value)
            If IsNumeric(new_value) Then
                statement = statement & _
                    new_value
            Else
                statement = statement & _
                    "'" & _
                    new_value & _
                    "'"
            End If
        Next col
        statement = statement & ")"

        ' Execute the INSERT statement.
        conn.Execute statement, , adCmdText
    Next row

    ' Close the database.
    conn.Close
    Set conn = Nothing

    ' Comment the Close and Quit lines to keep
    ' Excel running so you can see it.

    ' Close the workbook saving changes.
    excel_app.ActiveWorkbook.Close True
    excel_app.Quit

    Set excel_sheet = Nothing
    Set excel_app = Nothing

    Screen.MousePointer = vbDefault
    MsgBox "Copied " & Format$(max_row - 1) & " values."
End Sub

Reply With Quote
  #4  
Old January 6th, 2004, 03:19 PM
Bigtallim Bigtallim is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Indiana
Posts: 23 Bigtallim User rank is Corporal (100 - 500 Reputation Level)Bigtallim User rank is Corporal (100 - 500 Reputation Level)Bigtallim User rank is Corporal (100 - 500 Reputation Level)Bigtallim User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 50 m 17 sec
Reputation Power: 0
Send a message via AIM to Bigtallim
thank you

I appreciate your help, that is a much more thourough explanation than I planned on. One quick exit question if I may.
I am new at using visual basic.net, should I attempt this with .net or stay with 6.0???

Reply With Quote
  #5  
Old January 6th, 2004, 08:12 PM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
.Net is better at the cross platform as java..If u are a newer at vb..I recommend u to learn .net...It will give u more benefit..

Reply With Quote
  #6  
Old January 14th, 2004, 10:38 AM
gjlinker gjlinker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Manchester, UK
Posts: 23 gjlinker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You could keep it simpel and do the following:

1. load data into Excel
Use File|Open and open your text file
If the wizard does not start automatically use Data->Text to Columns. Use this wizard to load the data using fixed field and not delimiters.

2. once in Excel, use SQL*XL to load the data into your preferred database being Access, Oracle, SQL Server or anything else. It will be able to pick the data up from Excel and just put it into the database without any coding. If you want you can automate it all in Excel macros. SQL*XL supports Excel's macro recording.

You could actually do it all in SQL*XL using the text odbc driver but that would be more complex than using the import wizard.

You find out about SQL*XL at www.oraxcel.com.
Note you would need SQL*XL 3 and SQL*XL ADO

Best regards, Gerrit-Jan Linker
Linker IT Consulting Limited
www.oraxcel.com

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > conversion: *.*.txt to *.*.xls or *.*mdb


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT