Solved

Updating recordset in Access VBA

Posted on 2016-09-27
13
66 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Report that will show table changes 7 48
Field behavior for "locked" form 12 29
Help writing a query 6 73
Newbie needs help printing from a form. 10 20
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views 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 Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

862 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

23 Experts available now in Live!

Get 1:1 Help Now