I am using M.S.Access as backend database and able to add records from Excel but not able to Edit & Update the specific record from M.s Access with reference to cell value given in excel. Kinldy help me.
Regards,
ANANTH
Regards,
ANANTH
Sub EDIT_UPATE()
Dim Path As String
Dim rs As DAO.Recordset
Dim AccountId As String
Path = "C:\Documents and Settings\Jaganmohan\Desktop\db1.mdb"
Set Db = Workspaces(0).OpenDatabase(Path, ReadOnly:=False)
Set rs = Db.OpenRecordset("Accounts", DbOpenDynaset)
If rs.RecordCount <> 0 Then[INDENT]
rs.FindFirst "AccountId = 2235" ' DAO only
If Not rs.NoMatch Then
rs.Edit ' DAO only
rs!Amount = 200
rs.Update
Else
MsgBox "Record Not Found"
End If[/INDENT]
End If
rs.Close
Set Db = Nothing
Set rs = Nothing
EndSub
Sub EDIT_UPATE()
Dim Path As String
Dim rs As DAO.Recordset
Dim AccountId As String
Path = "C:\Documents and Settings\Jaganmohan\Desktop\db1.mdb"
Set Db = Workspaces(0).OpenDatabase(Path, ReadOnly:=False)
Set rs = Db.OpenRecordset("Accounts", DbOpenDynaset)
If rs.RecordCount <> 0 Then[INDENT]
rs.FindFirst "AccountId = 2235" ' DAO only
If Not rs.NoMatch Then
rs.Edit ' DAO only
rs!Amount = 200
rs.Update
Else
MsgBox "Record Not Found"
End If[/INDENT]
End If
rs.Close
Set Db = Nothing
Set rs = Nothing
EndSub
Sub EDIT_UPATE()
Dim xl As Object
Dim xlSht As Object
Dim xlWrkBk As Object
Dim xlFilePath As String
Dim xlFile As String
Dim dbFilePath As String
Dim rs As DAO.Recordset
Dim AccountId As String
Dim Amount As Currency
XlFile = “ xxxx.xls”
xlFilePath = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
dbFilePath = "C:\Documents and Settings\Jaganmohan\Desktop\db1.mdb"
‘open excel worksheet object
Set xl = CreateObject("Excel.Application")
Set xlWrkBk = GetObject(xlFile)
Set xlSht = xlWrkBk.Worksheets(1)
‘open access db table object
Set Db = Workspaces(0).OpenDatabase(dbFilePath, ReadOnly:=False)
Set rs = Db.OpenRecordset("Accounts", DbOpenDynaset)
‘loop until no more cells to copy to table
If rs.RecordCount <> 0 Then[INDENT] rs.MoveFirst
Do Until. rs.EOF = True
rs.AddNew
rs.Fields("AccountId") = xlSht.cells(2, "F")
rs.Fields("Amount") = Nz(xlsSht.cells(2, "C"),0)
rs.Update
rs.MoveNext[/INDENT]
Loop
End If
'Closing excel
xlWrkBk.Application.Quit
Set xl = Nothing
'test db table by itself
rs.FindFirst "AccountId = 2235" ' DAO only
If Not rs.NoMatch Then
rs.Edit ' DAO only
rs!Amount = 200
rs.Update
Else
MsgBox "Record Not Found"
End If
End If
'Closing recordset
rs.Close
Set Db = Nothing
Set rs = Nothing
End Sub
Comment