Solved

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

Posted on 2008-10-30
6
202 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

20 Experts available now in Live!

Get 1:1 Help Now