Hi Guy's hope you are all well, This following code was posted on here and works totally fantastic
This Transfers all contacts from database to Excel file and generates excel pages A-Z and transfers Contacts starting with A to Excel sheet A, Transfers Contacts Starting with B to Excel sheet B through to Z
If we change Name on the Database for example starting with M so Michael has changed to Mick, can this code be adapted where:
My thought of Option (1)
I have text box perhaps called txtInitial
I type M in that text box
A piece of this code just updates the Excel Page called M and doesn't generate new pages from A-Z ?
My thought of Option (2)
I am unsure if it's easier to delete pages A-Z then run the code I have so you guy's may advise an adaption of this code that deletes pages and use this code to generate new again ?
Note: I want to keep this code as works fab, I think ranman posted it
Note: these XL sheets are from sheet 4 as the first 3 sheets are template sheets to copy contacts from, So I DO NOT WANT TO DELETE the FIRST 3 Sheets only where the sheet names are A-Z
Much appreciated
Code:
Dim sSql As String, srcPath As String, srcFile As StringDim vChr
Dim i As Integer
Dim qdf As QueryDef
Const kQRY = "qsNames2Export"
Dim vFile, vLtr
On Error Resume Next
srcPath = "T:\DMT Ltd\XL Files\"
srcFile = "Contacts.xlsx"
vFile = srcPath & srcFile
sSql = "select * from tblDealers where left(ucase([Name]),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 tblDealers.Name, tblDealers.Town, tblDealers.Postcode from tblDealers where left(ucase([Name]),1)<='" & vLtr & "' ORDER BY tblDealers.Name, tblDealers.Name;"
Else
sSql = "select tblDealers.Name, tblDealers.Town, tblDealers.Postcode from tblDealers where left(ucase([Name]),1)='" & vLtr & "' ORDER BY tblDealers.Name, tblDealers.Name;"
End If
Set qdf = CurrentDb.QueryDefs(kQRY)
qdf.Sql = sSql
qdf.Close
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, kQRY, vFile, True, vLtr
Next
Set qdf = Nothing