Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316

    Unanswered: How to assign a field name to a string?

    I thought it would be something like this:
    Code:
    strCurFieldName = rs.Fields.Item(3)
    But this is giving strange results. Can anyone help?

  2. #2
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    maybe something like this (DAO) approach can help. i use this for filling a form from a recordset.

    dim recs As DAO.recordset
    dim rfld As DAO.field
    ......
    for each rfld in recs.fields
    me(rfld.name) = rfld
    next

    izy
    currently using SS 2008R2

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by bcass
    I thought it would be something like this:
    Code:
    strCurFieldName = rs.Fields.Item(3)
    But this is giving strange results. Can anyone help?
    A few things -
    If you want to assign the fieldname then you need to access the name property - the default property for the fields is the value. (as demonstrated in Izys post).
    Remember the collection is zero based so item three is the fourth item.
    Changes to the ordinal position of the field (i.e. changes to table structure or aselect statement may affect this) could result in problems. Although using the item number is more efficient, referring to the field name is more robust. If, of course, you are trying to find out the field name, ignore.

    If above is no help, please could you me more clear about "strange results" - i.e. waht do you want, what do you get and why are you trying to do this

    HTH
    Testimonial:
    pootle flump
    ur codings are working excelent.

  4. #4
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316
    The strange results I was getting were field contents (when using rs.Fields.Item(3)) - this I now know is because this defaults to the value. Anyway, I sussed it:
    Code:
    Dim cnn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim strQry As String
    Dim strCurField As String
    
        Set cnn = CurrentProject.Connection
    
        strQry = "SELECT * FROM tblMapping;" ' put your table here
        rs.Open strQry, cnn, adOpenStatic, adLockReadOnly
          
        strCurField = rs.Fields.Item(1).Name
            
        MsgBox (strCurField)
        
        rs.Close
        cnn.Close
        Set rs = Nothing
        Set cnn = Nothing
    Basically, I needed to ascertain the field name prior to knowing it.

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by bcass
    Basically, I needed to ascertain the field name prior to knowing it.
    Makes perfect sense to me
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Oct 2005
    Posts
    178
    Quote Originally Posted by bcass
    The strange results I was getting were field contents (when using rs.Fields.Item(3)) - this I now know is because this defaults to the value. Anyway, I sussed it:
    Code:
    Dim cnn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim strQry As String
    Dim strCurField As String
    
        Set cnn = CurrentProject.Connection
    
        strQry = "SELECT * FROM tblMapping;" ' put your table here
        rs.Open strQry, cnn, adOpenStatic, adLockReadOnly
          
        strCurField = rs.Fields.Item(1).Name
            
        MsgBox (strCurField)
        
        rs.Close
        cnn.Close
        Set rs = Nothing
        Set cnn = Nothing
    Basically, I needed to ascertain the field name prior to knowing it.
    if you knew the index of the field you wanted to know the name of use this instead in your code..
    for example you want the first field in your table index would be zero..

    strCurField = rs.Fields(0).Name
    Last edited by fredservillon; 01-09-06 at 13:04.

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by fredservillon
    if you knew the index of the field you wanted to know the name of use this instead in your code..
    for example you want the first field in your table index would be zero..

    strCurField = rs.Fields(0).Name
    Hi Fred
    The two lines are basically the same. They are like
    Code:
     rs.Fields(0)
    rs.Fields(0).Value
    The Item property is the default property of the Fields collection. As such it is optional (as per your code) but can be included (as per bcass).
    Testimonial:
    pootle flump
    ur codings are working excelent.

  8. #8
    Join Date
    Oct 2005
    Posts
    178
    Quote Originally Posted by pootle flump
    Hi Fred
    The two lines are basically the same. They are like
    Code:
     rs.Fields(0)
    rs.Fields(0).Value
    The Item property is the default property of the Fields collection. As such it is optional (as per your code) but can be included (as per bcass).
    YOu're right if you are wanting the record content of the field. But If you wanted the name of the field you need to specify the name property..ALso
    the item(x) inserted would return of the item list of the array variable
    Last edited by fredservillon; 01-12-06 at 12:21.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •