473,382 Members | 1,348 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,382 software developers and data experts.

VBA efficiency question

beacon
579 512MB
Hi everybody,

On most of the forms I create that have date textboxes, I usually add a command for today's date and yesterday's date.

In the past when I've written the code for each of the buttons, I've written an individual private function for each button (I may have between 1-10 today and yesterday buttons on a given form).

I'm curious, is this way efficient or would it be better for me to write one public function in a module and call that function in the OnClick event for each command button and just pass the textbox as a parameter? I was thinking that I would still have to create a different set of command buttons for each textbox, so I wasn't sure which way would be the most efficient.

Thanks...
Feb 6 '09 #1
4 1598
DonRayner
489 Expert 256MB
This is what I came up with. You can use one set of buttons to do all the date controls.

Placed in a module.
Expand|Select|Wrap|Line Numbers
  1. Public Function ChangeDate(ButtonVal as Boolean, ControlDate as Date) as Date
  2. If ButtonVal Then
  3.     ChangeDate = DateAdd("d",-1,ControlDate)
  4. Else
  5.     ChangeDate = DateAdd("d",1,ControlDate)
  6. End If
  7. End Function
Placed in the Forms Module
Expand|Select|Wrap|Line Numbers
  1. Private Sub PreviousButton_Click()
  2. Me(ctlName) = ChangeDate(1, Me(ctlName))
  3. Me(ctlName).SetFocus
  4. End Sub
  5.  
  6. Private Sub NextButton_Click()
  7. Me(ctlName) = ChangeDate(0, Me(ctlName))
  8. Me(ctlName).SetFocus
  9. End Sub
  10.  
  11. Private Sub Text0_GotFocus()
  12. ctlName = Me.ActiveControl.Name
  13. Me.PreviousButton.Enabled = True
  14. Me.NextButton.Enabled = True
  15. End Sub
  16.  
  17. Private Sub Text0_LostFocus()
  18. ctlName = ""
  19. Me.PreviousButton.Enabled = False
  20. Me.NextButton.Enabled = False
  21. End Sub
Feb 7 '09 #2
beacon
579 512MB
Hi Don,

I'm guessing from your suggestion that it would be more efficient to use a module and call the function from a private subroutine, as opposed to writing the same code in multiple private subs.

I actually had a function written to do something similar to what you wrote before, although it's not as nice as what you wrote, but was curious if this was the most efficient route to take.

Thanks for your help...
Feb 7 '09 #3
DonRayner
489 Expert 256MB
Your quite welcome beacon.
Feb 7 '09 #4
NeoPa
32,556 Expert Mod 16PB
As far as efficiency goes, calling a public procedure is actually less efficient in terms of code execution (The effect would be entirely negligible though, especially when combined with human interaction).

HOWEVER, it is far, far more efficient as far as development time goes.

I would certainly recommend a public procedure above coding something over and over again.
Feb 8 '09 #5

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

Similar topics

2
by: Sara | last post by:
Hi - I've been reading the posts for a solution to my query, and realize that I should ask an "approch" question as well. We receive our production data from a third party, so my uers import...
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
92
by: Dave Rudolf | last post by:
Hi all, Normally, I would trust that the ANSI libraries are written to be as efficient as possible, but I have an application in which the majority of the run time is calling the acos(...)...
1
by: Tomás | last post by:
dynamic_cast can be used to obtain a pointer or to obtain a reference. If the pointer form fails, then you're left with a null pointer. If the reference form fails, then an exception is thrown....
19
by: Frederick Gotham | last post by:
Commonly, people may ask a question along the lines of, "Which code snippet is more efficient?". If the code is anything other than assembler (e.g. C or C++), then there's no precise answer...
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
9
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.