Fordraiders
asked on
Import data from text file to sql server via field by field
Access 2010 VBA
sql server 2008
What I have:
I have code that works fine for a Bulk Insert. from access
What I need:
I need to read each field mapping the column in the text file to the field in the table.
Because I may have to special format fields
For Example: The data may contain 33.45223222
and i only need the field to accept 33.5( rounding it off to 2 decimal places.
Thanks
fordraiders
sql server 2008
What I have:
I have code that works fine for a Bulk Insert. from access
What I need:
I need to read each field mapping the column in the text file to the field in the table.
Because I may have to special format fields
For Example: The data may contain 33.45223222
and i only need the field to accept 33.5( rounding it off to 2 decimal places.
Dim fname As String
fname = "C:\Program Files\Enterprise\EXPORT_iQ.txt"
strSQL = "BULK INSERT dbo.tblImportiQ_Exported FROM '" & fname & "' WITH ( FIELDTERMINATOR = '|', ROWTERMINATOR = '\n',KEEPNULLS )"
Dim objCommand As SqlCommand
objCommand = New SqlCommand(strSQL, objConn)
objCommand.CommandText = strSQL
objCommand.ExecuteNonQuery()
fname = Dir()
Thanks
fordraiders
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
pat, Can't Link the file...will always be a different place on each persons pc..I have a dialog search box already created.
Yes you can link he file. That isn't a problem. You just give the user the file dialog so the user can navigate to the file. Do NOT use the newfangled link. Do it the old way where you can actually change the source of the file.
Use the file dialog box to locate the file. Then in your code, use TransferText to link to the file. As long as you give it the same "table" name each time you link, all your queries will continue to function correctly.
Use the file dialog box to locate the file. Then in your code, use TransferText to link to the file. As long as you give it the same "table" name each time you link, all your queries will continue to function correctly.
I wouldn't go with the text linking...just code to select the file and perform what is needed...probably your application is already too cluttered to add one more linked object
ASKER
I ended up using my previous code and it worked fine.
With xlw
Set xls = xlw.Worksheets("Template")
With xls
.Columns.AutoFit
lastColumn = 19
While Len(.Cells(lastRow, 11)) > 0
lastRow = lastRow + 1
Wend
lastRow = lastRow - 1
For i = 5 To lastRow
For j = 11 To lastColumn
CellDataOrig = CellDataOrig + Trim(.Cells(i, j).Value) + "|"
Next
CellData = ID_RESPONSE + "|" + CellDataOrig + Trim(.Cells(i, j).Value) + "|" + "1" + "|"
' remove any characters % $
CellData = Replace(CellData, "%", "")
CellData = Replace(CellData, "$", "")
CellData = Replace(CellData, "#", "")
oFile.WriteLine (CellData)
CellData = vbNullString
CellDataOrig = vbNullString
Next
End With
End With
oFile.Close
Set fso = Nothing
Set oFile = Nothing
xlw.Close SaveChanges:=False
xlx.Quit
ASKER
i did not mean to close it
ASKER
Thanks all
ASKER
Found this in my older questions:
Looking to do this..
The issue is reading the text file and import correctly sql server table..
I just want to be able to set a format if i have to on a field...
similar to this code:
Open in new window
This may work...
Going to try and revise this code.
fordraiders