Import text file into Memo field

davesmith202

Employee of Access World
Local time
Today, 11:20
Joined
Jul 20, 2001
Messages
522
I have mydata.txt on my Desktop. There is a frmMyData with a texbox called MyData, who's source is the mydata field in tblMyData.

I want to click a command button to pop up a File Open box where I can search for the file, select it and then it gets imported into the current memo field.

How can I do that?

Thanks,

Dave
 
I have mydata.txt on my Desktop. There is a frmMyData with a texbox called MyData, who's source is the mydata field in tblMyData.

I want to click a command button to pop up a File Open box where I can search for the file, select it and then it gets imported into the current memo field.

How can I do that?

Thanks,

Dave

As for openFileDialog, I got a version of it working based on examples from these sites: http://www.access-programmers.co.uk/forums/showthread.php?t=97787&highlight=dialog
http://www.mvps.org/access/api/api0001.htm
http://support.microsoft.com/kb/888695

The code I used is below (I hope this was the tested version, can't recall). In some cases you can pass in an empty string (because some of the params are optional).

Sample filter string:
sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0) & _
"JPEG Files (*.JPG)" & Chr(0) & "*.JPG" & Chr(0)


Private Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean

Private Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long


Private Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

'Pass in a boolean indicating whether this is a SaveFileDialog. If false, it will be an ope fileEDialog.
Private Function ShowFileDialog(ByVal SaveFileDialog as Boolean, strFilter as string, strTitle as string, Byval strInitialDirec as string)
Dim OFN As tagOPENFILENAME
Dim strFileName As String, strFileTitle As String
strFileName = Left(strFileName & String(256, 0), 256)
strFileTitle = String(256, 0)
With OFN
.lStructSize = Len(OFN)
.hwndOwner = Application.hWndAccessApp
.strFilter = strFilter
.nFilterIndex = 0
.strFile = Left(strFileName & String(256, 0), 256)
.nMaxFile = Len(strFileName)
.strFileTitle = String(256, 0)
.nMaxFileTitle = Len(strFileTitle)
.strTitle = strTitle
.Flags = 0
.strDefExt = ""
.strInitialDir = strInitialDirec
.strCustomFilter = ""
.nMaxCustFilter = 0
.lpfnHook = 0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
End With
if saveFileDialogThen aht_apiGetSaveFileName OFN
Else aht_apiGetOpenFileName
ShowFileDialog = OFN.strFile
End sub



**********


As for getting the textfile into a Memo field, you can read the entire textfile into a string like this:


Open "C:\MyFile.txt" For Input As #3
Dim str As String
str = Input$(LOF(3), 3)
Close #3

Then you can insert it into your table doing something like this:

Dim cmd as New Adodb.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandText = "INSERT INTO table1 (myMemo) VALUES(@myMemo) "
cmd.Parameters.Refresh
cmd.Parameters(0).Value = str
cmd.Execute
 
Last edited:

Users who are viewing this thread

Back
Top Bottom