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!

Displaying an image 1

Status
Not open for further replies.

darinmc

Technical User
Feb 27, 2005
171
GB
Hi
I have this code in the current event of the form..
However, I tried adapting it to a MODULE (Photo) and inside the module,
me. could not be used so i changed it to MyForm..
Below is the code BUT the image is not appearing...

Please can u help!!!

Code:
Option Compare Database

Option Explicit

Function PhotoGraph()
On Error GoTo Err_DisplayImage
    Dim MyForm As Form, C As Control, xName As String
    Dim VarTag As Integer
    Set MyForm = Screen.ActiveForm
Dim stPathA As String
Dim strResult As String
strResult = "Can't find image in the specified folder!"
stPathA = "C:\Lynx\EmpPhotos\" & MyForm.EmpRegNo & ".jpg"
'stPathA = "\\Lynxserver\lynx\EmpPhotos\" & MyForm.EmpRegNo & ".jpg"
'NOT NEEDED - stPathB = "\\Lynxserver\lynx\EmpPhotos\3b.jpg"
'PhotoGraph
If Dir(stPathA) <> "" Then
'Open Image
MyForm.ImageFrame.Visible = False
MyForm.lblImageNote.Visible = True
MyForm.lblImageNote.Caption = strResult
Else
MyForm.lblImageNote.Visible = False
MyForm.ImageFrame.Visible = True
MyForm.ImageFrame.Picture = stPathA
'Open Image
'Default Image
End If

Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
MyForm.ImageFrame.Visible = False
MyForm.lblImageNote.Visible = True
MyForm.lblImageNote.Caption = strResult
'ctlImageControl.Visible = False
'strResult = "Can't find image in the specified name."
'Resume Exit_DisplayImage:
Case Else ' Some other error.
'myform.ImageFrame.Visible = False
'myform.lblImageNote.Visible = True
'myform.lblImageNote.Caption = strResult
''Resume Exit_DisplayImage:
End Select
End Function

Thx

Darin
 
How are ya darinmc . . .

Try:
Code:
[blue]Set MyForm = Forms(Screen.ActiveForm.Name)[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
...Module code...
Code:
Function PhotoGraph()
On Error GoTo Err_DisplayImage
    Dim MyForm As Form, C As Control, xName As String
    Dim VarTag As Integer
    'Set MyForm = Screen.ActiveForm
    Set MyForm = Forms(Screen.ActiveForm.Name)
Dim stPathA As String
Dim strResult As String
strResult = "Can't find image in the specified folder!"
stPathA = "C:\Lynx\EmpPhotos\" & MyForm.EmpRegNo & ".jpg"
'stPathA = "\\Lynxserver\lynx\EmpPhotos\" & MyForm.EmpRegNo & ".jpg"
'NOT NEEDED - stPathB = "\\Lynxserver\lynx\EmpPhotos\3b.jpg"
'PhotoGraph
If Dir(stPathA) <> "" Then
'Open Image
MyForm!tImage = 1
MyForm!ImageFrame.Visible = True
MyForm!ImageFrame.Picture = stPathA
MyForm!lblImageNote.Caption = "Image Found"
'MyForm!lblImageNote.Visible = True
MyForm.Refresh
Else
'MyForm!tImage = 0
'MyForm!lblImageNote.Visible = False
MyForm!ImageFrame.Visible = False
'MyForm!ImageFrame.Picture = stPathA
MyForm!lblImageNote.Caption = strResult
'Open Image
'Default Image
End If
'MyForm!tImage = 1

Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
MyForm.ImageFrame.Visible = False
'MyForm.lblImageNote.Visible = True
MyForm.lblImageNote.Caption = strResult
'ctlImageControl.Visible = False
'strResult = "Can't find image in the specified name."
'Resume Exit_DisplayImage:
Case Else ' Some other error.
            'MsgBox Err.Number & " " & Err.Description
            'strResult = "An error occurred displaying image."
            'Resume Exit_DisplayImage:
'myform.ImageFrame.Visible = False
'myform.lblImageNote.Visible = True
'myform.lblImageNote.Caption = strResult
''Resume Exit_DisplayImage:
End Select
End Function

...Code on the form...
Code:
Private Sub Form_Current()
PhotoGraph
End Sub

I tested by creating another form..
What happens is that the image DOESN'T load, ONLY when i click next record, it then calls the image!!...
I have also tried using it on form load, but that doesnt call the picture either and then of course that wont work when clicking next record???

I need this procedure to work in the current status..

Hope u can help
Thx
Darin
 
darinmc . . .

After a closer inspection of your post origination, the problem is clear!
TheAceMan1 said:
[blue]You don't exit your code before the Error Handler, which always hides ImageFrame! This is evident in the fact that [blue]Exit_DisplayImage[/blue] doesn't exist![/blue]

So . . . cleaning up the code, I come up with:
Code:
[blue]Function PhotoGraph()
On Error GoTo Err_DisplayImage
   Dim frm As Form, [purple][b]Img[/b][/purple] As Control
   Dim stPathA As String, strResult As String
   
   Set frm = Forms(Screen.ActiveForm.Name)
   Set [purple][b]Img[/b][/purple] = frm!ImageFrame
   stPathA = "C:\Lynx\EmpPhotos\" & frm.EmpRegNo & ".jpg"
   strResult = "Can't find image in the specified folder!"
   
   If Dir(stPathA) <> "" Then
      frm!tImage = 1
      [purple][b]Img[/b][/purple].Visible = True
      [purple][b]Img[/b][/purple].Picture = stPathA
      frm!lblImageNote.Caption = "Image Found"
   Else
      [purple][b]Img[/b][/purple].Visible = False
      frm!lblImageNote.Caption = strResult
   End If

[purple][b]Exit_DisplayImage:
   Set [purple][b]Img[/b][/purple] = Nothing
   Set frm = Nothing
   Exit Function[/b][/purple]
   
Err_DisplayImage:
   Select Case Err.Number
      Case 2220
         [purple][b]Img[/b][/purple].Visible = False
         frm.lblImageNote.Caption = strResult
      Case Else
         MsgBox Err.Number & " " & Err.Description
   End Select
   
   [purple][b]Resume Exit_DisplayImage[/b][/purple]

End Function[/blue]
As a side note: Learn to format your code (as I've presented), for easier reading to you!

[blue]Your Thoughts . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thx
Yes, definitely easier to read. I start adjusting it and keep on using the ' to keep code in case i need it BUT tend to forget to clean up once working.

I'm now getting this error
2475 You entered an expression that requires a form to be the active window
I hit enter and the photo is still not there

If i comment out the below code, error doesnt show but the photo doesnt appear till i click to next record at the bottom.
Code:
Case Else
         'MsgBox Err.Number & " " & Err.Description
   End Select
 
Once the form is active, i can click a cmd button and it works...
I even tried an if statement under current, if lbl.caption =... BUT this created the same error 2475.

Is there a way, in the current part of form, to call photograph once its active?

thx
Darin
 
darinmc . . .

What happens if your explicit with the form reference:
Code:
[blue]   Set frm = Forms![[purple][B][I]YourFormName[/I][/B][/purple]][/blue]

Also from where, how, and when are you calling [blue]PhotoGraph[/blue]? . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
I was trying to avoid a specific form name as I want to use it with another form as well, when making a badge..

I'm using it in the current event, it's the first command i'm calling. (At the moment, the only command)

YES, it works with
Set frm = Forms![TESTPhoto]
BUT NOT using the method below..
Set frm = Forms![TESTPhoto] Or Forms![frmEmpReadBadge]


Darin
 
darinmc . . .

It appears the form doesn't become active for active detection until after the current event. So to maintain multiple forms will pass the formname to PhotoGraph. Install the changes in purple you see below:
Code:
[blue]Function PhotoGraph([purple][b]frmName As String[/b][/purple])
On Error GoTo Err_DisplayImage
   Dim frm As Form, Img As Control
   Dim stPathA As String, strResult As String
   
   Set frm = Forms([purple][b]frmName[/b][/purple])
   Set Img = frm!ImageFrame
   stPathA = "C:\Lynx\EmpPhotos\" & frm.EmpRegNo & ".jpg"
   strResult = "Can't find image in the specified folder!"[/blue]
From the calling forms, a call to PhotoGraph will look like:
Code:
[blue]   Call PhotoGraph(Me.Name)[/blue]
Name in Me.Name stays as is.

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thx
That has been very helpful including the cleanup of code.
Much appreciated...
Darin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top