Hello:
I am trying to use below code which i got from Google but for some reason it is erroring out at 3rd column.
Please help to find and resolve the problem.
Note : I am re-attaching the excel file with the code.
Thanks
Sub ExportToacess()
Dim oSelect As Range, i As Long, j As Integer, sPath As String
Sheets("EmpHours").Activate
Set oSelect = Application.InputBox("Range", , Range("A5").CurrentRegion.Address, , , , , 8)
'MsgBox ("Row: ") & oSelect.Rows.Count
'MsgBox ("Column: ") & oSelect.Columns.Count
Dim oDAO As DAO.DBEngine, oDB As DAO.Database, oRS As DAO.Recordset
ChDir ActiveWorkbook.Path
'sPath = Application.GetOpenFilename("Access,*.accdb")
sPath = Application.GetOpenFilename("Access,*.mdb")
If sPath = "False" Then Exit Sub
Set oDAO = New DAO.DBEngine
Set oDB = oDAO.OpenDatabase(sPath)
Set oRS = oDB.OpenRecordset("EmpHour2")
For i = 2 To oSelect.Rows.Count ' Skip lable row
oRS.AddNew
For j = 1 To oSelect.Columns.Count ' Field(0) is Auto#
' For j = 1 To 4
MsgBox ("Column Count: ") & j
oRS.Fields(j) = oSelect.Cells(i, j)
MsgBox ("oRSFields: ") & oRS.Fields(j)
Next j
oRS.Update
Stop
Next i
oDB.Close
If MsgBox("Open the table?", vbYesNo) = vbYes Then
Dim oAPP As Access.Application
Set oAPP = New Access.Application
oAPP.Visible = True
oAPP.OpenCurrentDatabase sPath
oAPP.DoCmd.OpenTable "EmpHour2", acViewNormal, acReadOnly
oAPP.DoCmd.GoToRecord , , acLast
DoEvents
End If
End Sub
Bookmarks