Bizarre problem with a function in a query.. (1 Viewer)

proben930

Registered User.
Local time
Yesterday, 21:39
Joined
Mar 22, 2004
Messages
30
I have a function that takes 4 inputs and creates a record in a table (for accounting). I wanted to process a batch of records at once, based on the results in a query. So I made the query to give me the records I wanted to make accounting entries. I then made a second query that has a field with the function in it, that feeds the inputs off the first query. Everything seems to be normal,the query returns 5 records, the function runs 5 times and creates the records. Problem is, it creates SIX records. The first one is duplicated for some reason. There are 5 results from the query, but 6 records created. Here is the code of the function:

Code:
Public Function AddRecord(tableopen As String, IDNum As Long, amnt As Currency, entdate As Date, descrip As String)

Dim dbnm As Database
Dim rstAccount As Recordset

Set dbnm = OpenDatabase("returnedchecks.mdb")
Set rstAccount = dbnm.OpenRecordset(tableopen)

    With rstAccount
        .AddNew
        !IDNo = IDNum
        !entrydate = entdate
        !amount = amnt
        !description = descrip
        .Update
    End With

AddRecord = "Yes"


End Function

Here's the SQL of my first query (creates the initial recordset):
SELECT tblChecks.*, tblChecks.IDNo AS IDNo2
FROM tblChecks
WHERE (((tblChecks.SentToDMV)=0) AND ((tblChecks.TagStatus)="2"));


SQL for second query (runs the function)

SELECT AddRecord("tblARDMV",qryDMV2!IDNo2,tblchecks!checkamounttag,Date(),"Stage 2: etc") AS Expr1, qryDMV2.IDNo, tblChecks.CheckAmountTag
FROM tblChecks INNER JOIN qryDMV2 ON tblChecks.IDNo = qryDMV2.IDNo;



What is going on here? Why is it creating an extra record in 'tblARDMV'?

Thanks a lot!
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 21:39
Joined
Feb 19, 2002
Messages
43,223
I don't know. I would have used an append query and not written any code at all. Select the columns you want from tblA, change the query type to Append. Access will automatically map columns with matching names. You need to manually select the Append To column for unmatched fields.
 

proben930

Registered User.
Local time
Yesterday, 21:39
Joined
Mar 22, 2004
Messages
30
Worked like a charm! Thanks Pat. First time I've used one of those before :) Have to admit though, that 6 record thing was pretty weird...
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 21:39
Joined
Feb 19, 2002
Messages
43,223
As I have said a numbe of times, I've written my million lines of code and I've spent way too many nights and weekends in the computer room and my daughter has spent more than one night sleeping under my desk. I don't write code unless I have to and it is not because I can't. It is because I don't want to debug it:) And that's why I love Access so much.
 

Users who are viewing this thread

Top Bottom