Results 1 to 6 of 6
  1. #1
    Mclaren is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164

    Adding records to existing table

    I have a form which when opened to add a new record, and using the ondirty property, should update a table with a certain number of records. I have used the following code:



    Code:
    Private Sub Form_Dirty(Cancel As Integer)
    DoCmd.SetWarnings False
    If Me.NewRecord Then
    
    For I = 1 To 14
    DoCmd.RunSQL "INSERT INTO tbl_Transactions_detail_Data (TransactionID) VALUES (" & Me.tbl_Transactions_TransactionID & ")"
    Next
    DoCmd.SetWarnings True
    Me.Requery
    Else
    End If
    
    End Sub
    The code works but not really, well:

    if i am in an existing record, the code adds the required number of records to the other table (not suppose to happen)

    If it is a new record the i get a runtime error 3134.

    I am sure this is probably because the new record has yet to be saved and as a result the
    Code:
    Me.tbl_Transactions_TransactionID
    value is not yet available.

  2. #2
    Mclaren is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    Ok, so i have figured out why i cannot insert the new records and that is because the transaction id has not yet been created.

    I have tried inserting the below code imediatly after checking if the record is new. this seems to do nothing.
    Code:
    RunCommand acCmdSaveRecord
    how can i save the record, so as to create a number in the TransactionId field (which is an Auto Number field). And then use the transactionID value to create the records in the linked table ?

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    The new record will be initiated as soon as a value is entered into any field (other than the AutoNumber of course). Then the acCmdSaveRecord should commit the record (although might not be necessary). Is there some field you could set with a default value? A date field can be set with Date() as the default and the current date will populate the field. That will serve to initiate the record.

    The reference Me.tbl_Transactions_TransactionID is odd to me. Is that the name of control on form?

    The few times I have tried to use the OnDirty event have been unsatisfactory, never could get it to work way I wanted. I have used the Dirty property for form successfully, as in: If Me.Dirty Then
    Last edited by June7; 05-16-2011 at 12:13 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Mclaren is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    yes, Me.tbl_Transactions_TransactionID, is the name of the control, however, i have fixed this and it is now just me.transactionID

    Each time the form is opened, i need to check:

    A: if the record is new, if so, then immedialty create 14 records in a table that is linked to the forms record set.

    B: if the record is not new, then no action to be taken as the 14 records in the secondary table would have been created when the record was first created.

    Basically for each record in the primary table, their must be exactly 14 records in the secondary table (no more and no less). Even if no data is to be inserted into these records in the secondary table.

    All this just so i can get a report to look exactly an original paper version of a document that is normally completed by hand.

    the document has a transaction id and place for 14 lines of products. these lines may or may not have information stored (i.e there are only 3 products listed so only 3 lines have data). The document is a standard form and is set by head office. iI need my report to look exactly like that document, including empty lines.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    I Googled: Access report blank rows
    Got something you might find interesting as alternative to bloating table with blank records: http://www.utteraccess.com/forum/Cre...3#entry1393413

    For the code you posted, try OnCurrent event instead of OnDirty. Also, the default value suggestion.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Mclaren is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    Thanks June7, got the example from utter-access working a treat, saves a lot of unnessesary empty records.

    My Report (and form) are now working just right.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Populate existing records from Excel
    By oleBucky in forum Import/Export Data
    Replies: 2
    Last Post: 03-08-2011, 12:23 AM
  2. Cloning data to other existing records.
    By GraemeG in forum Programming
    Replies: 0
    Last Post: 02-16-2011, 11:53 AM
  3. Replies: 3
    Last Post: 11-02-2010, 10:15 AM
  4. Replies: 1
    Last Post: 05-18-2010, 11:43 AM
  5. Replies: 0
    Last Post: 10-14-2009, 02:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums