Solved

Access 2007 - vb code

Posted on 2011-02-19
7
470 Views
Last Modified: 2012-05-11
My OS is windows 7, and the code below was submitted by one of the Gurus.  I would like to modify it so that I will be asked if I want to export to a ASCII file or Just as the code is.  If the response yes to the first Question, then could u please write me the code to export to a file.
Thank u
Private Sub btnDistList_Click()
Dim rst As DAO.Recordset
Dim strDistList As String
Set rst = CurrentDb.OpenRecordset("Seniors Club")

Do While Not rst.EOF
    strDistList = strDistList & rst!Email & ";"
    rst.MoveNext
Loop

    strDistList = Left(strDistList, Len(strDistList) - 1)

    Me.txtDistList = strDistList

rst.Close
Set rst = Nothing
End Sub

Open in new window

0
Comment
Question by:jegajothy
7 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 34932822
Export what?
There doesn't appear to be anything to export.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 34932931
You can use a MsgBox if you wish to "export" your strDistList variable:

If Msgbox("Print Email distribution list to file?", vbYesNo, "Print?") = vbYes Then
  Dim lFile As Long
  '/get the next available File handle
  lFile = FreeFileHandle
  '/delete the file if it exists
  If Dir(CurrentProject.Path & "\EmailList.txt") <> "" Then Kill CurrentProject.Path & "\EmailList.txt"  
  '/Now open the file
  Open CurrentProject.Path & "\EmailList.txt" FOR Output As #lFile
  '/write to that file
  Print #lFile, strDistList
  '/now close the file
  Close #lFile
End If


 
 
0
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 250 total points
ID: 34933269
Why not just export the emails directly from the table that the recordset is based on, rather than constructing a string of them?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 34933283
Make a select query with just the email field, and then export it using TransferText:
DoCmd.TransferText transfertype:=acExportDelim, _
      tablename:="qryEmail", _
      FileName:="D:\Documents\Emails.csv", _
      hasfieldnames:=True

Open in new window

0
 

Author Comment

by:jegajothy
ID: 34935181
In response to LSM Consulting, when the code was executed I got a Run time error 52 and it says "Bad file name or number".
Maybe if we  also a define a path, like : C:\Documents, it will prevent the code from going crazy.
Hope u can fix the code.  Thank u for your response.
0
 

Author Comment

by:jegajothy
ID: 34935186
In response to Helen Fedema, sorry being a novice.  But can u please document your code.
What is tablename set to : Is it a query?
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 34969595
transfertype, tablename, FileName, and HasFieldNames are the arguments of the TransferText method.  I like to use named arguments, for greater clarity.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Outlook 2010 not responding when sending email. 4 39
Word and Excel 2013 - Disabled Items List 3 57
Office 2016 Temp Files 3 28
microsoft word short cuts 3 30
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question