I have data from a series of Excel VBA calculations stored as variables in the VBA code. What I want to do at the end of the code is to transfer the data from these variables into an MS Access database as a new record.
The Access database already exists and is ready. Do I have to put the data in the variables into cells on a worksheet before I can transfer them to Access?
Here are an example of the variables in the VBA code that I am trying to store in the database (variable name corresponds to the database column heading)
Dim customerName As String
Dim customerAge As Integer
Dim customerSpend As Double
customerName = "Tim"
customerAge = 26
customerSpend = 12876
This is the code I've managed to write so far to try and transfer the data, but I'm stuck at the 'Range' part, as they are variables and not a range of cells:
Dim strPath As String
Dim objAccess As Object
strPath = "C:\db1.accdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.OpenCurrentDatabase(strPath)
objAccess.Visible = True
Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel12, CustomerDetails, Range)
How do I transfer the data stored in the variables in the VBA code into the corresponding columns in the Access database to make a new record?
DoCmd.TransferSpreadsheetis an Access thing, yet you're creating a late-bound Access application (why???).TransferSpreadsheetdoesn't transfer variables or data, it transfers a spreadsheet, so it wants aRangethat contains the data you want to "transfer". Dump the variables into a worksheet, transfer that worksheet. - Mathieu GuindonINSERTquery that takes a parameter for every value you want to insert, and invoke that. There's tons of ways to do this. - Mathieu Guindon