bear
bear

Reputation: 11605

Update access recordset using VB.NET

How do I update an MS-Access recordset using VB.NET?

I've basically to update a field which has id of 1, and field value for 'ticker' is 'UKX'.

The data to be updated is in the 'value' field and comes from a Public Sub. dLast is the data which I need inputting to update the current value.

I've currently got the code:

Function update_db()
    ' need to update the database
    Dim daoengine As DAO.DBEngine
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    daoengine = New DAO.DBEngine
    dbs = daoengine.OpenDatabase("Project.mdb")
    rst = dbs.OpenRecordset("ftsedata", DAO.RecordsetTypeEnum.dbOpenDynaset)
    rst.Edit()
    ' what do I do here to edit the field "value" where ticker=UKX ?
    rst.Close()
    dbs.Close()
End Function

Public Sub getftsestock()
    Dim sAPIUrl As String = "http://www.google.com/ig/api?stock=UKX"
    Dim oDocument As XDocument = XDocument.Load(sAPIUrl)
    Dim dLast As Double = CDbl(GetData(oDocument, "last"))
End Sub

Upvotes: 0

Views: 8691

Answers (2)

JohnFx
JohnFx

Reputation: 34909

First, change your sub into a function like so

Public function getftsestock() as double
    Dim sAPIUrl As String = "http://www.google.com/ig/api?stock=UKX"
    Dim oDocument As XDocument = XDocument.Load(sAPIUrl)
    getftsestock = CDbl(GetData(oDocument, "last"))
End Sub

Then do your update like this

    rst = dbs.OpenRecordset("select * from ftsedata where (ticker='UKX')", DAO.RecordsetTypeEnum.dbOpenDynaset)
    while not rsy.eof
        rst.Edit
        rst("value").value = getftsestock()
        rst.update

       rst.movenext
    wend

   rst.Close()

However, it would be much more efficient to do this.

daoengine.Execute "Update ftsedata SET value='" & getftsestock() & "' WHERE ticker='UKX'"

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94645

You have to call Execute method of Database object.

daoengine.Execute "Update TableName set Col1=Value1 Where Ticker='UKX'"

You should have to use ADO.NET Oledb provider instead of DAO in VB.NET.

Upvotes: 0

Related Questions