Change the background picture in every form using vba code (1 Viewer)

wire_jp

Registered User.
Local time
Today, 05:24
Joined
Jun 23, 2014
Messages
64
Hi Everyone,

I am using Microsoft Access 2010 software. I would like to change the background picture in every form of my database using vba code ((programmatically).

I found this vba code example http://access.mvps.org/access/api/api0043.htm and the code has be adapted slightly as follows: -
Code:
Private Const sImageFile As String = "yourImage.bmp"     ' image to use for form backgrounds
Private Const sIconFile As String = "yourIcon.ico"
' This code was originally written by Klaus Probst.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Klaus Probst
'
'// Place all this in a module
Public Declare Function LoadImage Lib "user32" _
   Alias "LoadImageA" _
   (ByVal hInst As Long, _
   ByVal lpsz As String, _
   ByVal un1 As Long, _
   ByVal n1 As Long, _
   ByVal n2 As Long, _
   ByVal un2 As Long) _
   As Long
   
Public Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
   (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   LParam As Any) _
   As Long
   
   
Public Const WM_GETICON = &H7F
Public Const WM_SETICON = &H80
Public Const ICON_SMALL = 0
Public Const ICON_BIG = 1
'// LoadImage() image types
Public Const IMAGE_BITMAP = 0
Public Const IMAGE_ICON = 1
Public Const IMAGE_CURSOR = 2
Public Const IMAGE_ENHMETAFILE = 3
'// LoadImage() flags
Public Const LR_DEFAULTCOLOR = &H0
Public Const LR_MONOCHROME = &H1
Public Const LR_COLOR = &H2
Public Const LR_COPYRETURNORG = &H4
Public Const LR_COPYDELETEORG = &H8
Public Const LR_LOADFROMFILE = &H10
Public Const LR_LOADTRANSPARENT = &H20
Public Const LR_DEFAULTSIZE = &H40
Public Const LR_LOADMAP3DCOLORS = &H1000
Public Const LR_CREATEDIBHeader = &H2000
Public Const LR_COPYFROMRESOURCE = &H4000
Public Const LR_SHARED = &H8000
Public Function SetFormIcon(hwnd As Long, IconPath As String) As Boolean
    Dim hIcon As Long
    hIcon = LoadImage(0&, IconPath, IMAGE_ICON, 16, 16, LR_LOADFROMFILE)
    '// wParam = 0; Setting small icon. wParam = 1; setting large icon
    If hIcon <> 0 Then
        Call SendMessage(hwnd, WM_SETICON, 0, ByVal hIcon)
        SetFormIcon = True
    End If
    
End Function
Public Sub LoadBackground(frm As Access.Form)
    If Len(Dir(CurrentProject.path & "\" & sImageFile)) > 0 Then
        frm.PictureTiling = True
        frm.Picture = CurrentProject.path & "\" & sImageFile
    End If
    
    SetFormIcon frm.hwnd, CurrentProject.path & "\" & sIconFile
    
End Sub

In my form to call the sub routine procedure: -
Code:
Private Sub Form_Load()
    ' loads background image and form icon from external file in app folder
    LoadBackground Me
End Sub

But my question is: if I want to add a command button, which an user will click and a file dialog box appears, which allows the user to browse to the directory path to select picture image and insert it into the form background, what is the vba code to add to the On Click event of the form?

Thank you in advance
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 08:24
Joined
Oct 17, 2012
Messages
3,276
Be nice. :p

Wire, you can find code for a file dialog here:

https://msdn.microsoft.com/en-us/vb...g-property-access?f=255&MSPPError=-2147217396

In the VBA editor, make sure to go to Tools -> References and turn on the reference to whichever version of the Microsoft Office library you have in your reference list.

Another version is here: https://rtmccormick.com/2013/08/22/select-a-file-with-file-dialog-in-ms-access-with-vba/

And another version here: http://www.java2s.com/Code/VBA-Excel-Access-Word/File-Path/FileDialogwithJPGfilefilter.htm
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:24
Joined
May 21, 2018
Messages
8,463
Sorry, not trying to be un-nice. That is the best way to show multiple answers, and teach how to fish.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 08:24
Joined
Oct 17, 2012
Messages
3,276
They come here looking for help. Effectively telling them 'figure it out yourself' defeats the purpose of the site.
 

wire_jp

Registered User.
Local time
Today, 05:24
Joined
Jun 23, 2014
Messages
64
Hi Everyone,

Thank you for the quick responses. Referring to Post 3, I was going to update the References, under the VBA Editor, but when I clicked on Tools > References, a pop-up dialog box appeared called Blank Password with the message enter Password (this has never happened to me before, when I made selections from the References). I do not know what the password is. Just on a side note, my computer crashed two days ago and so I am using an older Windows 7 computer (a different computer) and so I don't know if this change has anything to do this request to enter password. Any help will be greatly appreciated and I did google search beforehand to see if there any examples of this issue.

Thanks.
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 12:24
Joined
Jul 9, 2003
Messages
16,244
Regarding "Be Nice"...

You would think that using Google should be people's first port of call. However there's a big problem with Google, you have to know how to word your question, particularly if you're dealing with something new, you're not necessarily going to know the right search terms. So then you are going to get back lots of unrelated, crappy hits!

It is likely that people have tried Google before they come to the forum. On the other hand, people may not like using Google, or let me put it a different way, people prefer to have interaction was real people.

Anyway I don't think there was any need for me to make any comment on this thread except for the fact that I want to promote one of my videos!

In this video I demonstrate how you can use Google to Search specifically within Access World Forums (AWF) --- and being one of the most knowledgeable forums on MS Access, you are likely to get a much more satisfactory list of search results than Just randomly Googling:-

Searching For Answers

And here is a link to the advanced Google search page which is focused on Access World Forums:-

Search Access World Forums - Only!
 

jdraw

Super Moderator
Staff member
Local time
Today, 08:24
Joined
Jan 23, 2006
Messages
15,361
Tony,

I agree you would think this
It is likely that people have tried Google before they come to the forum.
but I have seen lots of evidence that this is not always the case.

I have also seen experienced users/developers who have replied--jeez I never even thought of using Google.

I hope your links to your Search videos get actioned frequently.

Thanks for posting these links and all of your videos.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:24
Joined
Feb 28, 2001
Messages
26,996
One of the things I found out a long time ago was that there is a sub-language, a patois if you will, that you need to understand in order to ask the right questions. I was brutally reminded of this when researching an issue with Excel a couple of months ago because even though Excel and Access are BOTH parts of Office, the patois differs between the two products.

On the Excel forum I used, I didn't know the right way to ask the questions at first. Oh, sure, I picked it up and found out what I needed to do. The problem WAS solved. But for beginners and for people who know SOMETHING about Access but little or nothing about our forum, there is a subtle language barrier.

Which is why I try to not assume (unless it is flat-out obvious) that the person didn't do a web search first. Sometimes people have no clue at all; other times, their lack of clues is limited to lack of jargon. And let's be honest - we DO use jargon here if you think about it. If you were reading some of our articles about front-end and back-end files but had never been exposed to splitting a database, would YOU instinctively know what we meant? Much less that we then compress those to FE and BE?

Sometimes I get feedback when some of the forum members wonder why I get pedantic sometimes. Keep this conversation in mind.
 

Mark_

Longboard on the internet
Local time
Today, 05:24
Joined
Sep 12, 2017
Messages
2,111
MajP also pointed out something that I've seen often; the order in which terms are used on Google can often result in radically different search results. Searching for "File Dialog Box" will return a different set of results form "Access File Dialog".

Knowing HOW google will handle each and having an idea of how to get it to tell you what you need can be difficult for those not used to plying its waters.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:24
Joined
Feb 19, 2002
Messages
42,970
In my very first computer class over 50 years ago when the knowledge base was significantly smaller and the sources of information were limited to IBM manuals and a few very expensive books, my professor made a point of us learning how to use reference material. That stands even more today. You cannot possibly know everything there is to know about the technology you use so you need to learn how to be creative when searching for answers. The search engines are much better than they used to be and certainly more accessible than a bookcase full of manuals. But you still need to rely on your ability to come up with synonyms to find answers.

I still can't get google to find me the alarm clock I want. The display on my ancient Sony is getting dimmer by the day and the newer Sony models are a piece of you know what. So far I've returned three of them. Who wants an alarm clock that you can't work without the manual? or whose buttons you can read because the labels are black on black?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:24
Joined
May 21, 2018
Messages
8,463
Just so we are all clear here, that was not just a random post. I did a good search and returned quality answers. My intent was to push the user to the plethora of available data to include multiple videos (for some that is the way they process information). Follow the link. I understand not everyone gets the same result. Most importantly the first link I get returned (US Google) is MSDN. Most people on this site needs to know how to get to this and use it. It is well designed and better than any Access helpfile. I have seen the OPs database and it is not trivail. MSDN is far better resource than searching AWF and wading through incomplete and sometimes wrong data. There is so much information on MSDN and it is well organized. You learn that, and you can answer 90% of your questions. I rarely ask Access questions. Not because I am smart, but because I can navigate MSDN. Along with that it also returns some posts where people had problems. You learn more from what does not work than from what does work
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 08:24
Joined
Oct 17, 2012
Messages
3,276
And I wasn't saying pointing him to MSDN was bad - I was saying not only just giving a link and nothing else was rude, but that using LMGTFY was straight-up obnoxious.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:24
Joined
Sep 12, 2006
Messages
15,613
is this sorted?

to change this permanently, you would need ot open each from in design mode, set the background and any other options such as tiling, stretch, location of the image, and then save the design.

Useful tool to have, as the technique makes it easier to standardize your forms. You can set properties of all the objects on the form.

Note that you can't do this at run time with accde/mde, as you need to be in design mode.
 

wire_jp

Registered User.
Local time
Today, 05:24
Joined
Jun 23, 2014
Messages
64
Hi gemma-the-husky,

Yes, everything is sorted now.

Thanks again.
 

Users who are viewing this thread

Top Bottom