Hi - i have been using this code for some time now on many spreadsheets. I just created another spreadsheet did a copy and paste into a VBA module, edited the .fields to mirror my access table and it will not run. I noticed that the .fields ("") = Range("A" & mrow).Value has this label when i step through the code (f8) (fields ("") = Range("A" & mrow). =(object vairable or with block variable not set) appears.
Below is the code that i am trying to run, can someone look at it and let me know what I am doing wrong? I am using office 07 but needed to write in 2000 - 2003 version.
Sub Users_tomdb()
' open excel file first that you want to export data to the database this goes into the excel module.
Dim mData As String 'this will be the active excel workbook
mData = ActiveWorkbook.Name 'this shows the excel workbook that is active
Dim mrow As Integer 'Counting of rows in sheet to be exported
'Create connection
Dim mConnection As ADODB.Connection
Set mConnection = New ADODB.Connection
'create the link to Access master data file using your connection created.
'You need to specify what drive your data base is on
Dim mtransit_file As String 'This is the name of the database
mtransit_file = "Provider=Microsoft.Jet.oledb.4.0;data Source=" & "\\erdxxxnas000\cccdata\ECMS\S_M\XXX\PC \All_Projects\Turnover\XXX_Web_Access.mdb"
'Open Connection
mConnection.Open ConnectionString:=mtransit_file
'create link to data table. Make sure you name the table in the mRecordset.open " "
Dim mRecordset As ADODB.Recordset
Set mRecordset = New ADODB.Recordset
mRecordset.Open "USERS", mtransit_file, adOpenKeyset, adLockOptimistic
'Get row by row data from import file = excel worksheet
mrow = 2 ' the start row in the worksheet
Do While Len(Range("A" & mrow).Formula) > 0
'repeat until first empty cell in column A
With mRecordset
.AddNew ' create a new record
'add values to each field in the Access Database Record
'("This is where you match the excel column headers to the access data base") .fields(" ")= Range("A" & mrow).Value
.Fields("Renew") = Range("A" & mrow).Value
.Fields("Renew_with_Changes") = Range("B" & mrow).Value
.Fields("Non_Renew") = Range("C" & mrow).Value
.Fields("HIG_EMP Y/N") = Range("D" & mrow).Value
.Fields("Emp_ID") = Range("E" & mrow).Value
.Fields("ACS_User_ID") = Range("F" & mrow).Value
.Fields("Agent_Name") = Range("G" & mrow).Value
.Fields("HIG_EMail_Address") = Range("H" & mrow).Value
.Fields("LOB") = Range("I" & mrow).Value
.Fields("Supervisor") = Range("J" & mrow).Value
.Fields("Supervisor_E-Mail") = Range("K" & mrow).Value
.Fields("Add_Date") = Range("L" & mrow).Value
.Fields("Delete_Date") = Range("M" & mrow).Value
.Fields("Certification_Date") = Range("N" & mrow).Value
.Fields("For_ESG_Use_ONLY_ACSWEBPortal") = Range("O" & mrow).Value
.Update ' stores the new record
End With
mrow = mrow + 1 ' next row
Loop
MsgBox "Your file has been successfully sent to the DataBase."
'Close all links - Clear memory
mRecordset.Close
Set mRecordset = Nothing
mConnection.Close
Set mConnection = Nothing
end sub
Below is the code that i am trying to run, can someone look at it and let me know what I am doing wrong? I am using office 07 but needed to write in 2000 - 2003 version.
Sub Users_tomdb()
' open excel file first that you want to export data to the database this goes into the excel module.
Dim mData As String 'this will be the active excel workbook
mData = ActiveWorkbook.Name 'this shows the excel workbook that is active
Dim mrow As Integer 'Counting of rows in sheet to be exported
'Create connection
Dim mConnection As ADODB.Connection
Set mConnection = New ADODB.Connection
'create the link to Access master data file using your connection created.
'You need to specify what drive your data base is on
Dim mtransit_file As String 'This is the name of the database
mtransit_file = "Provider=Microsoft.Jet.oledb.4.0;data Source=" & "\\erdxxxnas000\cccdata\ECMS\S_M\XXX\PC \All_Projects\Turnover\XXX_Web_Access.mdb"
'Open Connection
mConnection.Open ConnectionString:=mtransit_file
'create link to data table. Make sure you name the table in the mRecordset.open " "
Dim mRecordset As ADODB.Recordset
Set mRecordset = New ADODB.Recordset
mRecordset.Open "USERS", mtransit_file, adOpenKeyset, adLockOptimistic
'Get row by row data from import file = excel worksheet
mrow = 2 ' the start row in the worksheet
Do While Len(Range("A" & mrow).Formula) > 0
'repeat until first empty cell in column A
With mRecordset
.AddNew ' create a new record
'add values to each field in the Access Database Record
'("This is where you match the excel column headers to the access data base") .fields(" ")= Range("A" & mrow).Value
.Fields("Renew") = Range("A" & mrow).Value
.Fields("Renew_with_Changes") = Range("B" & mrow).Value
.Fields("Non_Renew") = Range("C" & mrow).Value
.Fields("HIG_EMP Y/N") = Range("D" & mrow).Value
.Fields("Emp_ID") = Range("E" & mrow).Value
.Fields("ACS_User_ID") = Range("F" & mrow).Value
.Fields("Agent_Name") = Range("G" & mrow).Value
.Fields("HIG_EMail_Address") = Range("H" & mrow).Value
.Fields("LOB") = Range("I" & mrow).Value
.Fields("Supervisor") = Range("J" & mrow).Value
.Fields("Supervisor_E-Mail") = Range("K" & mrow).Value
.Fields("Add_Date") = Range("L" & mrow).Value
.Fields("Delete_Date") = Range("M" & mrow).Value
.Fields("Certification_Date") = Range("N" & mrow).Value
.Fields("For_ESG_Use_ONLY_ACSWEBPortal") = Range("O" & mrow).Value
.Update ' stores the new record
End With
mrow = mrow + 1 ' next row
Loop
MsgBox "Your file has been successfully sent to the DataBase."
'Close all links - Clear memory
mRecordset.Close
Set mRecordset = Nothing
mConnection.Close
Set mConnection = Nothing
end sub