Results 1 to 15 of 15
  1. #1
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18

    Select distinct, access to excel

    Hi,

    I would like to put into an array a column from an access table.
    I have a lot of redundant data, is someome could help me to put my data into an array?
    Then I will filter the data by my own.
    I've found some code but didn't work :/



    Many thanks

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    if you're looking for duplicates there is a FIND DUPLICATES wizard for making new queries.

  3. #3
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Hi,
    thanks for thanks, but I should be able to send this to an excel workbook then, you will tell me that it's not a problem.

    But how can i put into an array one column? I think that is possible, but I can't code it everything I did, didn't work.

    I was thinking to do that first and then into my array look every different value (which should create one column into a excel workbook)

    Do you have any advice?

    Many thanks

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why do you need to use code and array just to eliminate duplicates? This should be possible with query.

    SELECT DISTINCT fieldname FROM tablename;

    or

    SELECT fieldname FROM tablename GROUP BY fieldname;

    Related/duplicate thread https://www.accessforums.net/program...cel-31922.html
    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.

  5. #5
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Wouhou, here we go, I did it, thanks to you.
    This is my code :

    Sub Travaux()

    Dim Rs As ADODB.Recordset
    Dim sSQL As String
    Dim VCategorie As String
    sSQL = "SELECT DISTINCT Categorie FROM Table1"
    Set Rs = New ADODB.Recordset
    Rs.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    Rs.MoveFirst
    Do Until Rs.EOF
    VCategorie = Rs("Categorie")

    Debug.Print VCategorie

    Rs.MoveNext
    Loop
    Rs.Close
    End Sub


    Now I have to create new column into my excel workbook, from the different categorie!
    I will have to do it in my loop, what do you think?

    Many thanks

  6. #6
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Hi, I'm still on it, but close to the end.
    I was thinking to do something like that, could you help me please?

    Many thanks

    Sub WorkC()

    Dim Rs As ADODB.Recordset
    Dim sSQL As String
    Dim VCategorie As String
    Const cStartRow As Byte = 5
    Const cStartColumn As Byte = 5
    iCol = cStartColumn
    iRow = cStartRow
    sSQL = "SELECT DISTINCT Categorie FROM Table1"
    Set Rs = New ADODB.Recordset
    Rs.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic

    Set appexcel = appexcel.Workbooks.Open("C:\target workbook")
    appexcel.Visible = True
    appexcel.Sheets("Report").Select

    Rs.MoveFirst
    Do Until Rs.EOF
    VCategorie = Rs("Categorie")
    Debug.Print VCategorie

    'Check if the value already existed (compare value from range from E5 to O5)
    'If iFld is not exited then add value to the next empty cell of the range E5:O5

    'appexcel.Cells(iRow, iCol) = VCategorie
    'iCol = iCol + 1
    'Else do nothing

    Rs.MoveNext
    Loop
    Rs.Close
    End Sub

  7. #7
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Done, I share it.
    Can be improve, but it works!
    Thanks June7
    How it looks like?

    Sub WorkC()

    Dim Rs As ADODB.Recordset
    Dim sSQL As String
    Dim VCategorie As String
    Dim celluletrouvee As Range
    Const cStartRow As Byte = 5
    Const cStartColumn As Byte = 5
    iCol = cStartColumn
    iRow = cStartRow
    sSQL = "SELECT DISTINCT Categorie FROM Table1"
    Set Rs = New ADODB.Recordset
    Rs.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic

    Set appexcel = CreateObject("Excel.Application")
    appexcel.Visible = True
    Set wbexcel = appexcel.Workbooks.Open("Path workbook")
    appexcel.Sheets("Sheet1").Select

    Rs.MoveFirst
    Do Until Rs.EOF
    VCategorie = Rs("Categorie")
    Debug.Print VCategorie

    Set celluletrouvee = Range("E5:O5").Find(VCategorie, lookat:=xlWhole)

    'Check if the value already existed
    If celluletrouvee Is Nothing Then

    appexcel.Cells(iRow, iCol) = VCategorie

    Else
    End If
    Rs.MoveNext
    iCol = iCol + 1
    Loop
    Rs.Close
    End Sub

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    As long as it works, looks good. Let us know if you have specific issue, otherwise call it solved.
    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.

  9. #9
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Hi, I would like a last device!
    I would like to konw the best way to export row from table (TransferSpreadsheet ?) into a workbook and placed them.
    I had attached one excel example, from access how to export two columns (field Town and company) and put one column (field work) below the correct Categorie (from my previous code)
    I was trying with query, but it's doesn't look the best way to go!
    (once finish I will share it, is it possible to change the name of the thread?)
    Many thanks in advance
    Regards

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Need to know more about your data structure. Sounds like you need to transpose data with a CROSSTAB query. Provide sample of source data and what the final output should look like. If you want to attach database, follow instructions at bottom of my post.

    What do you want to name the thread?
    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.

  11. #11
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Ok thank you i'm going to check crosstab query!
    I attached an example of structure that i'm looking for :
    I would like to change the name to help other people, I don't know something more efficient with keywords, like "Select distinct, access to excel"
    Thanks

  12. #12
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    With attachement is better Structure-Example.zip

    Regards

  13. #13
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Thanks, to rename it
    CrossTab looks complicate!
    I would like to modifie something, in my access table and field Categorie it will be always by order A, B, C ...
    Then in my workbook, I will add the field "Town" and "Company" from access always into column A6 and B6 (workbook) and my "work" always into categorie A (column E5) , then categorie B (column F5) etc ...
    I think it would be easier, do you?
    Regards

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    CROSSTAB not so tough. This will result in structure that looks like the Excel:

    TRANSFORM First(Table1.[Work]) AS FirstOfWork
    SELECT Table1.[company], Table1.[town]
    FROM Table1
    GROUP BY Table1.[company], Table1.[town]
    ORDER BY Table1.[company], Table1.[town]
    PIVOT Table1.[categorie];

    Copy/paste that into the SQL VIEW of query designer. Then look at the query design in Design View. Practice recreating that sql with the CROSSTAB design wizard.

    So do you still need to export to Excel?
    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.

  15. #15
    dacodac is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2013
    Posts
    18
    Hi, and sorry I haven't had time to reply before!
    Woah awesome! Thanks for this new thing
    And yes I will try to export it to excel (i use it like a report), but thanks to that it would be easier to export my data to excel!
    I don't have to be worry about to place it, I just have to add some columns in my excel file between the 2 first one and the message.
    Thanks again

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

Similar Threads

  1. Select distinct
    By Paul-NYS in forum Access
    Replies: 1
    Last Post: 07-09-2012, 09:03 AM
  2. SELECT DISTINCT or equivalent
    By kpo in forum Programming
    Replies: 13
    Last Post: 05-20-2012, 01:45 PM
  3. How to use variable in SELECT DISTINCT
    By celtics11 in forum Access
    Replies: 1
    Last Post: 11-18-2011, 04:28 PM
  4. Replies: 8
    Last Post: 04-21-2011, 05:29 PM
  5. SELECT DISTINCT not working
    By johnmerlino in forum Queries
    Replies: 2
    Last Post: 10-25-2010, 06:48 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