How to Attach a file in VB


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: How to Attach a file in VB

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    6

    Question How to Attach a file in VB

    hi all,
    i am new bie to vb and to this forum. i have started learning vb and i am doing project of my own in my pc.

    i don't know how to attach a file in a form.(not in mail). ie i have a form where the user were allowed to attach a file. the details should be stored in database and when the user come later he can able to see the file he has attached.

    i know how to attach a file in php and i have done lot of project using php but i don't know how to do the same in vb.

    plz help me to solve this problem.

    Thanks in advance,
    S.Azhagiri

  2. #2
    Join Date
    Dec 2003
    Posts
    2,750
    What do you mean by attach? I don't really know anything about PHP so I'm not sure what the equivalent would be in Visual Basic.

    Perhaps you're looking for the Visual Basic OLE control?
    Paul
    ~~~~
    Microsoft MVP (Visual Basic)

  3. #3
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819

    Arrow

    Or maybe u want the commn dialog control, do u want a browse button with a textbox to choose a file and put path in it for example ? or what do u want to do exactly ?
    Last edited by Amahdy; 01-22-2007 at 09:32 PM.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  4. #4
    Join Date
    Jan 2007
    Posts
    6

    how to attach a file in vb

    hi amahdy & paul,
    thanx for ur reply. i want a browse button with a textbox to choose a file and put path. so that when the user clicks the submit button all the details should be stored in the database. in php we will create a folder in server and we will store the file in that folder and in the database we will give the path of that file which matches the path in the common folder (ie while from the file textbox we will extract only the file name)

    but i don't know how to do the same in the vb.

    the reason why i want this is : in my case i should allow the user to store the file they have completed in a day and submit it for evaluation. later the evaluator can come to his panel and can see the work of the user and can evaluate by opening the file.

    i want to attach all type of files like word/excel/psg/html/flash etc.

    is there any way to do this.

    plz help me i am struck with this point.

    hope i have explained my status clearly

    awaiting for ur reply ASAP

    Thanks & regards,
    S.Azhagiri

  5. #5
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    As far as browsing is concerned, in VB you want the CommonDialog control. Then you set the properties, like dialog title, the filter ("All Files |*.*"), Default extension, Initial Directory, ect., then call the ShowOpen method.
    If the len of the FileName property is not zero then you got the filename. You will need to seperate the actual file name from the path.
    At this point you have the file name so you can use FileCopy to put a copy in a common directory.
    CommonDialog And FileCopy are all in the VB Help files, so you can look them up and see all of the various options.

  6. #6
    Join Date
    Jan 2007
    Posts
    6

    how to attach a file in vb

    Quote Originally Posted by Ron Weller
    As far as browsing is concerned, in VB you want the CommonDialog control. Then you set the properties, like dialog title, the filter ("All Files |*.*"), Default extension, Initial Directory, ect., then call the ShowOpen method.
    If the len of the FileName property is not zero then you got the filename. You will need to seperate the actual file name from the path.
    At this point you have the file name so you can use FileCopy to put a copy in a common directory.
    CommonDialog And FileCopy are all in the VB Help files, so you can look them up and see all of the various options.
    Hi ron,
    thanx for ur reply. i got the logic what u said. i would be happy if u give me
    some sample code for showopen() and for filecopy() method. the other doubt i have is how can i find out the len of file name.

    sorry to trouble u once again as i am new bie to vb i dont have much idea on how to use these function. i wuld be happy if u give some url which explains all these functions in detail.

    awaiting for ur reply

    Thanx and Regards,
    S.Azhagiri

  7. #7
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    Here is a little function that you can add to your forms module. Make sure to add a commondialog control to the form.
    Code:
    Function StoreFile() As String
      'Make sFile Static so it will remember the last file selected
      Static sFile As String
      Dim sPath As String
      Dim p As Long
    
      With CommonDialog1
        .DialogTitle = "Store File"
        .CancelError = False
        .Filter = "All Files (*.*)|*.*"
        .DefaultExt = ".txt"
        .FileName = sFile
        .InitDir = CurDir
        .CancelError = True
        Err.Clear
        On Error Resume Next
        .ShowOpen
        If Len(.FileName) = 0 Or Err.Number <> 0 Then GoTo Done
        On Error GoTo Err
        sFile = .FileName
      End With
    
      'seperate Path From File Name
      p = InStrRev(sFile, "\")
      sPath = Left(sFile, p)
      sFile = Mid(sFile, p + 1)
    
      'copy file to programs install directory
      FileCopy sPath & sFile, App.Path & "\" & sFile
    
      'Return File Name So it can be saved
      StoreFile = sFile
    
    Done:
      Exit Function
    Err:
       MsgBox Err.Description, vbCritical, "StoreFile"
       Resume Done
       Resume
    
    End Function

  8. #8
    Join Date
    Jan 2007
    Posts
    6

    how to attach a file in vb

    Quote Originally Posted by Ron Weller
    Here is a little function that you can add to your forms module. Make sure to add a commondialog control to the form.
    Code:
    Function StoreFile() As String
      'Make sFile Static so it will remember the last file selected
      Static sFile As String
      Dim sPath As String
      Dim p As Long
    
      With CommonDialog1
        .DialogTitle = "Store File"
        .CancelError = False
        .Filter = "All Files (*.*)|*.*"
        .DefaultExt = ".txt"
        .FileName = sFile
        .InitDir = CurDir
        .CancelError = True
        Err.Clear
        On Error Resume Next
        .ShowOpen
        If Len(.FileName) = 0 Or Err.Number <> 0 Then GoTo Done
        On Error GoTo Err
        sFile = .FileName
      End With
    
      'seperate Path From File Name
      p = InStrRev(sFile, "\")
      sPath = Left(sFile, p)
      sFile = Mid(sFile, p + 1)
    
      'copy file to programs install directory
      FileCopy sPath & sFile, App.Path & "\" & sFile
    
      'Return File Name So it can be saved
      StoreFile = sFile
    
    Done:
      Exit Function
    Err:
       MsgBox Err.Description, vbCritical, "StoreFile"
       Resume Done
       Resume
    
    End Function

    hi ron,
    once again thanx for ur reply. and i have tryed the similar type of code in my pc yesterday it was working fine and i am able to store the file path in the database and the file is getting stored in the directory which i have specified.

    everything is working fine for me. but in the admin mode i am diplaying the link to the admin where the file is located. when the admin clicks the links either the file has to open or it should be downloaded in his system.

    but it is not happening to me. i was trying to open the file using ShellExecute API function but it was not opening. is there any way to do the download the file when the admin clicks the link.

    i once again thanx u for showing favour for me and giving the code.

    waiting for ur reply

    Thanx and Regards,
    S.Azhagiri

  9. #9
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    If you copy the file to a common directory, then you do not realy need the path because you know it is in the common directory. All you need to save it the file name itself. As far as opening the file you need to reference it relative to the server. As you see here in my sample I reference the file from my server "Dell-xps". The standard URL reference for a server is with the double slashes (\\Dell-xps). The rest is from the servers C drive to the All Users documents and music folder and of course the file to open.
    Code:
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    'Launch Any Registered File Without Knowing The Associated App.
    
    Public Function LaunchApp(ByVal URL As String) As Long
      On Error Resume Next
      Dim strFile As String
      strFile = ShellExecute(0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus)
    End Function
    
    Sub OpenFile()
      Dim file As String
      
      'File and path need quotes qround them so file = """..."""
      
      file = """\\Dell-xps\C\Documents and Settings\All Users\Documents\My Music\Vladimir_Horowitz-Etude_In_C_Minor_Op_25_No_12_Chopin.mp3"""
      LaunchApp file      'opens the file in it's appropriate app
    
    End Sub

  10. #10
    Join Date
    Jan 2007
    Posts
    6

    how to attach a file in vb

    Quote Originally Posted by Ron Weller
    If you copy the file to a common directory, then you do not realy need the path because you know it is in the common directory. All you need to save it the file name itself. As far as opening the file you need to reference it relative to the server. As you see here in my sample I reference the file from my server "Dell-xps". The standard URL reference for a server is with the double slashes (\\Dell-xps). The rest is from the servers C drive to the All Users documents and music folder and of course the file to open.
    Code:
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    'Launch Any Registered File Without Knowing The Associated App.
    
    Public Function LaunchApp(ByVal URL As String) As Long
      On Error Resume Next
      Dim strFile As String
      strFile = ShellExecute(0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus)
    End Function
    
    Sub OpenFile()
      Dim file As String
      
      'File and path need quotes qround them so file = """..."""
      
      file = """\\Dell-xps\C\Documents and Settings\All Users\Documents\My Music\Vladimir_Horowitz-Etude_In_C_Minor_Op_25_No_12_Chopin.mp3"""
      LaunchApp file      'opens the file in it's appropriate app
    
    End Sub
    ya i accept ur point that we don't need the path if the file is stored in common directory. but in my case when the admin open his panel he/she has to view the other user request along with their attachment. to let the admin know that there is an attachment with the message i am giving some text as "attachment" after the message. when the user click the attachment it has to open the attached file.

    i am working on the code given by u. i have doubt in the code. if i am using the application in my local system how can i reffer the file and open it. (u told that from server u refer the file by \\Dell-xps\ ). if it is local system can we use the directory directly such as C\Documents and Settings\All Users\Documents\My Music\Vladimir_Horowitz-Etude_In_C_Minor_Op_25_No_12_Chopin.mp3 )

    Thanx and Regards,
    S.Azhagiri

  11. #11
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    I see, so you are tracking the entire transaction, that makes sence.

    Yes you can reference the file locally, in my original example, if that were a local reference then you would leave off the server part and add back in the colon in the drive reference, like this:
    C:\Documents and Settings\All Users\Documents\My Music\Vladimir_Horowitz-Etude_In_C_Minor_Op_25_No_12_Chopin.mp3

  12. #12
    Join Date
    Jan 2007
    Posts
    6

    how to attach a file in vb

    Quote Originally Posted by Ron Weller
    I see, so you are tracking the entire transaction, that makes sence.

    Yes you can reference the file locally, in my original example, if that were a local reference then you would leave off the server part and add back in the colon in the drive reference, like this:
    C:\Documents and Settings\All Users\Documents\My Music\Vladimir_Horowitz-Etude_In_C_Minor_Op_25_No_12_Chopin.mp3
    Thanx ron,
    thanx a lot for ur guidance and code. still i am working on the code gn by u and i will get back to u ASAP if it is working fine or if i face some problems.

    u have shown me a way when i was struggling alone. thanx a lot

    With Regards,
    S.Azhagiri

Similar Threads

  1. Printing Text File in VB 6
    By thunder777 in forum VB Classic
    Replies: 7
    Last Post: 10-21-2008, 07:28 AM
  2. reading and writing a file in VB
    By Athono in forum VB Classic
    Replies: 1
    Last Post: 01-02-2006, 10:07 PM
  3. reading and writing a file in VB
    By Athono in forum VB Classic
    Replies: 1
    Last Post: 12-28-2005, 02:46 PM
  4. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM
  5. Automated print to file from VB
    By Pon Thiagarajan TS in forum VB Classic
    Replies: 0
    Last Post: 02-14-2001, 11:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center