Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the file itself not the file name/path 1

Status
Not open for further replies.

hermanlaksko

Programmer
Aug 26, 2001
941
DK
One can, fairly easy, get a file name/path via a dialogbox however if I would like to get the file itself and save it in access i.e. a doc, xls, bmp, etc. how does one accomplish that.

I know that it can be done via drag/drop ( and I have that working) but I can not find a way to get a file via the dialogbox - Does anyone have a code for this?.


Herman
Say no to macros
 
Hi Remou

Yes I know that one, but it will only give me a path/file name not the file itself.

Herman
Say no to macros
 
Thanks Remou, but that will embed a file on a given form I need the file to end in a table/field as one would do in a drag/drop situation


Herman
Say no to macros
 
I don't know if you can mix drag'n'drop and dialog - or it's usability besides images, but the approach demonstrated by Larry Linson here is what I use for jpg and gifs in an app where I allow the users to load their own logo.

Roy-Vidar
 
Hey Roy
We are moving - this looks like something !
Thx a trillion ;-)


Herman
Say no to macros
 
As far as I know, you will need to use a form unless you wish to write BLOBs. It may be possible with Access 2007, but I do not have a copy to check. Microsoft deprecates storing OLE Objects in Access databases due to bloat, which may explain the dearth of information.
 
Frm!Fil.Action = acOLECreateEmbed

The above gives this error:
"The class argument in the CreateObject function of the Visual Basic procedure you're trying to run is invalid."

But if I drop a file into the field on the form it works, also if I use the RunCommand acCmdInsertObject to get an object it works fine.

However the insertobject command lacks any form of control

This is the full code:

Sub GetFil(Frm As Form)
Dim lngFlags As Long, strFilter As String, strPathAndFile As String
Frm.AllowAdditions = True
Frm.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Alle Filer (*.*)", "*.*")
strFilter = ahtAddFilterItem(strFilter, "Tekst Filer (*.txt)", "*.TXT")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:=GetDBPath, _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, DialogTitle:=SA)
If Len(strPathAndFile) > 0 Then
' Me!fil.Class = "???" 'Unable to find documented Object and Property per KB article
Frm!Fil.OLETypeAllowed = acOLEEmbedded
Frm!Fil.SourceDoc = strPathAndFile
Frm!Fil.Action = acOLECreateEmbed
DoCmd.RunCommand acCmdSaveRecord
Else
MsgBox "No file selected", , SA
End If
End Sub


Herman
Say no to macros
 
sorry, I don't know anything about this (I use the ReadBLOB/WriteBLOB routines for jpg and gif files only), but I'd suspect you'd have to do what the message tells you, give a class name. I don't know many of them, but I suppose "Word.Document" or "Excel.Workbook" should work?

As stated at the bottom here, "NOTE: To determine the Class name of an OLE object, see the documentation for the application supplying the object."

Roy-Vidar
 
Hi Roy

The problem seems to be that I am using a dblclick on the ole field itself. Also I am using a datasheet form,,,, both is a no-no. Hmmm for some odd reason.....

So my solution seems to be, that I must have an extra field on the form, and change the form into a "normal" form.

However when the form is in form view and I dblclick on the ole field, the document is not oppend as a ie. word doc in word ... no it is opened in access - in the ole field - as a tiny instance of word..... Why are they doing this ?

If I change the form into datasheet view it will open normaly in word itself.... ;-)

You just can't win with M$

So the code that works looks like this: (given the above var.)

Sub GetFil(ByVal Frm As Form)
Dim lngFlags As Long, strFilter As String, strPathAndFile As String
Frm.AllowAdditions = True
Frm.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.TXT")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:=GetDBPath, _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, DialogTitle:=SA)
If Len(strPathAndFile) > 0 Then
Frm!DocPath = strPathAndFile
Frm!Fil.OLETypeAllowed = acOLEEmbedded
Frm!Fil.SourceDoc = strPathAndFile
Frm!Fil.Action = acOLECreateEmbed
'DoCmd.RunCommand acCmdSaveRecord
Else
MsgBox "No file was selected", , SA
End If
End Sub

Does the command: RunCommand acCmdInsertObject have a "sister" like RunCommand acCmdOPENObject ?

Herman
Say no to macros
 
Sorry, this is out of my league, I have no idea, I'm not able to help any further.

I just do the gif/jpg stuff to keep my customers happy (ability to change their logo).

When working with other kind of files (pdf, Word, Excel...), I never store more than the path ;-)

Roy-Vidar
 
Ok Roy - I'll still award you a * for your valuable information in this.

Thanks for your help.

Herman
Say no to macros
 
basGrabFile
Code:
Public Function basGrabFile(FilIn As String) As String

    'Michael Red    3/3/2003
    'Sample Usage:  ? basGrabFile("C:\MsAccess\DrawArcsInVB.Txt")
    'Note the Arg [FilIn] is the FULLY QUALIFIED PATH of the Source _
     and the entire text is returned to the caller [prog | procedure]

    Dim MyFil As Integer

    Dim MyTxt As String
    Dim MyPrts() As String
    Dim MyPrtRec() As String

    'Just grab the Stuff
    MyFil = FreeFile

    Open FilIn For Binary As #MyFil

    MyTxt = String(LOF(MyFil), " ")
    Get #MyFil, 1, MyTxt

    Close #MyFil

    basGrabFile = MyTxt

End Function

This will "Grab" the entire. Not just the file name / path. In fact you must provide that info to the function. Further analysis of the file for your purposes is up to you, e.g. finding / knowing the file type; breaking it into seperate 'objects' via delimiters ... )

To see some simple examples, search these (Tek-Tips) fora for the name of the procesure. There are some examples showing how to process text files (CSV?) into individual records and fields. Dealing with other file types is a seperate iessue.



MichaelRed


 
Hi Michael

Thanks for your reply.

The above works fine, but it will only read a file (txt etc.) and store this in the database but, in this case we are looking for code that will store a file as such (doc, exl, bmp, etc.) in an ole-field.

I'll save your code and find use for it elsewhere, but in this case, it will not do the job.

Herman
Say no to macros
 
the Grab works for any/most/all? files, the example is for a text file - partly because that is (or was -at least) an easy one. What to DO with the file is specific to the file (structure in important) and so is left to the user to parse or otherwise manipulate.

When you open the file as BINARY, th file type is irrelevant.



MichaelRed


 
p.s. the return type here is what may be forcing the content to be text.



MichaelRed


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top