简体   繁体   中英

Excel-VBA code that moves Excel sheets to Microsoft access?

I was wondering if it was possible to move data from an excel sheet and store it in a Microsoft Access datbase. I have a lot of sheets of data with a similar format, and I would like a table for each of them in access. I would also like to retrieve data from the database, but i figure I should learn how to store data first. I found this code, I don't know if someone could explain how it works( Or if it is nothing like what I'm looking for)? I have read power programming in excel with vba, so I know basic vba, but not this database content(Probably more).

Public Sub DoTrans()



  Set cn = CreateObject("ADODB.Connection")
  dbPath = Application.ActiveWorkbook.Path & "\FDData.mdb"
  dbWb = Application.ActiveWorkbook.FullName
  dbWs = Application.ActiveSheet.Name
  scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath
  dsh = "[" & Application.ActiveSheet.Name & "$]"
  cn.Open scn

  ssql = "INSERT INTO fdFolio ([fdName], [fdOne], [fdTwo]) "
  ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh


  cn.Execute ssql


End Sub

Also if you have any book recommendations that would cover this/links, that would also be appreciated.

I'm sure it can be done in Excel, but I don't know it off the top of my head.

But it's fairly easy to do in Access (also uses VBA). Look at the TransferSpreadsheet method. If you combine it with saved import specs, it can do a lot.

You also have the choice of importing the data into a new table, or you can just link to the spreadsheet and have it act like a table. Linking is useful when you don't want all the spreadsheet info and want to query it.

Here's a link on the command syntax: http://msdn.microsoft.com/en-us/library/office/ff844793(v=office.14).aspx

The code that you have found transfers data to an already existing database named FDData.mdb that is probably already set up to look exactly like your excel worksheet. Can I ask why you don't just use Access? It is easier to use VBA to create excel sheets from Access than it is to do the opposite. There is also the import database from excel worksheet feature in Access, are you trying to automate this process for a vast number of excel worksheets? Otherwise you are better off just using the wizard. We might be able to help more if you can tell us exactly what you are trying to do, linking up Excel and Access via VBA might be more counterproductive than just picking one and dealing with the downsides unless you are prepared to write a whole lot of code.

Since you know about accessing cell value in your excel and know how to access an AccessDB as recordset in VBA, this wont be too hard for you ..

I'm sure Google will give you nice direction for it !

And I found this link for you .. http://www.ozgrid.com/forum/showthread.php?t=76110

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM