Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Updating recordset in Access VBA

Posted on 2016-09-27
13
Medium Priority
?
123 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 22
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 52

Accepted Solution

by:
Gustav Brock earned 1000 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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 52

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 1000 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 52

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

Industry Leaders: 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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

971 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