T
Tom S
I am running into two problems attempting to load data from an Excel
Spreadsheet into Access.
1. Access is locking the file open when the code is run
2. The program abends with run-time error code '9' - "subscript out of
range error"
The Excel workbook is set up with the sheet names on the 1st sheet and the
spreadsheet is updating frequently with new sheets being added or existing
sheets being updated. I am attempting to read in the sheet names on the 1st
sheet into an array, then determine if the sheet is new or just an update.
If it is new, then it would add the sheet information to the database,
otherwise it would just update the data in Access. Here's the code:
Public Function import_rlc()
Dim strFileName As String, strTab As String, strSql As String, strCell As
String
Dim strCell2 As String, strAdd As String
Dim xlsApp As Object
Dim xlsWB1 As Object
Dim xlsWS1 As Object
Dim xlsWS2 As Object
Dim intCol As Integer
Dim intRow As Integer
Dim blnProject As Boolean, blnNewP As Boolean
Dim dblCell As Double
Dim rst1 As Recordset, rst2 As Recordset
Dim col As Integer
Dim row As Integer
strFileName = "c:\RLC.xls"
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = False
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Set xlsWS1 = xlsWB1.Worksheets("Index")
MaxRow = 200
MaxCol = 15
ReDim CaseArray(MaxRow, MaxCol)
For row = 1 To MaxRow
For col = 1 To MaxCol
CaseArray(row, col) = xlsWS1.cells(row, col).Value
Next
Next
xlsWB1.Close
set xlsApp = Nothing
intRow = 1
Do Until intRow = 200
strTab = CaseArray(intRow, 1)
GoSub check_tab
If blnProject = True Then
Set xlsApp = CreateObject("Excel.Application")
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Set xlsWS2 = xlsWB1.Worksheets(strTab)
GoSub import_tab
xlsWB1.Close
Set xlsWB1 = Nothing
Set xlsWS2 = Nothing
Set xlsApp = Nothing
End If
intRow = intRow + 1
Loop
Exit Function
check_tab: 'check to see if the cell value is valid project
blnProject = True
strSql = "SELECT Tab_Name FROM tbl_NonProject_Tabs WHERE Tab_Name = "
strSql = strSql & "'" & strTab & "'"
'tbl_NonProject_Tabs lists those tabs that are not real projects
Set rst1 = CurrentDb.OpenRecordset(strSql)
If rst1.EOF = False Then
blnProject = False 'if value is in table then not a project
rst1.Close
End If
If strTab = "" Then
blnProject = False 'if value is blank then not a project
End If
Return
check_new_proj: 'check to see if the project exists on database
blnNewP = False
strSql = "Select * FROM tbl_projects WHERE project_code = '"
strSql = strSql & strCell & "'"
Set rst1 = CurrentDb.OpenRecordset(strSql)
If rst1.EOF = True Then
blnNewP = True
End If
rst1.Close
Return
add_proj: 'add new project to database
Set rst2 = CurrentDb.OpenRecordset("tbl_projects", dbOpenDynaset)
rst2.AddNew
rst2![project_code] = strCell
rst2![project_description] = strCell2
rst2![Active] = True
rst2.Update
rst2.Close
Return
import_tab: 'read data from tab and add new data
strCell = xlsWS2.cells(2, 4).Value
strCell2 = xlsWS2.cells(2, 5).Value
GoSub check_new_proj 'check to see if project exists in database
If blnNewP = True Then
GoSub add_proj 'add project if project doesn't exist
End If
Return
End Function
Spreadsheet into Access.
1. Access is locking the file open when the code is run
2. The program abends with run-time error code '9' - "subscript out of
range error"
The Excel workbook is set up with the sheet names on the 1st sheet and the
spreadsheet is updating frequently with new sheets being added or existing
sheets being updated. I am attempting to read in the sheet names on the 1st
sheet into an array, then determine if the sheet is new or just an update.
If it is new, then it would add the sheet information to the database,
otherwise it would just update the data in Access. Here's the code:
Public Function import_rlc()
Dim strFileName As String, strTab As String, strSql As String, strCell As
String
Dim strCell2 As String, strAdd As String
Dim xlsApp As Object
Dim xlsWB1 As Object
Dim xlsWS1 As Object
Dim xlsWS2 As Object
Dim intCol As Integer
Dim intRow As Integer
Dim blnProject As Boolean, blnNewP As Boolean
Dim dblCell As Double
Dim rst1 As Recordset, rst2 As Recordset
Dim col As Integer
Dim row As Integer
strFileName = "c:\RLC.xls"
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = False
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Set xlsWS1 = xlsWB1.Worksheets("Index")
MaxRow = 200
MaxCol = 15
ReDim CaseArray(MaxRow, MaxCol)
For row = 1 To MaxRow
For col = 1 To MaxCol
CaseArray(row, col) = xlsWS1.cells(row, col).Value
Next
Next
xlsWB1.Close
set xlsApp = Nothing
intRow = 1
Do Until intRow = 200
strTab = CaseArray(intRow, 1)
GoSub check_tab
If blnProject = True Then
Set xlsApp = CreateObject("Excel.Application")
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Set xlsWS2 = xlsWB1.Worksheets(strTab)
GoSub import_tab
xlsWB1.Close
Set xlsWB1 = Nothing
Set xlsWS2 = Nothing
Set xlsApp = Nothing
End If
intRow = intRow + 1
Loop
Exit Function
check_tab: 'check to see if the cell value is valid project
blnProject = True
strSql = "SELECT Tab_Name FROM tbl_NonProject_Tabs WHERE Tab_Name = "
strSql = strSql & "'" & strTab & "'"
'tbl_NonProject_Tabs lists those tabs that are not real projects
Set rst1 = CurrentDb.OpenRecordset(strSql)
If rst1.EOF = False Then
blnProject = False 'if value is in table then not a project
rst1.Close
End If
If strTab = "" Then
blnProject = False 'if value is blank then not a project
End If
Return
check_new_proj: 'check to see if the project exists on database
blnNewP = False
strSql = "Select * FROM tbl_projects WHERE project_code = '"
strSql = strSql & strCell & "'"
Set rst1 = CurrentDb.OpenRecordset(strSql)
If rst1.EOF = True Then
blnNewP = True
End If
rst1.Close
Return
add_proj: 'add new project to database
Set rst2 = CurrentDb.OpenRecordset("tbl_projects", dbOpenDynaset)
rst2.AddNew
rst2![project_code] = strCell
rst2![project_description] = strCell2
rst2![Active] = True
rst2.Update
rst2.Close
Return
import_tab: 'read data from tab and add new data
strCell = xlsWS2.cells(2, 4).Value
strCell2 = xlsWS2.cells(2, 5).Value
GoSub check_new_proj 'check to see if project exists in database
If blnNewP = True Then
GoSub add_proj 'add project if project doesn't exist
End If
Return
End Function