Solved

Updating recordset in Access VBA

Posted on 2016-09-27
13
108 Views
Last Modified: 2016-10-05
I am trying to update a record in a table based on the criteria in a field, let's say color.  There may be many records that have the color RED but I only need to grab the first one to make an adjustment to the price.  I am using the code below which is making a price adjustment but it is not grabbing a RED record, it is grabbing another color.  This is a snippet of the code:

With rstJournalShell
                    .Edit
                    rstJournalShell![GLCOLOR] = strEntities(iCounter) 'this is an array that hold the color RED
                    .Fields("SumOfCoursePrice") = rstJournalShell.Fields("SumOfCoursePrice") - curBalance 'This is the price adjustment
                   .Update
                End With


Is there a way that I can grab a record that has the color RED and make the adjustment to that?
0
Comment
Question by:marku24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41818871
YOU PROBABLY NEED TO 'FINDFIRST' A RECORD WITH RED, THEN UPDATE
0
 
LVL 21
ID: 41818992
since you didn't post all your code, I am assuming that because there is "SumOfCoursePrice", that this is a query that is not an updateable recordset.  Please post code for the whole procedure, thanks.
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 41819305
You need first to look up a record with RED:
With rstJournalShell
    .FindFirst ![GLCOLOR].Value = strEntities(iCounter)
    If .NoMatch = False Then
        .Edit
            With .Fields("SumOfCoursePrice")
                .Value = .Value - curBalance 'This is the price adjustment
            End Width
        .Update
    End If
End With

Open in new window

/gustav
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:marku24
ID: 41820064
Thank you Gustav - this seems to be the correct solution but my syntax for the .Findfirst is giving me an error.  below is my real field names.  not sure exactly how to use .findfirst.  can you help?
With rstJournalShell
                    .FindFirst "[GLEntityConversion] = '" & strEntities(iCounter) & "'"
                    If .NoMatch = False Then
                         .Edit
                            With .Fields("SumOfCoursePrice")
                                .Value = .Value - curBalance
                            End With
                        .Update
                    End If
                End With
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 41820096
It seems correct to me if the search value is a string.
Try inserting a line with Debug and study the output:

    Debug.Print "[GLEntityConversion] = '" & strEntities(iCounter) & "'"
    .FindFirst "[GLEntityConversion] = '" & strEntities(iCounter) & "'"

/gustav
0
 

Author Comment

by:marku24
ID: 41820184
I get this in the immediate window:  [GLEntityConversion] = '0001' which is correct.  When it tries to run the .findfirst code i get this:

Run-time error 3251; Operation is not supported for this type of object,
0
 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 250 total points
ID: 41820206
what type of recordset did you open?
s/b dynaset or snapshot
0
 

Author Comment

by:marku24
ID: 41820264
Dim rstJournalShell As DAO.Recordset
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 41820285
Maybe you don't have any records. Try:

    With rstJournalShell
        Debug.Print .RecordCount
        If .RecordCount > 0 Then
            .FindFirst "[GLEntityConversion] = '" & strEntities(iCounter) & "'"

It should return minimum 1.

/gustav
0
 

Author Comment

by:marku24
ID: 41820297
Record count returns 2, I am using sample data.   same error on .findfirst.
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41820303
please send your open recordset code (dynaset or snapshot?)
0
 

Author Comment

by:marku24
ID: 41820306
That worked!!!!  I had:
Set rstJournalShell = CurrentDb.OpenRecordset("tblJournalEntryShell")

and changed to
Set rstJournalShell = CurrentDb.OpenRecordset("tblJournalEntryShell", dbOpenDynaset)

That was the difference.  Code runs great.  Thank you all.
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41820326
good to hear :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question