Solved

Access 2007 - vb code

Posted on 2011-02-19
7
463 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now