|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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!! |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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
|
|
#4
|
|||
|
|||
|
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??? |
|
#5
|
|||
|
|||
|
.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..
|
|
#6
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > conversion: *.*.txt to *.*.xls or *.*mdb |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|