Showing images on main form by using vba codes

shafiq-yasin

New member
Local time
Today, 19:34
Joined
Mar 30, 2018
Messages
6
Kindly help me that how i can use vba code that enables me to show pictures from by table to a form?
Now i am coding like
Picture = dlookup from “table login” and further from “IMAGE “ field
Then
Form main ! “Attachment 01” = picture
It show error 438 Any one help pleas



Sent from my iPhone using Tapatalk
 
See if the attached example db helps
This is based on external image files for which the path is stored in the database.

If you are using attachment fields, it won't be much use to you
As I don't use these, I can't advise further
 

Attachments

If it is an attachment field and in same table as your form just drag the attachment field to your form.
 
If it is an attachment field and in same table as your form just drag the attachment field to your form.



But i have made multi user form and each user have to see his picture as he login into DB.


Sent from my iPhone using Tapatalk
 
Tell me your table name. Field names. Add autonumber field to ur table.
 
this is what we need to do.
copy first this function in a Standard Module in VBA.
Code:
Public Function fncGetImage(tableName As String, _
                            AutoField As String, _
                            AutoValue As Long, _
                            attachFieldName As String) As String
    Dim rsParent As DAO.Recordset2
    Dim rsChild As DAO.Recordset2
    Dim path As String
    Set rsParent = CurrentDb.OpenRecordset( _
                        "select [" & attachFieldName & "] from " & tableName & " where " & _
                                AutoField & "=" & AutoValue, dbOpenSnapshot)
    If Not (rsParent.EOF And rsParent.EOF) Then
        rsParent.MoveFirst
        Set rsChild = rsParent(attachFieldName).Value
        With rsChild
            If Not (.BOF And .EOF) Then
                .MoveFirst
                path = Environ("Temp") & "\" & .Fields("FileName")
                If Dir(path) <> "" Then Kill path
                .Fields("FileData").SaveToFile path
                fncGetImage = path
            End If
            .Close
        End With
        Set rsChild = Nothing
    End If
    rsParent.Close
    Set rsParent = Nothing
End Function

bring tblLogin in Design View and add an AutoNumber Field (recommend naming it ID).
next you need to modify your login form so that you get the ID of the user to verify
his identity:
Code:
dim lngID as long
lngID = Nz(dlookup("id", "tblLogin", "username=""" & me.txtUserName & """ And useid=""" & me.txtUserID & """"), 0)

If lngID <> 0 then
	Me.Image.Picture = fncGetImage("tblLogin","id", lngID, "Image")
Else
  	MsgBox "Invalid username or userid"
End If
 
Some comments on arnelgp's code:

The username or userid field should be unique & used as the primary key
If so, you don't need an additional autonumber field

Is the function actually necessary?

The following definitely works where you store the file path
Wouldn't it or something similar also work for an image saved as an attached field? (untested)

Code:
Dim strImage As String

[COLOR="SeaGreen"]'If userID is a text field[/COLOR]
strImage = Nz(DLookup("Image","tblLogin","userid='" & me.txtUserID & "'"),"")
[COLOR="seagreen"]'or if it's a number field
'strImage = Nz(DLookup("Image","tblLogin","userid=" & me.txtUserID),"")[/COLOR]

If strImage<>"" Then
	Me.Image.Picture = strImage
Else
  	MsgBox "Invalid username or password"
End If

That's it....possibly with some tweaking needed??
As I mentioned in my first reply, I don't use attachment fields so if I've got it all wrong, my apologies

@shafiq-yasin
As you may be aware, attachment fields significantly increase database size & therefore reduce performance.
Using a text field containing the file path is much better and then the above will definitely work (fully tested!)
 
Last edited:

Users who are viewing this thread

Back
Top Bottom