Good morning all,
I have been attempting to write a rather complicated (for me at least) VBA code that exports data from fields in an Access Form to an Excel Spreadsheet. The form has many entries, as such my ultimate goal is to be able to navigate to each record, click the event button and have Access export the record to the spreadsheet, entering the data in the next empty cells/columns. I have the code that selects which sheet in the workbook to used based on a 'Rel_Date' field in the form. The issue I am having is that clicking the event button works for one record in the form, but when I navigate to the next record, the code opens the Spreadsheet again (via the file path) rather than just add the data to the correct sheet. I am forced to save the workbook, close it and then click the event button again. The sheets also only support 2 entries; once the event button is used thrice, it overwrites the second record in the sheet. Additionally, the data is being added to the bottom of the table in the sheet rather than the top of table, then populating the data below it. I've pieced together the following code from bits of online help and the greatest minds within my organisation and have gone bust! Any help would be greatly appreciated. If my description of my grievances is unclear (likely), please feel free to ask questions! Thank you all.
Private Sub Command23_Click()
Dim objXLApp As Object
Dim objXLBook As Object
Dim i As Integer
Dim tempstr As String
Dim tempSheet As Worksheet
Form_TrackApprovals.RelDate.SetFocus "Form_TrackApprovals is the name of the Form"
tempstr = Form_TrackApprovals.RelDate.Value "This is the function that directs the data to the appropriate sheet in the workbook, based on the 'Rel_Date' field in the Form"
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open( _
\\Usrdata3.highmark.com\home3dir1\LID9P9B\Approval Tracker Proj\Approval tracker\MNINAppTrPractice.xlsx) "This is the file path to the workbook, saved on a local drive for now"
objXLApp.Application.Visible = True
Set tempSheet = objXLBook.Sheets(tempstr)
With objXLBook.Sheets(tempstr)
tempSheet.Select
i = .Range("A2").End(xlDown).Row + 1 "This line of code appears to be...wrong. It does not resolve when entered; it was copied and pasted from a different forum"
.Cells(i, 1).Value = Form_TrackApprovals.STI__ITI__or_Other_
.Cells(i, 2).Value = Form_TrackApprovals.Project_Manager
.Cells(i, 3).Value = Form_TrackApprovals.DocTitle1
.Cells(i, 4).Value = Form_TrackApprovals.Sender_Name
.Cells(i, 5).Value = Form_TrackApprovals.Due_Date
.Cells(i, 6).Value = Form_TrackApprovals.Created
.Cells(i, 7).Value = Form_TrackApprovals.Subject
End With
Set tempSheet = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
End Sub
I have been attempting to write a rather complicated (for me at least) VBA code that exports data from fields in an Access Form to an Excel Spreadsheet. The form has many entries, as such my ultimate goal is to be able to navigate to each record, click the event button and have Access export the record to the spreadsheet, entering the data in the next empty cells/columns. I have the code that selects which sheet in the workbook to used based on a 'Rel_Date' field in the form. The issue I am having is that clicking the event button works for one record in the form, but when I navigate to the next record, the code opens the Spreadsheet again (via the file path) rather than just add the data to the correct sheet. I am forced to save the workbook, close it and then click the event button again. The sheets also only support 2 entries; once the event button is used thrice, it overwrites the second record in the sheet. Additionally, the data is being added to the bottom of the table in the sheet rather than the top of table, then populating the data below it. I've pieced together the following code from bits of online help and the greatest minds within my organisation and have gone bust! Any help would be greatly appreciated. If my description of my grievances is unclear (likely), please feel free to ask questions! Thank you all.
Private Sub Command23_Click()
Dim objXLApp As Object
Dim objXLBook As Object
Dim i As Integer
Dim tempstr As String
Dim tempSheet As Worksheet
Form_TrackApprovals.RelDate.SetFocus "Form_TrackApprovals is the name of the Form"
tempstr = Form_TrackApprovals.RelDate.Value "This is the function that directs the data to the appropriate sheet in the workbook, based on the 'Rel_Date' field in the Form"
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open( _
\\Usrdata3.highmark.com\home3dir1\LID9P9B\Approval Tracker Proj\Approval tracker\MNINAppTrPractice.xlsx) "This is the file path to the workbook, saved on a local drive for now"
objXLApp.Application.Visible = True
Set tempSheet = objXLBook.Sheets(tempstr)
With objXLBook.Sheets(tempstr)
tempSheet.Select
i = .Range("A2").End(xlDown).Row + 1 "This line of code appears to be...wrong. It does not resolve when entered; it was copied and pasted from a different forum"
.Cells(i, 1).Value = Form_TrackApprovals.STI__ITI__or_Other_
.Cells(i, 2).Value = Form_TrackApprovals.Project_Manager
.Cells(i, 3).Value = Form_TrackApprovals.DocTitle1
.Cells(i, 4).Value = Form_TrackApprovals.Sender_Name
.Cells(i, 5).Value = Form_TrackApprovals.Due_Date
.Cells(i, 6).Value = Form_TrackApprovals.Created
.Cells(i, 7).Value = Form_TrackApprovals.Subject
End With
Set tempSheet = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
End Sub