Results 1 to 8 of 8
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365

    Exporting Contacts From Access To Excel

    Hi Guy's I haven't started this yet but need to quite quickly!!!



    I have an Excel file called Contacts.xlsx, there are 26 pages named A-B-C To Z

    What is the easiest option to have a command button to export Data form a contact Table to the relevant page

    There is only one issue that i will come across is some contacts can start with a number and that would need to be on page A

    Similar to my last post but setting criteria's where Name starts = <b then name starts with b then name starts with c to got to pages A-B & C

    I haven't generated a starting point yet but just a general idea would help please ?

    ie:

    myCriteria = < B
    Next Criteria = C (Will I need 26 Criterias to cater for the alphabet ?
    Set the rs WHERE Name = < b etc etc
    Open XL File
    With xl Work Sheet A Output the recordset
    Next Output

    I don't want to start without the general so if it's possible, what is the best way to achieve this please ?

    Kindest

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    You can determine if a string contains a number first by using the Val() function
    Air code

    If Val(MyString) > 0 Then ' The contact starts with a number

    You could even add this a a calculated field in your query to drive the page sorting for the export.

    MyExcelWSTab: IIF (Val(MyString) > 0 Then "0-9", Left(MyString,1))
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    adjust the table name / fields as you need

    Code:
    Public Sub export26()
    Dim sSql As String
    Dim vChr
    Dim i As Integer
    Dim qdf As QueryDef
    Const kQRY = "qsNames2Export"
    Dim vFile, vLtr
    
    
    On Error Resume Next
    
    
    vFile = "c:\temp\contacts.xls"
    
    
     sSql = "select * from tClients where left(ucase([lastn]),1)<='A'"
    Set qdf = New QueryDef
    qdf.SQL = sSql
    qdf.Name = kQRY
    CurrentDb.QueryDefs.Append qdf
    
    
    For i = 65 To 90
         vLtr = Chr(i)
         If i = 65 Then   'A or less
            sSql = "select * from tClients where left(ucase([lastn]),1)<='" & vLtr & "' ORDER BY tClients.LastN, tClients.FirstN;"
         Else
            sSql = "select * from tClients where left(ucase([lastn]),1)='" & vLtr & "' ORDER BY tClients.LastN, tClients.FirstN;"
         End If
         
         Set qdf = CurrentDb.QueryDefs(kQRY)
         qdf.SQL = sSql
         qdf.Close
         
         DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, kQRY, vFile, True, vLtr
    Next
    Set qdf = Nothing
    End Sub

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    to remove the starting number perhaps use the val function like this

    ?replace("123ABC",val("123ABC"),"",,1)
    ABC

    Or to get the sheet character

    ?left(replace("123ABC",val("123ABC"),"",,1),1)
    A
    ?left(replace("CBA",val("CBA"),"",,1),1)
    C

  5. #5
    Join Date
    Apr 2017
    Posts
    1,792
    Why do you need a code for this?

    In excel, create an ODBC query which reads your contacts table from your Access database [back-end]

    And no need for have a separate sheet for every litera in Excel. Either
    a) Read all contacts into single table, and use autofilter feature in Excel do display any selection of contacts you want;
    b) Add a Data Validation list to cell at top of sheet with querytable, where user can select a litera (A or B or C etc.), and the query uses this cell as parameter to update returned data immediately, and returns the list of contacts matching for this selection.
    Last edited by ArviLaanemets; 10-02-2021 at 01:03 PM.

  6. #6
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Aren't you making this much more difficult with 26 sheets vs 1 sheet with sorted names? Some sheets will only have a couple of names on them, some none? I have to imagine that this workbook isn't going to be used much, otherwise why not just work with the Outlook address book?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Thank you to all of you who have advised, i have studied your ideas and will get a start on this shortly,

    Micron just to answer, I am maybe thinking down the wrong lines but the reason for this

    Customer updates online (one drive) spreadsheet

    My button opens (contacts file)

    1st Page Called Input we copy and paste from one drive to input page

    Customer only adds Name, Town, Post code

    I need to put more data in and I always think the most unique criteria to DLookup is postcode, Note > we never have 1 company next door to another nor private addresses sp postcodes are all different.

    The issue i have is the customer uploading info can have character incorrect (maybe typo) on their system therefore most of my code doesn't work as postcode doesn't match

    so I thought which ever is incorrect, for quickness click on the relevant page letter: copy postcode, return to 1st page (input page) and paste the postcode we have got

    The database will now add all of the records from the input page

    MY LAST THREAD IS INVOLVED WITH THIS COPY EXCEL TO DB works fantastic but this is something i need to add as I found a flaw (Customer doesn't always have the same postcode)

    I like all of your inputs though and going to read through a few times

    Thank you again to all of you, will let you know how I get on and any more suggestions based on this I will study

    Have a fab day and kindest

  8. #8
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Just adpated ranmans suggestion works great

    also looked at other options and will do some studying

    Thank you guys

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

Similar Threads

  1. Replies: 7
    Last Post: 06-17-2019, 09:41 PM
  2. Replies: 6
    Last Post: 03-18-2019, 07:22 AM
  3. Replies: 4
    Last Post: 12-16-2016, 01:24 PM
  4. Exporting Excel to Access
    By Fletch161 in forum Access
    Replies: 2
    Last Post: 08-12-2015, 12:54 PM
  5. Replies: 10
    Last Post: 12-28-2012, 02:06 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