?
Solved

Access 2007 - vb code

Posted on 2011-02-19
7
Medium Priority
?
479 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 77

Expert Comment

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

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1000 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 1000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Suggested Courses

765 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