问题
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