SilverSN95
07-10-2009, 01:17 PM
Hi, Im not sure if this is should be in the Access forum, as the coding is done in excel but the error is happening in access.
Anyways, Ive modified a macro that I would like to create a new record in the db from information I have in a column in excel (column B). The field names to be updated are in column A. I had simular macro working just fine where I would just say:
.AddNew
.Fields("Insured Name")= Range("B" & r).Value
but I want it to be able to run in a loop as I'm attempting below.
Private Sub CommandButton231_Click()
Dim curDatabase As DAO.Database
Dim table As DAO.Recordset
Dim r As Long
Dim str As String
Set curDatabase = OpenDatabase(Range("J1").Value)
Set table = curDatabase.OpenRecordset(Range("J2").Value)
r = 1 'start row in xls
'MsgBox Range("A" & r)
Do While Len(Range("A" & r).Formula) > 0 ' Scale rows untill bottom of column A
With table
.AddNew
.Fields(Range("A" & r)) = Range("B" & r).Value
End With
r = r + 1 'next row
Loop
table.Update
table.Close
Set table = Nothing
curDatabase.Close
Set curDatabase = Nothing
MsgBox "Information Submitted"
CommandButton1.Visible = False
End Sub
The application error occurs at table.Update. Any ideas?
Just a heads up I know very little VBA although I'm not new to programming in general.
Thanks.
Anyways, Ive modified a macro that I would like to create a new record in the db from information I have in a column in excel (column B). The field names to be updated are in column A. I had simular macro working just fine where I would just say:
.AddNew
.Fields("Insured Name")= Range("B" & r).Value
but I want it to be able to run in a loop as I'm attempting below.
Private Sub CommandButton231_Click()
Dim curDatabase As DAO.Database
Dim table As DAO.Recordset
Dim r As Long
Dim str As String
Set curDatabase = OpenDatabase(Range("J1").Value)
Set table = curDatabase.OpenRecordset(Range("J2").Value)
r = 1 'start row in xls
'MsgBox Range("A" & r)
Do While Len(Range("A" & r).Formula) > 0 ' Scale rows untill bottom of column A
With table
.AddNew
.Fields(Range("A" & r)) = Range("B" & r).Value
End With
r = r + 1 'next row
Loop
table.Update
table.Close
Set table = Nothing
curDatabase.Close
Set curDatabase = Nothing
MsgBox "Information Submitted"
CommandButton1.Visible = False
End Sub
The application error occurs at table.Update. Any ideas?
Just a heads up I know very little VBA although I'm not new to programming in general.
Thanks.