export to text file

G

Guest

Hi

I have a Form that is in the layout of a label for medicines. I need to
export the data that is displayed on the Form to a *txt file at the click of
a button. The output of the *txt file should look like this:


LEAFLETNO = " "
PRODUCT1 = " "
PRODUCT2 = " "
QTY = " "
LINE1 = " "
LINE2 = " "
LINE3 = " "
LINE4 = " "
LINE5 = " "
MAH1 = " "
MAH2 = " "
MAH3 = " "
MANU1 = " "
MANU2 = " "
EU = " "
RPD1 = " "
RPD2 = " "

The quotation marks should contain the text that is displayed on the open
Form. Is this possible??? Help would be appreciated.
 
G

Guest

Add the following procedure to the form's module:

Private Sub WriteToFile(strPath As String)

Open strPath For Output As #1
Print #1, "LEAFLET NO = " & Me.txtLeafletNo
Print #1, "PRODUCT 1 = " & Me.txtProduct1
' and so on
Print #1, "RPD2 = " & Me.txtRPD2
Close #1

End Sub

where txtLeafletNo, txtProduct1 etc are the names of the controls on the form.

Call the procedure in the button's Click event procedure , passing the path
to the text file into it as its argument. You might for instance enter the
path in another text box on the form (or better still open a common dialogue
to browse to a folder and select or enter a file name and assign the path to
the text box), so you'd call it like so:

WriteToFile Me.txtPath

Ken Sheridan
Stafford, England
 
G

Guest

Hi

I have done the coding for the 'write to file' but I am unsure of what you
mean about what should go in the command buttons procedure.
 
G

Guest

You call the WriteToFile procedure in the Click event procedure of a button
on the form. The procedure requires the path to the file to be created as
its argument so to call it you need to follow the name of the procedure with
the path to the file this could be a literal path, e.g.

WiteToFile "C:\MyFiles\MyFile.txt"

but more usually you would be able to assign a path at runtime. This could
be entered in a text box on the form as I suggested so you'd then reference
the text box:

WriteToFile Me.txtPath

or you could allow the user to browse to the file by opening a common
dialogue with the button and assigning the path selected by the user in the
dialogue. You can download code for opening a common dialogue from many
places but the one I normally use is Bill Wilson's BrowseForFileClass class
module which van be obtained from:


http://community.netscape.com/n/pfx...yMessages&tsn=1&tid=22415&webtag=ws-msdevapps


Having installed Bill's module in your database the code for the button's
Click event procedure to open it, assign the selected path to a text box
txtPath, and call the WriteToFile procedure could be:

On Error GoTo Err_Handler

Dim OpenDlg As New BrowseForFileClass
Dim strPath As String
Dim strAdditionalTypes As String
Dim strMessage As String
Dim blnOverwrite As Boolean

blnOverwrite = True

' open common dialogue to browse to file
OpenDlg.DialogTitle = "Select File"
OpenDlg.AdditionalTypes = _
"Text Files (*.txt; *.csv; *.tab; *.asc) |*.txt; *.csv; *.tab; *.asc"
strPath = OpenDlg.GetFileSpec
Set OpenDlg = Nothing

Me.txtPath = strPath

' get user confirmation of write operation
strMessage = "Write data to file " & strPath & "?"
If MsgBox(strMessage, vbYesNo + vbQuestion, "Confirm Write Operation") =
vbYes Then
' if file already exists get user confirmation to overwrite it
If Dir(strpath) <> "" Then
strMessage = "File " & strpath & " already exists. Overwrite?"
If MsgBox(strMessage, vbYesNo + vbQuestion, "Confirm Overwrite")
= vbNo Then
blnOverwrite = False
End If
End If
' call procedure to write data to file if overwrite confirmed
' or file doesn't already exist
If blnOverwrite Then
WriteToFile strpath
End If
End If

Exit_here:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error"
Resume Exit_here

You don't actually need the text box for this, but you might feel it better
for the user to see the path on the form rather than merely in the message
box.

Ken Sheridan
Stafford, England

Kirt84 said:
Hi

I have done the coding for the 'write to file' but I am unsure of what you
mean about what should go in the command buttons procedure.
 
G

Guest

I missed a few lines of code in my last post. It should have read:

On Error GoTo Err_Handler

Dim OpenDlg As New BrowseForFileClass
Dim strPath As String
Dim strAdditionalTypes As String
Dim strMessage As String
Dim blnOverwrite As Boolean

blnOverwrite = True

' open common dialogue to browse to file
OpenDlg.DialogTitle = "Select File"
OpenDlg.AdditionalTypes = _
"Text Files (*.txt; *.csv; *.tab; *.asc) |*.txt; *.csv; *.tab; *.asc"
strPath = OpenDlg.GetFileSpec
Set OpenDlg = Nothing

Me.txtPath = strPath

' get user confirmation of write operation
strMessage = "Write data to file " & strPath & "?"
If MsgBox(strMessage, vbYesNo + vbQuestion, "Confirm Write Operation") =
vbYes Then
' if file already exists get user confirmation to overwrite it
If Dir(strpath) <> "" Then
strMessage = "File " & strpath & " already exists. Overwrite?"
If MsgBox(strMessage, vbYesNo + vbQuestion, "Confirm Overwrite")
= vbNo Then
blnOverwrite = False
Else
' delete existing file
Kill strpath
End If
End If
' call procedure to write data to file if overwrite confirmed
' or file doesn't already exist
If blnOverwrite Then
WriteToFile strpath
End If
End If

Exit_here:
Exit Sub

Ken Sheridan
Stafford, England
 
G

Guest

Hi Ken

Thanks a lot for your help. While working on this I have actually been
notified that I do not require a dialogue box just the function of clicking
the command button and writing to file. I have also been informed that I
require the output to have speech marks within the text:

LEAFLETNO = " "
PRODUCT1 = " "
PRODUCT2 = " "
QTY = " "
etc...

Is this possible??
 
G

Guest

If you want to overwrite the same file each time the data is written and the
path to the file is constant then you can simply call the function, passing
the path as a string, e.g.

WriteToFile "F:\SomeFolder\SomeFile.txt"

If you want to append the data to existing data in the file then change the
function so that it opens the file for Append rather than Output.

To include literal quotes around the values of the include pairs of
contiguous quotes characters for each literal quote within each expression.
A contiguous pair of quotes within a string delimited by quotes is
interpreted as a literal quotes character e.g.

Print #1, "LEAFLET NO = """ & Me.txtLeafletNo & """"

Ken Sheridan
Stafford, England
 
G

Guest

Thanks so much for your help

And yes I do want to clear the data each time i press the command button.
Therefore, i changed the function to Append rather than Output. However, each
time i export to file the data from before is still in the text file and the
data that was exported appears underneath. Any reason for this?
 
P

Pieter Wijnen

Which is opposite of what you want
Output = Overwrite
Append = aah well, Append

Pieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top