Results 1 to 7 of 7
  1. #1
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    153

    Adding a field to a table created byt SELECT INTO

    Thought I had this cracked, needed to created a table on the fly from a query used to populate a report. Easy one-liner:
    Code:
    DoCmd.RunSQL "SELECT qryLoadingSheet1.* INTO [" & wLLTable & "]  FROM qryLoadingSheet1;"
    Then THEY wanted to add an Index field (autonumber) and a blank column.

    The blank column is easy
    Code:
    DoCmd.RunSQL "SELECT qryLoadingSheet1.*, '' AS QReturn INTO [" & wLLTable & "]  FROM qryLoadingSheet1;"
    But how can I add an Index field (increments for each record)?

    Tried


    Code:
    DoCmd.RunSQL "SELECT Id, qryLoadingSheet1.*, '' AS QReturn INTO [" & wLLTable & "]  FROM qryLoadingSheet1;"
    but of course it prompts for Id and replicates the entered value in every record

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    Use a row number public function. In my phone but would be something like

    function rownum() as long
    static r as long

    if isnull(r) then r=0
    rownum=r+1
    r=rownum

    end function

    sql would be

    select rownum as ID, …….

    as I said, on my phone but you may need pass a parameter,

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    Then THEY wanted to add an Index field (autonumber)
    It's a one-time thing, so can't you just open the table in design and add an autonumber field?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    You can create the table with the make-table like you do now then add the autonumber field using DAO or ADO DDL:
    https://stackoverflow.com/questions/...ss-at-run-time
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    153
    Quote Originally Posted by CJ_London View Post
    Use a row number public function. In my phone but would be something like

    function rownum() as long
    static r as long

    if isnull(r) then r=0
    rownum=r+1
    r=rownum

    end function

    sql would be

    select rownum as ID, …….

    as I said, on my phone but you may need pass a parameter,

    Thank you - this looked the most hopeful reply. Unfortunately I have not got it to work. Running with a stop of the rownum function - then yes it is entered, but only once. Having got an answer "1" it proceeds to use it for every row of the table.

    Don't want anyone to spend too much time so I have asked THEY if they can do without the Index. The table is never more than 30 records and other fields have unique qualities - if I was doing what THEY are doing I could easily cope with out it

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,571
    If it is only 30 or so records, then a DCount() would not hurt much?
    https://www.google.com/search?q=gene...hrome&ie=UTF-8
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    Having got an answer "1"
    As I said, you may need to pass a parameter. Now back at my machine and can confirm that is the case. Example sql - note the criteria which 'resets' the static value

    Code:
    SELECT admEvents.*, RowNumber([EventFromDT]) AS PK
    FROM admEvents
    WHERE (((RowNumber())=False))
    and this is the function

    Code:
    Function RowNumber(Optional r As Variant = -1) As Variant
    Static X As Long
    Static s
    
    
        If r = -1 Then
        
            X = 0
    
    
        ElseIf s <> r Then
        
            X = X + 1
            s = r
            
        End If
        
        RowNumber = X
        
    End Function
    Note that the value you pass as a parameter has to be unique from the previous row- this example uses a date field as in this case there is only ever one event for any given date and time. Read and understand the code, you may want to play around with the elseif line and there is nothing to stop you concatenating fields to create a unique value.

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

Similar Threads

  1. Replies: 13
    Last Post: 10-16-2020, 10:44 AM
  2. Replies: 3
    Last Post: 07-15-2016, 03:17 PM
  3. Replies: 3
    Last Post: 05-21-2015, 11:26 AM
  4. Replies: 3
    Last Post: 11-07-2011, 10:41 AM
  5. Replies: 0
    Last Post: 10-14-2009, 02:44 AM

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