Solved

How can data from Excel be imported to a SQL Server Database?

Posted on 2008-10-30
6
203 Views
Last Modified: 2012-05-05
Hi Experts,

How can data from Excel be imported to a SQL Server Database programmatically?

This is my VBA code to do it in Access database but how can it be done in SQL server database?

Thanks

Option Compare Database
 

Public Sub Import_BW_Data()
 

Dim rs As DAO.Recordset

Dim db As DAO.Database

Dim tbl As TableDef
 
 

Dim strSQL As String

Dim strFilename As String

Dim ExecDataUpdate As Long
 

Dim PathIs As String
 

'Detect path

PathIs = Application.CurrentProject.Path
 

Set db = CurrentDb()
 

DoCmd.SetWarnings (False)
 

strSQL = "DELETE FROM [BW_FTT]"

db.Execute (strSQL)
 

strFilename = Dir$(PathIs & "\UploadedFiles\BW_FTT\*.xls")
 

While (Not (Len(Trim$(strFilename))) = 0)
 

    DoEvents
 

    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "BW_FTT_Import", PathIs & "\UploadedFiles\BW_FTT\" & strFilename, False

     

    'Append

    DoCmd.OpenQuery ("Append to BW_FTT")

 

    DoCmd.DeleteObject acTable, "BW_FTT_Import"   'Delete table

    

    FileCopy "" & PathIs & "\UploadedFiles\BW_FTT\" & strFilename & "", "" & PathIs & "\ImportedFiles\BW_FTT\" & strFilename & ""
 

    Kill "" & PathIs & "\UploadedFiles\BW_FTT\" & strFilename & ""

    

    strFilename = Dir$()
 

Wend
 
 

DoCmd.SetWarnings (True)
 

MsgBox ("BW FTT Data Imported")
 

End Sub

Open in new window

0
Comment
Question by:indyng
  • 2
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:PedroCGD
ID: 22842960
You can do that also using SSIS (new version of DTS)
What is your doubt in you code?
Regards!
pedro
0
 
LVL 2

Expert Comment

by:BobTheViolent
ID: 22843350
To elaborate on what pedro said.  You can set up a DTS or SSIS package using the wizard or just building it yourself and you specify a connection to an xls file and tell it what sql queries or tables to pull from it etc...   Then you can call the package from a stored procedure, query or SQL server agent.  You could even call the package from Access VBA code.

"All Tasks" -> "Import Data" command if you right click while in the Enterprise Manager looking at the Tables view or with"Action" -> "All Tasks" -> "Import Data" from the top command bar.  Or just go to "Local Packages"  and create a new DTS or SSIS.
0
 
LVL 1

Author Comment

by:indyng
ID: 22844730
I don't think i can because i am running SQL Server Express 2005
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 2

Expert Comment

by:BobTheViolent
ID: 22844987
On msdn there is a BCP utility documented that can transfer files or tables.  Maybe this would work for you.  http://msdn.microsoft.com/en-us/library/ms162802.aspx

I didn't realize "Express" did not have DTS/SSIS.  It looks like you may have to stick with pushing the data from Access to the SQL Server.

Is there a particular reason you do not want to do the transfer with VBA like you originally posted?
0
 
LVL 22

Accepted Solution

by:
PedroCGD earned 500 total points
ID: 22849098
SQL Server 2005 Express Edition does not include Integration Services (SSIS) or support for SQL Server 2000 DTS packages.

To read text file from SQL Server try this:
http://www.databasejournal.com/features/mssql/article.php/3584751
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1559168&SiteID=1
http://doc.ddart.net/mssql/sql70/sp_adda_17.htm

If you use OPENROWSET or OPENDATASOURCE you must enable that in SQL Configuration Surface Area (Features)

Helped?
Regards!
Pedro
SQL-Surface.JPG
0
 
LVL 1

Author Comment

by:indyng
ID: 22849850
BobTheViolent:

Can I do the transfer with VBA in SQL server? I thought i had to do the transfer in access and then push it to SQL server express 2005.

Thanks
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now