?
Solved

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

Posted on 2008-10-30
6
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 1500 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

762 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