I'm trying to transfer the value of a control on an Access form to an Excel worksheet using VBA. The Access form is a single form with all the controls disabled, so that data is "read only." I have a button on the form which opens the Excel worksheet, which then basically serves as the input form for the Access database.
What I need is to somehow get the primary key (EstimateID) of the current record on the Access form transferred to the Excel worksheet, then use ADO and ".find" to locate that record in the Access form's query and gather the rest of the data.
Right now my code is in an Excel module that I run in the "Workbook_Open" event.
Expand|Select|Wrap|Line Numbers
- Private Const DatabasePath As String = "C:\Documents and Settings\Owner\My Documents\Storm Shield Database.mdb"
- Dim strEstimateID As String
- Sub TransferFromDatabase()
- Dim AdConnection As ADODB.Connection, Estimate As ADODB.Recordset
- ' Connect to the Access database
- AdConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" &_
- "Data Source= " & DatabasePath & "; Persist Security Info=False"
- ' Open a recordset
- Estimate.Open "qryEstimates", AdConnection
- ' Find the current estimate based on the EstimateID
- Estimate.Find "EstimateID = " & strEstimateID
- ' Add a new estimate if an existing one isn't found
- If EOF = True Then Estimate.AddNew
- ' Fill the appropriate cells with data from the database
- ActiveSheet.Cells(I6) = Estimate.Fields(EstimateID).Value
- *
- *
- *
- ' Close the recordset and connection
- Estimate.Close
- AdConnection.Close
- Set Estimate = Nothing
- Set AdConnection = Nothing
- End Sub
I'm sure there are errors in the code, and the code is not finished, but I can't debug it until I find a method to get the primary key transferred into Excel. If you spot error, feel free to point them out, but my focus is really on the problem of the primary key.
...Not sure if it's relevant, but here is the code for the Access button which opens the Excel worksheet...
Expand|Select|Wrap|Line Numbers
- Private Sub btnOpenEstimate_Click()
- ' Set path and filename of the estimate sheet to open
- strEstimateSheetPath = "I:\Miscellaneous\Database Files\Estimate Sheets\"
- strEstimateSheetName = ProjectName & " -" & Str(EstimateID) & ".xls"
- ' If new record: open new estimate sheet -- else: open corresponding estimate sheet
- If IsNull(EstimateID) Then
- Call ShellExecute(0, "", strEstimateSheetPath & "Estimate Sheet.xls", "", "", 2)
- Else
- Call ShellExecute(0, "", strEstimateSheetPath & strEstimateSheetName, "", "", 2)
- End If
- End Sub
I'm not a programmer (well...2 years of Basic in HS :-) ), so keep that in mind in your answers.
P.S. - I could probably use the clipboard to transfer the value of the control on the Access form, but I want to avoid that if possible.