Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22

    How to fetch Column names of a table

    Hi All,

    I have a table with 60+ fields, which includes around some 30 dates also as field names.
    Now my requirement is that i need to move those date field-names alone into another table or a query..?



    Ex : Table
    Code:
    Field 1 | Field2 | FIeld3(date) | Field4 (Date) | and so on...
     
    The new table should contain 
    Dates (Column Name )
    -----
    Field3(Date)
    FIeld4(Date)...


    How to do this..

    Thanks in Advance,
    Deepan M

  2. #2
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    A crude way of doing it, if it is a one time operation :
    1) Use a select query on the table which has date fields selecting the Date fields, something like :
    SELECT
    Field3(Date),
    FIeld4(Date),
    ... ,
    .....
    FROM
    myTable
    WHERE
    1 = 2

    2) Export the results of this query to an excel sheet.
    3) In the Excel sheet, you will get the Dates as Headings
    4) Use Paste Special to Transpose the fields.
    5) Import the Transposed data to your table in access.

    Thanks

  3. #3
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Cross Posted at http://www.access-programmers.co.uk/...d.php?t=227338

    See my response there. Please read the following.

    http://www.excelguru.ca/content.php?184

  4. #4
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Hi Recyan,

    plz check the following code.
    I am getting the column names from TBL_PITDATA_DATES
    and move that as entries to TBL_FORECASTDATES

    The first set of code is used to delete the current contents of the table TBL_FORECASTDATES.
    The second of code loops thru the table TBL_PITDATA_DATES and fetched the field names correctly (checked using the Message box), bu t i could not insert them to the table TBL_FORECASTDATES (Insert statement highlighted)

    please help on how to do this insert. Thanks in advance.
    Table TBL_FORECASTDATES contains four fields with first field as DATES into which the contents have to be moved.

    Code:
     Private Sub Command141_Click()
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    strSQL = "SELECT TBL_FORECASTDATES.* FROM TBL_FORECASTDATES"
    Set db = CurrentDb()
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    Do Until rs.RecordCount = 0
    With rs
    .MoveFirst
    .Delete
    .MoveNext
    End With
    Loop
    
    Set db = CurrentDb()
    Set rs1 = db.OpenRecordset("TBL_PITDATA_DATES")
    Dim fld As DAO.Field
    For Each fld In rs1.Fields
    MsgBox (fld.Name)
    
    insert into ("TBL_FORECASTDATES.DATES") by (fld.Name)
    
    Next
    Set fld = Nothing
    End Sub
    Hi Alansidman, Apologies for cross posting.


    Thanks,
    Deepan

  5. #5
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Sorry, Am no good with VBA. Hold on till someone comes along.

    At first glance,
    Code:
    strsqlInsert = "insert into ("TBL_FORECASTDATES.DATES") by (fld.Name)"
            dbs.Execute (strsqlInsert)
    Thanks

    Edit : Just curious : Is, what you are trying to do, not a one time operation ?

    Thanks

  6. #6
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Hi,

    Nope not a single time operation. This code assigned to a button.

    Am getting Runtime Error 424 - Object Required.
    In the Second line.

    Code:
    strsqlInsert = "insert into TBL_FORECASTDATES.DATES by (fld.Name)"
    dbs.Execute (strsqlInsert)
    Thanks for quick response
    - Deepan

  7. #7
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Like I said, Not in to VBA. But then again, till someone comes along :
    What is the table name in to which you are trying to insert the values.
    From what I know
    INSERT INTO tablename (fieldName) VALUES (value).

    Was wondering about the functional environment, in which this kind of operation, is not a one time operation & needs to be repeated again & again ?

    Thanks

  8. #8
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Thanks Recyan...

    It's still throwing error...
    Yes the operation would be repeated daily.
    A system generated excel file which consists dates as column headers will be uploaded everyday.

    Thanks,
    Deepan

  9. #9
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Did you try my suggestion at the other posting.

  10. #10
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Quote Originally Posted by deepanadhi View Post

    Yes the operation would be repeated daily.
    A system generated excel file which consists dates as column headers will be uploaded everyday.
    Now I understand.

    Quote Originally Posted by deepanadhi View Post
    It's still throwing error...
    What is the error ?
    Have you tried step debugging ?
    Also after your insert sql, try this :

    Code:
    strsqlInsert = "insert into TBL_FORECASTDATES.DATES by (fld.Name)"
    Debug.Print strsqlInsert
    dbs.Execute (strsqlInsert)
    This should ideally print the sql statement in the immediate window below.
    Then run one of the insert sql statement in your query window & see what error it throws up.
    Ideally, your sql in the immediate window should look something like below :
    Code:
    insert into MyTable (TheDates) VALUES (#12/31/2012#)
    Edit :
    Sorry Alan, was typing while you were posting.
    God, all the time, I was thinking that the OP was trying the link that you had given ( I had not actually read it, since VBA goes a bit over my head).
    Thanks

  11. #11
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Quote Originally Posted by alansidman View Post
    Did you try my suggestion at the other posting.
    Hi Alan,

    Yeah checked the Link, cant understand much. Way over my knowledge. Am a newbie to both Access and Excel & this is my first project

    Thanks,
    Deepan

  12. #12
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    what happened with the current code that you were trying ?

    Thanks

  13. #13
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Hi Recyan

    It throws me this error

    Runtime Error '3024'
    could not find file "C:\Users\documents\TBL_forecsastdates.mdb"

    This is printed in the immediate window
    Insert into TBL_FORECASTDATES.DATE values (fld.Name)

    Thanks,
    Deepan
    Last edited by deepanadhi; 06-04-2012 at 12:24 AM. Reason: Added Code

  14. #14
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    The Current code..

    [CODE]
    Set db = CurrentDb()
    Set rs1 = db.OpenRecordset("TBL_PITDATA_DATES")
    Dim fld As DAO.Field
    For Each fld In rs1.Fields
    MsgBox (fld.Name)
    strSQL = " Insert into TBL_FORECASTDATES.DATE values (fld.Name)"
    Debug.Print strSQL
    db.Execute strSQL
    Next
    Set fld = Nothing[\CODE]

  15. #15
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Let us give a try till someone comes along :

    1) You have a first table with column names say :
    field1, field2, 6/1/2012, 6/2/2012, 6/3/2012, field6, field7,..
    from which you want to get the field names that are dates.
    If above is true, what is the name of this table ?
    If false, correct the above statement.

    2) You now want to insert these date values in to another table.
    If above is true, what is the name of this second table
    and
    what is the name of the column / field in this table in to which you want to enter these Date values?
    If false, correct the above statement.

    Thanks

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query Entity Names, and Column Names
    By Accidental DBA in forum Queries
    Replies: 1
    Last Post: 07-22-2011, 02:38 PM
  2. Replies: 5
    Last Post: 04-24-2011, 03:14 AM
  3. Need Query to fetch last row in a table
    By gunapriyan in forum Queries
    Replies: 10
    Last Post: 02-20-2010, 12:46 AM
  4. Query to get column names in a table
    By access in forum Queries
    Replies: 1
    Last Post: 07-06-2009, 05:10 AM
  5. Query to get column names in a table
    By access in forum Forms
    Replies: 1
    Last Post: 07-01-2009, 02:50 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