how do I start wordpad, open a text file, copy the contents, from.

R

russ7046

MSAccess 2002.........how do I run notepad, open a txt file, select contents
and copy contents to a form, using a command button?
 
J

John Nurick

MSAccess 2002.........how do I run notepad, open a txt file, select contents
and copy contents to a form, using a command button?

With considerable difficulty and little reliability, because Notepad
isn't designed for remote control.

Instead, use the VBA function i've posted below, called by code like
this in the button's Click event procedure:

Dim strFileSpec As String

strFilespec = "C:\Folder\file.txt"
Me.MyTextBoxName.Value = FileContents(strFilespec, True)

If you want the user to select the file, use the code at
http://www.mvps.org/access/api/api0001.htm to invoke the File Open
dialog, then pass the resulting filespec to FileContents().


Public Function FileContents( _
ByVal FileSpec As Variant, _
Optional ByVal ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return a error value.
' Alternatively, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function
 

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