Solved

Updating recordset in Access VBA

Posted on 2016-09-27
13
60 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
  • 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 19
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 49

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
 

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 49

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 49

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now