Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Hi Recyan,

    Both your statements are True.

    1 . Table Name is TBL_PITDATA_DATES
    2. TBL_FORECASTDATES , Column Name is "Date".

    Thanks,
    Deepan M

  2. #17
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Have used code based on your post no 4.
    Have changed the column name "Date" in TBL_FORECASTDATES to "TheDates".
    Do the same at your end & try running below & see if it gives some guidelines :


    Code:
    Private Sub Command141_Click()
        
    ' The declarations
        Dim strsqlDelete As String
        Dim strSQL As String
        Dim db As DAO.Database
        Dim rs As DAO.Recordset

        Dim fld As DAO.Field
        Dim strsqlInsert As String
        '
    -----------------------------------------------------
        
        
    Set db CurrentDb()
        
        
    '-----------------------------------------------------
        ' 
    This empties the myDatesTable if there is any data in it
        
    ' Is it better to Delete the table itself & re-create a new table
        ' 
    or
        
    ' Is it okay to delete all the records from the table ?
        ' 
    Ideallyfirstwe need to check if myDatesTable exists
        
    ' If it does not exist, we need to create an empty myDatesTable
        
        strsqlDelete = "DELETE * FROM TBL_FORECASTDATES"
        db.Execute (strsqlDelete)
        
        '
    -----------------------------------------------------
        
        
    ' Select only the column / field names from the table
        ' 
    as we do not need any data from the table
        
        strSQL 
    "SELECT * FROM TBL_PITDATA_DATES WHERE 1 = 2"

        ' Get the column names with no record returned
        Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
        
        '
    -----------------------------------------------------
        
        
    ' Loop through the column / field names using fld.Name
        For Each fld In rs.Fields
            '
    MsgBox (fld.Name)
            
            
    ' Check if the field name is a Date
            ' 
    since we want only Field Names that are Dates),
            
    ' if yes execute the condition,
            ' 
    else skip
            
            
    If IsDate(fld.NameThen
                
    'MsgBox (fld.Name)
                strsqlInsert = "INSERT INTO TBL_FORECASTDATES (TheDates) VALUES (#" & fld.Name & "#)"
                '
    Debug.Print strsqlInsert
            
                db
    .Execute (strsqlInsert)
            
    End If

        
    Next
        
        
    '-----------------------------------------------------
        
            
        Set fld = Nothing
        rs.Close
        Set rs = Nothing
        MsgBox ("Successful")
    End Sub 
    Thanks

  3. #18
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Thanksalot for the effort & Time Recyan.

    But My bad, it still throws error in the Insert Statement.
    Runtime Error 3134
    Syntax Error in INSERT INTO Satement

    In the Immediate window following is printed..
    INSERT INTO TBL_FORECASTDATES (Date) VALUES (#05/19/2012#)

    Code:
       If IsDate(fld.Name) Then
                'MsgBox (fld.Name)
                strsqlInsert = "INSERT INTO TBL_FORECASTDATES (Date) VALUES (#" & fld.Name & "#)"
                Debug.Print strsqlInsert
                db.Execute (strsqlInsert)
            End If

  4. #19
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Have you tried after replacing the Field name Date with TheDates in TBL_FORECASTDATES?
    Date is a reserved word. Perhaps that is causing the problem.
    After changing,
    retain the original
    INSERT INTO TBL_FORECASTDATES (TheDates) VALUES (#" & fld.Name & "#).

    Thanks

  5. #20
    deepanadhi is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    22
    Quote Originally Posted by recyan View Post
    Have you tried after replacing the Field name Date with TheDates in TBL_FORECASTDATES?
    Date is a reserved word. Perhaps that is causing the problem.
    After changing,
    retain the original
    INSERT INTO TBL_FORECASTDATES (TheDates) VALUES (#" & fld.Name & "#).

    Thanks
    Yup,
    That was the issue all the way
    The Key Word - Date.

    Thanks a lot Recyan, it works great.

    Thanks,
    Deepan.

  6. #21
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Glad you found it helpful.
    As I told you. I am not in to VBA, Be sure to test it properly.

    Thanks

  7. #22
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Hi deepanadhi,

    PMFJI, but you really should normalize your table structure.

    I have a table with 60+ fields, which includes around some 30 dates also as field names.
    You have data (the dates) as field names. Instead of one record with 60 fields (30 date fields), you should have 30 records with 31 fields (unless other field names are also "data").

    From http://support.microsoft.com/kb/283878 :
    First Normal Form

    Eliminate repeating groups in individual tables.
    Create a separate table for each set of related data.
    Identify each set of related data with a primary key.

    Do not use multiple fields in a single table to store similar data. For example, to track an inventory item that may come from two possible sources, an inventory record may contain fields for Vendor Code 1 and Vendor Code 2. (You have dates)

    What happens when you add a third vendor? (or more dates) Adding a field is not the answer; it requires program and table modifications and does not smoothly accommodate a dynamic number of vendors. Instead, place all vendor information in a separate table called Vendors, then link inventory to vendors with an item number key, or vendors to inventory with a vendor code key.
    There are a lot of sites on normalization.......

Page 2 of 2 FirstFirst 12
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