How do I export a single record from Access to specific cells in Excel

家住魔仙堡 提交于 2020-01-07 04:34:24

问题


I am trying to export the record displayed on a form in Access to an Excel template which I would then rename and use the data in further calculations. I want to click a button on the form to transfer the record to Excel, I rename it and be done. The data fields will need to go to specific cells in the spreadsheet. Each record represents a seasoning formula with fields as such: Formula_name Formula_number Date_entered Ingredient1 Amount1 Ingredient2 Amount2 Ingredient3 And so on

The amounts are a percent of total amount. Ingredients and amounts need to be in columns. I will send the newly named excel sheet (Batch Sheet) to a pc in production for pulling, weighing and lot code recording. I have looked at vba but don’t have time to learn it in time for this project so I am hoping to just cut and paste the code or better-yet email the database and excel template to someone much smarter than me and have it working when returned. $$ Also can it work with different versions of MS Office. Thanks.


回答1:


This is not exactly a trivial 'cut and paste' problem unless you really want a simple solution without any customization.

Here is a link to an article that shows how to do it: http://www.accessibledatasolutions.com/articles11/AccessToExcel.htm




回答2:


In general you need to use something like the following code. You need to adjust the paths and then the SQL statement and you can put the extracted values from Access wherever you want. Maybe you can start with this and get some easy SQL extraction running.

Sub ConnectDatabase()

Dim con As New ADODB.Connection
Dim connected As Boolean
Dim rstAnswer As New ADODB.Recordset
Dim RootPath, DBPath As String
Dim tempString As String

connected = False

RootPath = "C:\Test\"
DBPath = RootPath & "Test.accdb"
con.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBPath & ";"

connected = True

rstAnswer.Open "SELECT CustomerName From tblCustomers " & _
    "WHERE tblCustomers.Country = '" & Denmark & "';", con, adOpenKeyset, adLockOptimistic

Do Until rstAnswer.EOF
    tempString = CStr(rstAnswer!CustomerName)
    Application.ActiveWorkbook.Worksheets("Sheet1").Range("A1").Value = tempString
    rstAnswer.MoveNext
Loop

rstAnswer.Close
con.Close   
connected = False

End Sub


来源:https://stackoverflow.com/questions/20810306/how-do-i-export-a-single-record-from-access-to-specific-cells-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!