Chat with a LIVE Microsoft Access Expert!
 
       
 

         

   

Go Back   Access World Forums > Microsoft Access Discussion > Forms

 
 
Chat with a LIVE Microsoft Access Expert!
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-16-2010, 11:12 AM
Rx_'s Avatar
Rx_ Rx_ is offline
Code not Y3K Compliant!
 
Join Date: Oct 2009
Location: Denver, Colorado
Posts: 238
Rx_ is on a distinguished road
Insert first record into empty record set (based on query)

A form uses a List Box that populates with a query. The query uses another forms ID that is selected. The record set (filterd by the ID) populates the list box. The records in the list box can be updated. It is possible to insert a new record in the list box, refresh the query and have it appear.

Problem - The query returns an empty recordset.
The record set recgonizes the field names, types. But, there are zero records. Try to insert a new record and it errors. And still shows zero recordcount.

Code:
1300            Set rs = Me.Recordset

1340            rs.AddNew ' clears fields add any data, update - ID 99999 is just a place holder  Field 0 is an autocounter
1350     Debug.Print "Record count is " & rs.RecordCount '  (zero returned)                       Debug.print rs.fields(1).name  ' Yes the field is named "ID" 1360                rs.Fields("ID").Value = 99999
1370                rs.Update
1380                strAddNewRecordBookmark = rs.Bookmark
Reply With Quote
Sponsored Links
  #2  
Old 03-16-2010, 11:29 AM
vbaInet vbaInet is offline
AWF VIP
 
Join Date: Jan 2010
Location: U.K.
Posts: 6,324
vbaInet has a spectacular aura aboutvbaInet has a spectacular aura aboutvbaInet has a spectacular aura about
Re: Insert first record into empty record set (based on query)

There might be other ways to achieve this. What is your overall goal? Update the combo box on Form B after a new record in Form A has been inserted?
Reply With Quote
  #3  
Old 03-16-2010, 11:40 AM
Rx_'s Avatar
Rx_ Rx_ is offline
Code not Y3K Compliant!
 
Join Date: Oct 2009
Location: Denver, Colorado
Posts: 238
Rx_ is on a distinguished road
Re: Insert first record into empty record set (based on query)

I am just trying to add a first record to the underlyhing empty record set that populates the list box. When the first record is inserted, the users complete a form and save, then the list box will display the results.

Was looking at some Insert code - Would probably have to
NewData = Replace(NewData, "'", "''")

strSQL = "INSERT INTO Wells ( id ) " & _
"SELECT '" & NewData & "' AS Wells;"
DBEngine(0)(0).Execute strSQL, dbFailOnError
Reply With Quote
  #4  
Old 03-16-2010, 11:49 AM
vbaInet vbaInet is offline
AWF VIP
 
Join Date: Jan 2010
Location: U.K.
Posts: 6,324
vbaInet has a spectacular aura aboutvbaInet has a spectacular aura aboutvbaInet has a spectacular aura about
Re: Insert first record into empty record set (based on query)

I think you might be making things too complex by using a recordset to add to the list box's underlying record source. As long as the listbox is based on the table you're adding to it will automatically including the records. Sometimes all you need is a Refresh command.

So yes, the INSERT statement is the way to go. Here's a link:
http://www.techonthenet.com/sql/insert.php
Reply With Quote
  #5  
Old 03-16-2010, 12:40 PM
Rx_'s Avatar
Rx_ Rx_ is offline
Code not Y3K Compliant!
 
Join Date: Oct 2009
Location: Denver, Colorado
Posts: 238
Rx_ is on a distinguished road
Exclamation Re: Insert first record into empty record set (based on query)

Right on, the listbox is driven off of the recordset, so just disregard that there is a list box. I am just finding there is different code for a form's record set depending on if there are zero records or more than zero.
For zero recordcount, this code worked
Code:
 
1300            Set rs = Me.Recordset
1310
1340            rs.AddNew 
1350      Debug.Print "Record count is " & rs.RecordCount ' zero here
' then add data to two drop list boxes tied to two of these fields
' press an update button that runs this code
1500   DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
 
1600  rs.Update
1610  rs.MoveLast                     ' move to new record
1620            rs.Edit
1630            rs.Fields(1).Value = intMyID
1640            rs.Update
' Now I have added a new record with three field values - two by list box, one by variable
'Note, the Rs.Update would not work until the DoCmd above was run.
' Inother words, the rs.Update would fail on the rs.Edit if it was run first
' the DoCmd statement was not necessary when the record set has one or more records in it

Last edited by Rx_; 03-16-2010 at 12:47 PM.. Reason: speliing
Reply With Quote
  #6  
Old 03-16-2010, 06:27 PM
vbaInet vbaInet is offline
AWF VIP
 
Join Date: Jan 2010
Location: U.K.
Posts: 6,324
vbaInet has a spectacular aura aboutvbaInet has a spectacular aura aboutvbaInet has a spectacular aura about
Re: Insert first record into empty record set (based on query)

Seems you're still back to your initial way of doing things. Your code is wrong. Like I mentioned if all you're doing is adding data to a table (regardless of which control uses it as it's recordsource) then you don't need a Recordset. Recordsets are used for much complex tasks. I had suggested using an Insert query.

A DoMenuItem command does not affect a recordset because a recordset is an object entirely independent of that command.
Reply With Quote
Sponsored Links
Reply

Tags
dao, empty recordset, first insert, query

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Have an 3349 error while exporting data Skip Bisconer Modules & VBA 2 10-18-2008 09:10 PM
An interesting experiment! Need others opinion on different ODBC query behaviors. Banana Queries 12 07-01-2008 08:48 PM
record ids auto number problem. jason2885 Forms 4 07-03-2007 10:16 PM
cannot recreate a query after deleting it in vba ciqala Modules & VBA 2 08-25-2005 05:11 AM
Crosstable query returns too many records Palsam Queries 2 03-09-2004 10:41 AM


All times are GMT -8. The time now is 09:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
(c) copyright 2009 Access World