Solved

Updating recordset in Access VBA

Posted on 2016-09-27
13
96 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 20
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 50

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 50

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 50

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

710 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