Results 1 to 5 of 5
  1. #1
    bell is offline Advanced Beginner
    Windows 7 64bit Access 2002
    Join Date
    Feb 2015
    Posts
    37

    VBA Record Manipulation, can you use a string to access a field inside a recordset

    Hi all,



    I would like some help with this problem.

    I have a lot of fields in a table that have the same beginning text name followed by a number, for example some fields may be "Field1" "Field2" ... "Field25" ... etc.

    I am pulling in a lot of data from another database, so to make life easier I wanted to use a Do Loop Until so that I wont need to repeat the same coding steps for each field number, "Field1" "Field2" ... "Field25" ... etc.

    Here's something similar to what I tried

    Code:
    Dim count As Number
    Dim i As Number
    
    Dim str As String
    Dim countStr As String
    
    Dim rs As DAO.Recordset
    Set rs = db.OpenRecordset("Record Set Table", dbOpenDynaset)
    
    i = 0
    count = 1
    
    Do
    
    countStr = count
    str = "Field" + countStr
    
    If IsNull(rs![str]) Then
    'Dont copy the text to the new database
    'End the Do Loop Until
    i = 1
    Else
    'Copy the text to the new database
    '[Code for copying]
    'Increment count to get the next field
    count = count + 1
    End If
    
    Loop Until i = 1
    My problem is rs![str] doesn't work. Is there another way where I can use strings or something similar to access the fields in the tables?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    if you are doing "Field1" "Field2" ... "Field25" , then your db is set up wrong.
    however, to access a field as a variable

    Code:
    dim sFld as string
    dim i as integer
    for i = 1 to 25
      sFld = "Field" & i
      msgbox rst.fields(sFld).value
    next i

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why VBA looping - why not import wizard or TransferDatabase method? If you have a link set to the other database table, why not an UPDATE and/or INSERT query object?

    What other fields are in table? Do you need to copy those as well? Are the field names the same in both tables?
    Last edited by June7; 01-05-2016 at 05:43 PM.
    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
    bell is offline Advanced Beginner
    Windows 7 64bit Access 2002
    Join Date
    Feb 2015
    Posts
    37
    I have to redesign an old database at my job which is setup with fields like Seq1 Seq2 etc. I know that's an awful way to design a database, so I designed a new one much differently. Its was a little hard to just relate both databases together so I thought writing my own code would be easier.

    That being said, ranman256 solution worked for me. Thanks

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    So this is a one-time event? There are non-VBA methods using queries to get the data normalized. A UNION query could rearrange the Seq_ fields into normalized structure and that query could be the source for new table.

    However, glad you got a solution.
    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.

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

Similar Threads

  1. Replies: 3
    Last Post: 12-03-2015, 02:41 PM
  2. basic string manipulation
    By trevor40 in forum Programming
    Replies: 2
    Last Post: 01-30-2015, 05:33 AM
  3. String text manipulation
    By justphilip2003 in forum Access
    Replies: 2
    Last Post: 03-21-2013, 09:56 PM
  4. Replies: 3
    Last Post: 04-23-2012, 12:06 AM
  5. Replies: 2
    Last Post: 01-14-2012, 05:08 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