Ok I am not a VBA guy, I can understand a good amount of code but I need Google for the most part 
I had this bit of code created for me to import a bunch of Excel documents into 5 tables inside access. The problem is that it works on a test database but not on my working database.
I guess what I am looking for is some direction on where I might look for issues.
Adding code, not sure if it will help
I had this bit of code created for me to import a bunch of Excel documents into 5 tables inside access. The problem is that it works on a test database but not on my working database.
I guess what I am looking for is some direction on where I might look for issues.
Adding code, not sure if it will help
Code:
Option Compare Database
Option Explicit
Public objExcel As Object 'Excel.Application
Public blnNewExcelLaunched As Boolean
Const blnOverWriteWithoutConfirmation As Boolean = True 'Change this to false, and the script will ask for confirmation before overwriting
Sub ProcessThisForm(strFile As String)
Dim wbk As Object 'Excel.Workbook
Dim wks As Object 'Excel.Worksheet
Dim lngLastRow As Long
Dim lngStartRow As Long
Dim lngLoop As Long
Dim rst As Recordset
Dim lngFormIDForeignKey As Long
Dim lngCategoryIDForeignKey As Long
Dim strCurrentCategory As String
Dim lngRowsDiscardedBelow As Long
If objExcel Is Nothing Then
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
Err.Clear: On Error GoTo -1: On Error GoTo 0
If objExcel Is Nothing Then
Set objExcel = CreateObject("Excel.Application")
blnNewExcelLaunched = True
End If
End If
objExcel.Visible = 1
Set wbk = objExcel.Workbooks.Open(strFile, 0, 1, , , , 1, , , 0, 0, , 0)
For Each wks In wbk.Worksheets
If wks.Visible = -1 Then 'xlSheetVisible
Exit For
End If
Next wks
On Error GoTo ErrHandler
lngStartRow = wks.Range("$A:$A").Find(What:=1, LookAt:=xlWhole).Row
lngLastRow = wks.Cells(wks.Rows.Count, 1).End(xlUp).Row
Debug.Print wks.Cells(lngStartRow + 2, 3).Value
Set rst = CurrentDb.OpenRecordset("SELECT [COI] FROM tblSMRForm WHERE [COI]=""" & wks.Cells(lngStartRow + 2, 3).Value & """", , [dbSeeChanges])
If (rst.BOF And rst.EOF) Then 'Meaning the COI does not exist in the tblSMRForm
OverWRite:
If InStr(1, wks.Range("$A:$A").Find(What:=6, LookIn:=xlValues, LookAt:=xlWhole).Offset(2, 1).Value, "Time out") = 0 Then
'For Site name, if user has selected 'Other', then pick from the adjacent column
CurrentDb.Execute ("INSERT INTO [tblSMRForm]" & vbNewLine & _
"(COI, TypeOfVisit, SiteName, SiteCity, SiteState, VisitDate, TimeIn, TimeOut, MonitoredBy, PreparedBy, CompletionDate)" & vbNewLine & _
"Values (""" & wks.Cells(lngStartRow + 2, 3).Value & """, " & vbNewLine & """" & _
wks.Cells(lngStartRow, 3).Value & """, " & vbNewLine & """" & _
IIf(wks.Cells(lngStartRow + 5, 3).Value = "Other", wks.Cells(lngStartRow + 5, 4).Value, wks.Cells(lngStartRow + 5, 3).Value) & """, " & vbNewLine & """" & _
wks.Cells(lngStartRow + 6, 3).Value & """, " & vbNewLine & """" & _
wks.Cells(lngStartRow + 7, 3).Value & """, " & vbNewLine & _
CDate(wks.Cells(lngStartRow + 8, 3).Value) & ", " & vbNewLine & "#" & _
CDate(wks.Cells(lngStartRow + 9, 3).Value) & "#, " & vbNewLine & "#" & _
CDate(wks.Cells(lngLastRow, 3).Value) & "#, " & vbNewLine & """" & _
wks.Cells(lngStartRow + 14, 3).Value & """, " & vbNewLine & """" & _
wks.Cells(lngLastRow - 2, 3).Value & """, " & vbNewLine & "#" & _
wks.Cells(lngLastRow - 1, 3).Value & "#)")
lngRowsDiscardedBelow = 3
Else
CurrentDb.Execute ("INSERT INTO [tblSMRForm]" & vbNewLine & _
"(COI, TypeOfVisit, SiteName, SiteCity, SiteState, VisitDate, TimeIn, TimeOut, MonitoredBy, PreparedBy, CompletionDate)" & vbNewLine & _
"Values (""" & wks.Cells(lngStartRow + 2, 3).Value & """, " & vbNewLine & """" & _
wks.Cells(lngStartRow, 3).Value & """, " & vbNewLine & """" & _
IIf(wks.Cells(lngStartRow + 5, 3).Value = "Other", wks.Cells(lngStartRow + 5, 4).Value, wks.Cells(lngStartRow + 5, 3).Value) & """, " & vbNewLine & """" & _
wks.Cells(lngStartRow + 6, 3).Value & """, " & vbNewLine & """" & _
wks.Cells(lngStartRow + 7, 3).Value & """, " & vbNewLine & "#" & _
CDate(wks.Cells(lngStartRow + 8, 3).Value) & "#, " & vbNewLine & "#" & _
CDate(wks.Cells(lngStartRow + 9, 3).Value) & "#, " & vbNewLine & "#" & _
CDate(wks.Cells(lngStartRow + 10, 3).Value) & "#, " & vbNewLine & """" & _
wks.Cells(lngStartRow + 14, 3).Value & """, " & vbNewLine & """" & _
wks.Cells(lngLastRow - 1, 3).Value & """, " & vbNewLine & "#" & _
wks.Cells(lngLastRow, 3).Value & "#)")
lngRowsDiscardedBelow = 2
End If
Set rst = Nothing
Set rst = CurrentDb.OpenRecordset("SELECT DISTINCT SMRFormID FROM tblSMRForm WHERE COI=""" & wks.Cells(lngStartRow + 2, 3).Value & """", , [dbSeeChanges])
lngFormIDForeignKey = rst.Fields(0).Value
For lngLoop = wks.Range("$A:$A").Find(What:=7, LookIn:=xlValues, LookAt:=xlWhole).Row To wks.Range("$A:$A").Find(What:=8, LookIn:=xlValues, LookAt:=xlWhole).Row - 1
If InStr(1, wks.Cells(lngLoop, 3).Value, "N/A") = 0 Then
CurrentDb.Execute "INSERT INTO [tblPersonnel] (Personnel, fk_SMRFormID) Values(""" & Replace(wks.Cells(lngLoop, 3).Value, """", """""") & """," & lngFormIDForeignKey & ")"
End If
Next lngLoop
For lngLoop = 21 To lngLastRow - lngRowsDiscardedBelow
If (IsNumeric(wks.Cells(lngLoop, 1).Value) Or (Not IsEmpty(wks.Cells(lngLoop, 2)))) And Not (wks.Cells(lngLoop, 1).Value & wks.Cells(lngLoop, 2).Value = "") Then
Set rst = Nothing
Set rst = CurrentDb.OpenRecordset("SELECT DISTINCT CategoryID FROM [tblCategory] WHERE [Category]=""" & strCurrentCategory & """ AND [fk_SMRFormID]=" & lngFormIDForeignKey, , [dbSeeChanges])
CurrentDb.Execute ("INSERT INTO [tblQandA] (Question, YesNoNAOption, Comments, fk_CategoryID, fk_SMRFormID) Values(""" & Replace(wks.Cells(lngLoop, 2).Value, """", """""") & """, """ & Replace(wks.Cells(lngLoop, 3).Value, """", """""") & """, """ & Replace(wks.Cells(lngLoop, 4).Value, """", """""") & """," & rst.Fields(0).Value & "," & lngFormIDForeignKey & ")")
ElseIf (Not IsNumeric(wks.Cells(lngLoop, 1).Value)) And IsEmpty(wks.Cells(lngLoop, 2)) Then
strCurrentCategory = wks.Cells(lngLoop, 1).Value
CurrentDb.Execute ("INSERT INTO [tblCategory] (Category, fk_SMRFormID) Values(""" & Replace(strCurrentCategory, """", """""") & """," & lngFormIDForeignKey & ")")
End If
Next lngLoop
Else
Set rst = Nothing
If blnOverWriteWithoutConfirmation Then
GoTo GoToSilentOverWrite
End If
If vbOK = MsgBox("This COI already exists in the database!" & vbCrLf & vbCrLf & "To overwrite with data from this form, click OK", vbOKCancel + vbInformation, "Existing COI") Then
GoToSilentOverWrite:
CurrentDb.Execute "DELETE * FROM [tblSMRForm] WHERE [COI]=""" & wks.Cells(lngStartRow + 2, 3).Value & """;"
GoTo OverWRite
End If
End If
ErrHandler:
If Err.Number <> 0 Then
CurrentDb.Execute "INSERT INTO [tblErrorLog] SELECT """ & wbk.Name & """ AS WorkbookName,#" & Now() & "# AS ErrorDate,""" & Err.Description & """ AS ErrorDesc;"
Err.Clear: On Error GoTo -1: On Error GoTo 0
End If
wbk.Close 0
Set wks = Nothing
Set wbk = Nothing
End Sub