Solved

Cannot add record using addnew with recordset

Posted on 2011-09-13
7
342 Views
Last Modified: 2012-05-12
I am working on a project that adds a newrecord to a recordset based on information contained in another recordset.  I can't get the new record to add for some reason . I am attaching code.
Option Compare Database

Public Function CheckCoating()

'This function Checks to see if tray was coated prior to wash/block
On Error GoTo Err_CheckCoating


'This function Checks to see if tray was coated prior to wash/block
Dim rstCoated As DAO.Recordset
Dim rstWashed As DAO.Recordset
Dim Response As String
Dim Msg As String
Dim ID As String
Dim flag1 As Integer

Dim recordidX As Integer
Dim trayidX As String
Dim lotnumberX As String
Dim partnumberX As String
Dim OperatornameX As String
Dim starttimeX As Date
Dim endtimeX As Date
Dim endoperatorX As String
Dim approvedX As Boolean
Dim coatedX As Boolean
Dim coatopX As String
Dim coattmX As Date

'set variables to empty

recordidX = Empty
trayidX = Empty
lotnumberX = Empty
partnumberX = Empty
OperatornameX = Empty
starttimeX = Empty
endtimeX = Empty
endoperatorX = Empty
approvedX = Empty
coatedX = Empty
coatopX = Empty
coattmX = Empty
flag1 = 0

'Setup recordset
Set rstCoated = CurrentDb.OpenRecordset("qryCoatedTrays")
Set rstWashed = CurrentDb.OpenRecordset("qryWashedTrays")

'Create a transaction to prevent data loss

Workspaces(0).BeginTrans

'Check for data

If rstCoated.BOF = True Then
    MsgBox "There are no coated trays"
    Workspaces(0).Rollback
    Exit Function
End If

rstCoated.MoveFirst

Do Until rstCoated.EOF
    If rstCoated!TrayID = Forms![frmScanTray]![txtTrayID] Then
         
         With rstCoated
         recordidX = !RecordID
         trayidX = !TrayID
         lotnumberX = !LotNumber
         partnumberX = !PartNumber
         OperatornameX = CurrentUser()
         coatedX = !Coated
         coatopX = !CoatOp
         coattmX = !CoatTM
         End With
         
         starttimeX = Now()
         endtimeX = Empty
         endoperatorX = Empty
         approvedX = False
         
         With rstWashed
         
            .AddNew
            
            !RecordID = recordidX
            !TrayID = trayidX
            !LotNumber = lotnumberX
            !PartNumber = partnumberX
            !OperatorName = OperatornameX
            !StartTime = starttimeX
            !EndTime = endtimeX
            !EndOperator = endoperatorX
            !Approved = approvedX
            !Coated = coatedX
            !CoatOp = coatopX
            !CoatTM = coattmX
            
            .Update
            
    End With
         
         flag1 = flag1 + 1
         
         Exit Do
        
        End If
    
    rstCoated.MoveNext
    
 Loop
 
If flag1 = 0 Then
    MsgBox "Tray not coated"
    Workspaces(0).Rollback
    Exit Function
End If
   
Exit_CheckCoating:
    Exit Function
            
Err_CheckCoating:
    Workspaces(0).Rollback
    MsgBox Err.Description
    
End Function

Open in new window

0
Comment
Question by:tmalmond
[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
7 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36531568
what is the sql of query "qryWashedTrays"?  are you using more than one table in this query

why not just use the table as the domain for recordset rsWashed ?
0
 

Author Comment

by:tmalmond
ID: 36531614
I was told that it is better to use a query. The query is all the fields in the table. Just for kicks, I tried what you suggested and it didn't change.

I have done this many times and never had a problem. Its just weird.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36531673
why do you need a complete recordset of rsCoated from query "qryCoatedTrays"

and here you are testing the TrayID to be the same as the one displayed in the form

 If rstCoated!TrayID = Forms![frmScanTray]![txtTrayID] Then


why not, change your query "qryCoatedTrays" to have a criteria , where TrayID=" & Forms![frmScanTray]![txtTrayID]

then test accordingly

if rstCoated.Eof then exit function

'place code to add record to rsWahed here





0
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
LVL 14

Accepted Solution

by:
pteranodon72 earned 500 total points
ID: 36531688
You never commit your transaction.
HTH,
pT72
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36531819
<I was told that it is better to use a query.>
Told by who?, and under what circumstances...?

In any event, ...if the query is not update-able, this simply wont work...

<I have done this many times and never had a problem. >
With the exact same query?

0
 
LVL 75
ID: 36531852
I believe pT72 is correct ... there is no (transaction) Commit !

mx
0
 

Author Closing Comment

by:tmalmond
ID: 36532335
Bingo, left out the committrans. Good call.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

635 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