Results 1 to 10 of 10
  1. #1
    nick.h is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    22

    Add record using VBA code

    Hey guys,



    I have a report that displays data from three different tables, and I would like to avoid having it ever show up blank (due to no data in one table or another). The best way that I was able to come up with solving that problem is to have some logic that:

    1) checks to see if there is a record in the second and third tables with the current FK in it (PK on table 1 is a foreign key in tables 2 and 3). The FK is unique in the other tables

    2) If there is no record with the current FK then add a record with the FK in it.

    3) If there is a record with this FK then do nothing.

    Is there a different way to go about this? The big thing is I don't ever want to have this report show up completely blank! Let me know if you have any questions about any of this.

    Thanks for your time!
    Nick

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481
    The following link is a concise description of working with recordsets using VBA.
    http://allenbrowne.com/ser-29.html
    Item 8 is of particular interest to you.

  3. #3
    nick.h is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    22
    Still having issues. Here is the code that I'm trying to use to add a recrod, but to no avail. What is the problem with it?

    Code:
    Set db = CurrentDb
    Set rs = db.OpenRecordset("GKInfo")
    DoCmd.Save , "frmBusinessMain"
    If Not rs.BOF Then
        Do Until rs.EOF
            If Me!BID = rs("BID") Then
            Exit Sub
            Else: With rs
                    .AddNew
                    !BID = Me!BID
                    !AddedBy = StrUserName
                    !MyDSC = StrMyDSC
                    .Update
                    .Close
                End With
                Exit Do
            End If
            rs.MoveNext
            Loop
        End If

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Do not mix single line and multi-line code. Change this line: Else: With rs

  5. #5
    nick.h is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    22
    I'm sorry, but I don't understand. What should I change it to? I've been teaching myself (rather poorly it seems!) and I'm not sure what you mean by single line and multi line

  6. #6
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Here's what RuralGuy meant ... and more

    Quote Originally Posted by nick.h View Post
    Still having issues. Here is the code that I'm trying to use to add a recrod, but to no avail. What is the problem with it?

    Code:
    Set db = CurrentDb
    Set rs = db.OpenRecordset("GKInfo")
    DoCmd.Save , "frmBusinessMain"
    If Not rs.BOF Then
        Do Until rs.EOF
            If Me!BID = rs("BID") Then  'I'm not familar with rs("Bid") syntax, if it works...great
            Exit Sub
             'Else: With rs
    ;should be 
      Else:
      with rs
                     .AddNew
                    !BID = Me!BID
                    !AddedBy = StrUserName
                    !MyDSC = StrMyDSC
                    .Update
                    .Close
                End With
                Exit Do
            End If
            rs.MoveNext
            Loop
        End If
    You used a loop, but my first take on the code is that only one record at most will be added????

  7. #7
    nick.h is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    22
    Yep only one record. I am trying to get it to look and find out if there is a record that contains the unique FK "BID", and if there is a record with BID then just end the routine. If there is NOT a record with that BID I want it to create a record.

    Yes, only one record at most.

  8. #8
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Let's look at your code again. I have questions,

    Set db = CurrentDbSet rs = db.OpenRecordset("GKInfo")
    DoCmd.Save , "frmBusinessMain"
    ' If you need to read through the recordset, then I would change the next line to : rs.movefirst
    and remove the if NOT.....

    If Not rs.BOF Then

    Do Until rs.EOF
    If Me!BID = rs("BID") Then
    'I'm not familar with rs("Bid") syntax, if it works...great
    ' However, I suspect you want: = rs.[BID]

    Exit Sub
    'Else: With rs
    'should be
    Else:
    with rs
    .AddNew
    !BID = Me!BID
    !AddedBy = StrUserName

    !MyDSC = StrMyDSC
    ' What are StrUserName and StrMyDSC? If they are fields, they need brackets around them.

    .Update
    .Close
    End With
    Exit Do
    End If
    rs.MoveNext
    Loop
    End If ' may be unneeded.

    VBA has requirements for referencing fields, variables, and controls. You have to follow them.
    See this link for example of field references:
    http://www.access-programmers.co.uk/...d.php?t=128854

  9. #9
    nick.h is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    22
    It adds the record now, which is a huge step forward! The problem is that if there is already a record with a matching BID in GKInfo then it displays the following error:
    The list item could not be inserted or updated because duplicate values were found for one or more fileds in the list.

    I have BID set to unique since it is a foreign key, and should never have more than one record. Why doesn't it end the routine when it finds a matching BID? I feel like with the code I had above it should Exit Sub gracefully!

    Thanks for your help
    Nick

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    To check for a duplicate you can use DCOUNT()

    This will get count from a particular table and can include a where clause.

    If Dcount("RECId","MyTable", "RecId =" & "MyProposedRecId") > 0 Then
    'if the count is not 0 then by pass adding this "duplicate" attempt
    goto BypassTheUpdate
    else
    "Update code would go here"
    endif

    ByPassTheUpdate:

    see http://www.techonthenet.com/access/f...ain/dcount.php

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

Similar Threads

  1. Replies: 7
    Last Post: 11-23-2011, 08:14 PM
  2. execute code on subform without adding a record
    By markjkubicki in forum Forms
    Replies: 5
    Last Post: 10-13-2011, 12:23 PM
  3. Replies: 2
    Last Post: 01-27-2011, 08:04 AM
  4. Code to pull in data from a specific record
    By jdunn36 in forum Access
    Replies: 1
    Last Post: 09-20-2010, 11:54 AM
  5. Replies: 4
    Last Post: 05-12-2009, 01:50 PM

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