Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / October 2007

Tip: Looking for answers? Try searching our database.

Recordset updates crazy

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Billy B - 21 Oct 2007 08:37 GMT
I am trying to work with recordsets and have the following code. I want it to
add a new record if there are no records in the table and add a new record if
there are existing records. I get duplicate records and can't figure it out.
Would appreciate help. Thank you.

Option Compare Database

Private Sub Form_Load()

Dim strAsk As String
strAsk = UCase(InputBox("Do you want to add a new record?", "Add New?",
"YES"))
DoCmd.OpenForm "frmPlants", acNormal
If strAsk = "YES" Then
   DoCmd.GoToRecord , , acNewRec
Else
   DoCmd.GoToRecord , , acFirst
End If

End Sub
Private Sub cmdAddNew_Click()

   'declare variable and assign address to object variables
   Dim Conn As ADODB.Connection, rsPlants As ADODB.Recordset
   Dim strSql As String, strLoc As String
   Set Conn = Application.CurrentProject.Connection
   Set rsPlants = New ADODB.Recordset
   strSql = "Select * from tblPlants"
   strLoc = Me.lstLoc.Value
   
'    'open the recordset
   rsPlants.Open strSql, Conn, adOpenForwardOnly, adLockPessimistic
       'check to see if there is a record in the table
       If Not rsPlants.BOF And Not rsPlants.EOF Then
           With rsPlants
               'if table contains data
'                .AddNew
               .Fields("PlantName").Value = Me.PlantName
               .Fields("BotonName").Value = Me.BotonName
               .Fields("PlantType").Value = Me.PlantType
               .Fields("GrowthSize").Value = Me.GrowthSize
               .Fields("PlantingLoc").Value = strLoc
               .Fields("Description").Value = Me.Description
               .Fields("Pic").Value = Me.Pic
               .Update
           End With
       Else
           'table does not contain data
           With rsPlants
               .AddNew
               .Fields("PlantName").Value = Me.PlantName
               .Fields("BotonName").Value = Me.BotonName
               .Fields("PlantType").Value = Me.PlantType
               .Fields("GrowthSize").Value = Me.GrowthSize
               .Fields("PlantingLoc").Value = strLoc
               .Fields("Description").Value = Me.Description
               .Fields("Pic").Value = Me.Pic
               .Update
           End With
       End If
           
       
   'after new record, allow user to add new record
'    rsPlants.AddNew

   Set rsPlants = Nothing
   DoCmd.GoToRecord , , acNewRec

End Sub
J_Goddard - 21 Oct 2007 14:28 GMT
Hi -

First, check your code.  If I read it correctly, you have an AddNew button on
your form.  When clicked, if there are records in the table, it does not add
a new one right away - it updates whatever the first record happens to be -
the .AddNew is commented out.

But I don't understand your question - you said:

"... I want it to add a new record if there are no records in the table and
add a new record if
there are existing records..."

which means that you want to add a new record whether there are or are not
any existing ones.
So, why bother to check in your code?

Can you clarify this, please?

John

>I am trying to work with recordsets and have the following code. I want it to
>add a new record if there are no records in the table and add a new record if
[quoted text clipped - 65 lines]
>
>End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.