Solved Create and delete form on the fly (1 Viewer)

ysdai

Member
Local time
Today, 00:25
Joined
Nov 28, 2019
Messages
33
I have a simple form that, when the user clicks on a hyperlink, the image file path is passed to the form's OpenArgs and the form displays the image in full size.
The code goes like this:
Code:
Option Compare Database
Option Explicit

Dim clsClose As clsCloseButton

Private Sub Form_Load()
    Set clsClose = New clsCloseButton
    clsClose.Init_Button Me.btnClose
    Dim oImg As Object
    Set oImg = CreateObject("WIA.ImageFile")
    oImg.LoadFile Me.OpenArgs
    With Me
        'Resize the window using the Move command, set the width to that of the image, 1 pixel = 15 twips
        .Move .WindowLeft, , Width:=oImg.Width * 15
        'Adjust the height by setting the Detail section of the form, then setting the InsideHeight to match
        'Add a little strip at the bottom for the Close button
        .Detail.Height = oImg.Height * 15 + .btnClose.Height
        .InsideHeight = .Detail.Height
        .Picture = Nz(Me.OpenArgs, "")
        .PictureAlignment = 0   'Top left
        .PictureSizeMode = 0    'Clip mode
        .btnClose.Top = .Detail.Height
        .btnClose.Left = (oImg.Width * 15 - .btnClose.Width) / 2    'Center the button
    End With
    Set oImg = Nothing
End Sub
(I think there's a potential problem to this code, like it might choke if the image to display is larger than the screen, but I'll deal with that separately)
I was just thinking that, if all the properties of this form are defined by code, and the only button in there is a CloseButton class module that closes its parent form, then maybe I can just create this form on the fly, and kill it when the user closes it.

I'm sure a form can be created programmatically, but delete it after use?

I don't know if this is a good idea, just wondering how to do it.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:25
Joined
May 7, 2009
Messages
14,983
I'm sure a form can be created programmatically, but delete it after use?
in my opinion it is not good to delete/re-create the form.
why too many code. you are adjusting the form's size to the image size?
you should do it the other way around, image should resize with the form and yet display the image in full.
experiment with the Image property like: Size Mode, Picture Alignment, Horizontal Anchor and Vertical Anchor.

you can do away with WIA object, what you need is an Image Control.
 

ysdai

Member
Local time
Today, 00:25
Joined
Nov 28, 2019
Messages
33
Well, the original version of this form has an image control, used to display 640x480 images at full size. Right now this is the standard image size for this database, but in the future we may store other types of images with greater detail, so I'm just trying to come up with a new viewer that can display images at 100% size (hence SizeMode=clip), and if the image exceeds the screen, just display it at screen size. This code is not finished yet.

And during the modification process, I found out you can show the image on the form itself, so I ditched the Image Control. Was that a bad move?

WIA was just used to retrieve the image size so I can adjust the form accordingly.
 

sonic8

AWF VIP
Local time
Today, 09:25
Joined
Oct 27, 2015
Messages
474
I'm sure a form can be created programmatically, but delete it after use?
It can be done using the Application.CreateForm method to create and then DoCmd.DeleteObject to delete the form afterwards.
However, it only works in an AccDB file but not in an AccDE and will cause your application to loose its compilation state.

I don't think this is a good idea for your use case. You should rather create the form with all required controls and logic in advance and then just adjust it to the picture size at runtime.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:25
Joined
May 7, 2009
Messages
14,983
(hence SizeMode=clip)
size mode should be Zoom or Stretch using Image Control and Requires no code at all (except setting the Picture property to the image path).
 

ysdai

Member
Local time
Today, 00:25
Joined
Nov 28, 2019
Messages
33
The thing is, I wanted to display the images in their original sizes and resolutions. I have tried zooming/stretching in the previous version of this form with an image control. Not very satisfactory because although stored images are already downsized, they still come in different aspect ratios. Stretching often distorted the pictures and zooming pixelated them if blown up to a larger size than the original. You never know what aspect ratio your next image will be, so setting a fixed-size frame to contain them and stretch/zoom/shrink when necessary doesn't really work. That's why I opted for this dynamic sizing form and the undistorted images indeed look better in them. There are just some issues I need to work out.

But then, this thread is about dynamically creating/deleting forms. I will post the image issues in another thread if I get stuck.
 

ysdai

Member
Local time
Today, 00:25
Joined
Nov 28, 2019
Messages
33
It can be done using the Application.CreateForm method to create and then DoCmd.DeleteObject to delete the form afterwards.
However, it only works in an AccDB file but not in an AccDE and will cause your application to loose its compilation state.

I don't think this is a good idea for your use case. You should rather create the form with all required controls and logic in advance and then just adjust it to the picture size at runtime.
Yeah, I guess you're right. I just wanted to know if this can be done and how to do it. Now I know. Thanks a lot.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:25
Joined
May 7, 2009
Messages
14,983
like you said, there may be 4k image (beyond the screen can accommodate).
have you used photoshop or any photo editing software?
it does not changed its window size based on the image being opened.
if you are using this as slide show on different image size, can you imagine the form is rezing from very small
to very big.
next day, goto ophta?
 

ysdai

Member
Local time
Today, 00:25
Joined
Nov 28, 2019
Messages
33
like you said, there may be 4k image (beyond the screen can accommodate).
I'm actually looking for a way to limit the picture size to screen size. That means, show original size if it fits the screen, otherwise shrink the long side to screen size, but maintain the aspect ratio. Shrinking to screen size doesn't really reduce the picture quality, but zooming definitely does.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:25
Joined
Feb 19, 2002
Messages
34,345
Access is designed to work with numbers and letters, not images. Access's support for images/graphics is poor at best.

Also, saving blobs in your tables will bloat the database in short order. It is far more efficient to save the path to the image and then use FollowHyperlink to open the image in the default Windows image viewer. That will also give you less trouble with resolution.
 

ysdai

Member
Local time
Today, 00:25
Joined
Nov 28, 2019
Messages
33
Also, saving blobs in your tables will bloat the database in short order. It is far more efficient to save the path to the image and then use FollowHyperlink to open the image in the default Windows image viewer. That will also give you less trouble with resolution.
I don't save blobs in the database, just the path. But you're right, I should use FollowHyperlink instead. I always thought it was used to open websites only. If this will open the image in the default viewer, that will be the best option. Thanks for the tip.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:25
Joined
Feb 19, 2002
Messages
34,345
You're welcome. FollowHyperlink works for any file type defined to Windows. As long as Windows knows what program to open to open the file, FollowHyperlink works:) It even opens outlook if the string is an email address.
 

Users who are viewing this thread

Top Bottom