Results 1 to 5 of 5
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670

    Iterating Recordsets

    Good morning. I am not getting an error thrown with my syntax, but I am not getting any data inserted into my table either so I know something is awry. I have used the Debug.Print to verify that my variables hold the appropriate values, and I have manually checked and verified that the Create Table statement is executing and the table is being created, so the issue must lie with my syntax in the recordset.

    Can someone more proficient in the use of recordsets analyze my code and tell me what I need to change so it executes as desired?
    Code:
    	'Variables declared at top of module
    	Public userid As String
    	Public username As String
    	Public userdescription As String
    
    
    	'Variables set in Public Sub
    	userid = "jj21863"
    	username = "James Jones"
    	userdescription = "normal user, no admin rights"
    
    
    	'Statement to create table
    	DoCmd.RunSQL ("Create Table [temp] (userid Text,username Text,userdescription Text)")
    
    
    	'Recordset to iterate the records and insert into the created table
    	Dim rsSelect As DAO.Recordset
    	Dim rsInsert As DAO.Recordset
    	Dim db As DAO.Database
    	Set db = CurrentDb
    	Set rsSelect = db.OpenRecordset("Select [importeddata]." & userid & " FROM [importeddata]")
    	Set rsInsert = db.OpenRecordset("temp")
    	Do While Not rsSelect.EOF
    		rsInsert.AddNew
    		rsInsert!userid = rsSelect.Fields(" & chr(34) & userid & chr(34) & ")
    		rsInsert!username = ConcatRelated("&username&", "[importeddata]", "[userid] =" & rsSelect!userid)
    		rsInsert!userdescription = ConcatRelated("&userdescription&", "[importeddata]", "[userid] =" & rsSelect!userid)
    		rsInsert.Update
    		rsSelect.MoveNext
    	Loop


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    There really no reason to write code for this. A query will do this a hundred times faster that looping thru code.
    and stop any syntax errors.
    Run an append query.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670
    Quote Originally Posted by ranman256 View Post
    There really no reason to write code for this. A query will do this a hundred times faster that looping thru code.
    and stop any syntax errors.
    Run an append query.
    This is a pure simplistic data set for example sake only. I am more curious how to actually use a variable in a recordset in VBA

  4. #4
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670
    Quote Originally Posted by ranman256 View Post
    There really no reason to write code for this. A query will do this a hundred times faster that looping thru code.
    and stop any syntax errors.
    Run an append query.
    If I run this syntax
    Code:
    Debug.Print rsSelect.Fields(userid).Value
    Nothing is printed - which could explain why my table never has data inserted into it. Once I open the recordset, shouldn't I be able to capture the data from any of the fields?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    It looks like the variable holds a value; the code is expecting the name of a field, not a value within that field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. VBA Not Iterating Properly
    By jo15765 in forum Programming
    Replies: 7
    Last Post: 03-30-2017, 04:56 PM
  2. Replies: 3
    Last Post: 12-26-2016, 03:28 PM
  3. About recordsets
    By Lou_Reed in forum Access
    Replies: 4
    Last Post: 11-10-2016, 11:20 AM
  4. Replies: 7
    Last Post: 08-01-2013, 11:52 AM
  5. Recordsets, add new, DAO and ADO
    By Arca in forum Programming
    Replies: 1
    Last Post: 04-07-2008, 03:57 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