Solved

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

Posted on 2008-10-30
6
206 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Record open by another user 6 74
Caste datetime 2 69
Strange msg in the SSMS pane 13 60
SQL parsing XML works but want to do it another way 4 22
There are some very powerful Data 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 discu…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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