473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Changing the appearance of a command button

204 128KB
I want to temporarily change the appearance (caption, text colour) of a command button cmdExitButton when another command button is clicked. I have the following code in the On Click event of the second button:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.Hourglass (True)
  2. Me.cmdExitButton.Caption = "Please wait ..."
  3. Me.cmdExitButton.ForeColor = RGB(255, 0, 0)
  4. Me.Refresh
  5. WantedDB = IIf(Currently = "test", "live", "test")    ' "Currently" was set to the current BE by function WhichBackEnd
  6. For Each tbl In CurrentDB.TableDefs
  7.     If InStr(tbl.Connect, Currently) > 0 Then         ' i.e. if it is a linked table
  8.           tbl.Connect = Replace(tbl.Connect, Currently, WantedDB)  ' Update the connect string test-->live or live-->test
  9.           tbl.RefreshLink                             ' Relink the table
  10.     End If
  11. Next
  12. LogActivity ("switched to")
  13. Me.Refresh
  14.  
  15. ExitSub:
  16.     DoCmd.Hourglass (False)
  17.     Me.cmdExitButton.Caption = "Exit Database"
  18.     Me.cmdExitButton.ForeColor = RGB(255, 255, 255)
  19.     Exit Sub
This works when I step through the code, but when I run it normally the hourglass appears but the button doesn't change appearance.
(The Me.Refresh in line 4 doesn't appear to be necessary - it doesn't make any difference).

Does anyone have any suggestion as to why?
Peter
Jun 21 '17 #1

✓ answered by PhilOfWalton

Hi Petrol

Had be foxed, so I simulated your code with this

Expand|Select|Wrap|Line Numbers
  1.     Dim Lngi As Long
  2.  
  3.     DoCmd.Hourglass (True)
  4.     CmdExitButton.ForeColor = vbRed
  5.     CmdExitButton.Caption = "Please Wait..."
  6.  
  7.     DoEvents
  8.     For Lngi = 1 To 500000000                  ' Simulates a time delay
  9.     Next Lngi
  10.  
  11.     DoCmd.Hourglass (False)
  12.     CmdExitButton.ForeColor = vbWhite
  13.     CmdExitButton.Caption = "Exit Database"
  14.  
Until I put the DoEvents in line 7, I had the same problem as you, but that seems to fix it.

Phil

7 2911
PhilOfWalton
1,430 Expert 1GB
Hi Petrol

Had be foxed, so I simulated your code with this

Expand|Select|Wrap|Line Numbers
  1.     Dim Lngi As Long
  2.  
  3.     DoCmd.Hourglass (True)
  4.     CmdExitButton.ForeColor = vbRed
  5.     CmdExitButton.Caption = "Please Wait..."
  6.  
  7.     DoEvents
  8.     For Lngi = 1 To 500000000                  ' Simulates a time delay
  9.     Next Lngi
  10.  
  11.     DoCmd.Hourglass (False)
  12.     CmdExitButton.ForeColor = vbWhite
  13.     CmdExitButton.Caption = "Exit Database"
  14.  
Until I put the DoEvents in line 7, I had the same problem as you, but that seems to fix it.

Phil
Jun 21 '17 #2
Petrol
204 128KB
How clever! I had never heard of DoEvents. Thank you.

(You were just sitting there, saying "I wonder when Petrol will run into his next problem", weren't you? :)
Jun 21 '17 #3
PhilOfWalton
1,430 Expert 1GB
Ah Petrol

You can't beat regular customers (even the non paying ones!!!)

Regards

Phil
Jun 21 '17 #4
NeoPa
32,556 Expert Mod 16PB
Hi Petrol.

You'll notice, in Access particularly, that when code runs it tends to be quite dogged about keeping control of the processor. IE. When it's running then other stuff like screen displays can be left waiting. This is largely due to the nature of database work. For instance it's important that things don't change behind the scenes when half way through a block update query.

Thus VBA gives the opportunity for your code to be a little better behaved by making DoEvents available to programmers. This tells the managing code that your code can afford to allow other processes to run in the background before it takes its next step. It's always a good idea to bear in mind the behaviour of your projects. They should be good citizens where possible (IE Use DoEvents where it makes sense to).

In the other direction, it's also possible to delay updates on Forms, Reports etc using :
Expand|Select|Wrap|Line Numbers
  1. Me.Painting = False
  2. ...
  3. Me.Painting = True
Jun 21 '17 #5
Petrol
204 128KB
Hmm, thanks, NeoPa. I wish I had known this a year ago when I first started blundering about in VBA coding!

Most of my many procedures are quite short (and it would be along slow process to even find them all now!)... but are there any rules of thumb to indicate where/when it's a good idea to throw a few DoEvents in?
Jun 21 '17 #6
NeoPa
32,556 Expert Mod 16PB
Yes. I would say between running of action queries in a sequence. Also in any code that takes a long time to execute. Generally these fall into some sort of loop so add it within the loop.
Jun 22 '17 #7
Petrol
204 128KB
Thanks, NeoPa :-)
Peter
Jun 22 '17 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rick | last post by:
Hello, I currently use a field that I have set up to keep notes in. It is a memo data type field. It looks like a simple box, and I typically time and date stamp my notes as I enter them. For...
2
by: Chris Calzaretta | last post by:
How would I got about changing my rectangle command button to a circle command button? Thanks Chris
21
by: MLH | last post by:
If I choose a command button on a form in design view, open the properties box and click the Picture property and its wiz button, I'm presented with a lengthy list of pictures. Everything from Add...
7
by: support | last post by:
Hi, I am trying to change the text of a Command button using the Windows API Function SetWindowText, which I have declared as follows: <DllImport("User32")_ Public Function...
2
by: brnkstyle | last post by:
I want to change the controlsource of a textbox when i click a command button but everytime i do so it gives me #Name$ in the text boxt this is what i want to control source to be ...
1
by: sarangjavkhedkar | last post by:
changing the color of command button caption
5
BigToe
by: BigToe | last post by:
Hi folks, can anyone help me with an easy one: - access 2000 I have a combo box with client names. the client names change based on two other combo boxes which do requeries. Unfortunately, I...
5
by: WestAussie | last post by:
Hello I'm new to .NET and have been working through some exercises to get a bit more fluent. I have no idea what I have done to cause command buttons to have their background appearance...
5
by: Susan Bricker | last post by:
I have a command button that I'd like to make "disappear" after being clicked and after the OnClick function has executed. I have tried changing the 'visible' attribute to false while within the...
9
by: Greg Sovan | last post by:
Excel 2000 my earlier post in which i said my vba code would not execute after changing to a different sheet was actually launched with a command button. it would not do anything, not even move...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.