Form to Load /Save picture from Web URL

tucker61

Registered User.
Local time
Today, 02:09
Joined
Jan 13, 2008
Messages
344
I have a table in SQL which is full of hyperlinks to pictures.
What i want to do is from within a access form, look up the picture on hyperlink address, then save the picture to a local drive,

The URL is transferred to the MyPic String with no issues, but i am struggling to save this as a Jpg file.

I do not get any errors, it just does not save.

Do i have to do this a different way ?

Code:
Sub GetPicture(CatNo As String)
On Error GoTo Handler
Dim PicPath As String
Dim sLocalFile As String
Dim MyPic As String

            Call Open_Connection1(DB_Name1)
            strsql = "SELECT [URL] FROM [dbo].[Image_URLs] WHERE [CATALOGUE_NUMBER] = '" & CatNo & "'"
            Set rs = New ADODB.Recordset
            rs.Open strsql, cnAWS, adOpenStatic 'runs the defined query
            On Error Resume Next
            MyPic = rs("URL")
            Set rs = Nothing
            Call Close_Connection
            
    sLocalFile = Environ("Temp") & "\Pic.jpg"
    SaveFile MyPic, PicPath
    imgWeb.Picture = PicPath
    DoEvents
Exit Sub
 
Can you post the code for the SaveFile routine?
 
just tried this and it seems to work

Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long


Sub GetPick()
    Dim sin As String
    Dim sout As String

    Dim ret As Long

    sin = "  Your url here  "  
    sout = CurrentProject.Path & "\image.jpg"    'path to saved image location 

    ret = URLDownloadToFile(0, sin, sout, 0, 0)
    If (ret = 0) Then MsgBox "Succedded" Else MsgBox "failed"
End Sub
 
Code:
Sub SaveFile(Source As String, Target As String)
Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CopyFile Source, Target
    Set fs = Nothing
End Sub
 
just tried this and it seems to work

Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long


Sub GetPick()
    Dim sin As String
    Dim sout As String

    Dim ret As Long

    sin = "  Your url here  " 
    sout = CurrentProject.Path & "\image.jpg"    'path to saved image location

    ret = URLDownloadToFile(0, sin, sout, 0, 0)
    If (ret = 0) Then MsgBox "Succedded" Else MsgBox "failed"
End Sub
Thanks, but i keep getting a fail. Ret value = -2146697208
 
Code:
Sub SaveFile(Source As String, Target As String)
Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CopyFile Source, Target
    Set fs = Nothing
End Sub
I could be wrong, but I don't think FSO can copy a file from a URL to a local disk. I think the function @moke123 posted would be more like you want to use instead. If it's not working for you, you might also try using a HTTPRequest class.
 
modified to use arguments

Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long


 Sub GetPick(sin As String, sout As String)

    Dim ret As Long

    ret = URLDownloadToFile(0, sin, sout, 0, 0)
    If (ret = 0) Then MsgBox "Succedded" Else MsgBox "failed"
End Sub

tested with
Code:
Private Sub Command5_Click()
GetPick "https://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/articles/health_tools/surprises_about_dogs_and_cats_slideshow/photolibrary_rm_photo_of_sneezing_woman_and_dog.jpg", CurrentProject.Path & "\image.jpg"

End Sub

I tested it with a few different urls and it worked for me each time.
 
modified to use arguments

Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long


Sub GetPick(sin As String, sout As String)

    Dim ret As Long

    ret = URLDownloadToFile(0, sin, sout, 0, 0)
    If (ret = 0) Then MsgBox "Succedded" Else MsgBox "failed"
End Sub

tested with
Code:
Private Sub Command5_Click()
GetPick "https://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/articles/health_tools/surprises_about_dogs_and_cats_slideshow/photolibrary_rm_photo_of_sneezing_woman_and_dog.jpg", CurrentProject.Path & "\image.jpg"

End Sub

I tested it with a few different urls and it worked for me each time.
 
Thanks, now getting Access Crashing.
Using Access 64 bit in AWS environment.
Might try again tomorrow.
 
sorry, i'm access 2010 32bit.
probably a bitness issue.
good luck.
 
Thanks, now getting Access Crashing.
Using Access 64 bit in AWS environment.
Might try again tomorrow.
Just for fun, try adding PtrSafe. For example.
Code:
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA"
 
Just for fun, try adding PtrSafe. For example.
Code:
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA"
Already done. Had to do that when testing.
 

Users who are viewing this thread

Back
Top Bottom