473,406 Members | 2,847 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,406 software developers and data experts.

Is it possible to assign a command to a variable and then run the variable?

Seth Schrock
2,965 Expert 2GB
I have a form that has two textboxes, txtStartDate & txtEndDate, that are used as criteria for a query. Also on the form are two buttons, btnRefresh & btnPrintReport. Both buttons need do to the same checking to make sure that the textboxes have dates in them. I would like to make that check be done in a function. The problem is that what needs done in the If False portion is different for the two buttons. btnRefresh will requery a subform and btnPrintReport will open a report. Is it possible to do something like the following?

Function
Expand|Select|Wrap|Line Numbers
  1. Function DateCheck()
  2. If IsNull(Me.txtStartDate) Then
  3.     MsgBox ("Please enter a start date")
  4. Else
  5.     If IsNull(Me.txtEndDate) Then
  6.         Me.txtEndDate = Date
  7.         strCommand
  8.     End If
  9. End If
  10. End Function
btnPrintReport On_Click
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnPrintReport_Click()
  2.  
  3. strCommand = DoCmd.OpenReport "rptPurchaseDateReport", acViewPreview
  4. DateCheck
  5.  
  6. End Sub
btnRefresh On_Click
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnRefresh_Click()
  2.  
  3. strCommand = Me.sfrmPurchaseDateReport.Requery
  4. DateCheck
  5.  
  6. End Sub
strCommand is declared as a string at the top of the forms VBA (not in a function or private sub).

So basically, each button assigns its command to the variable and then runs the DateCheck function which uses the command assigned to the variable.

I know that my code doesn't work, but I would like to do something like this so that I can easily change the DateCheck function if I have to and have that change both buttons.
Jul 26 '12 #1

✓ answered by Rabbit

You could do it that way. But you could also have the function return the boolean instead of storing it in a global variable. Then you can just do something like this:

Expand|Select|Wrap|Line Numbers
  1. If DateCheck Then
  2.    'Do something
  3. End If

6 1835
Seth Schrock
2,965 Expert 2GB
I have come up with a work-around, but I don't know if it is the best solution. I added a boolean variable blnDateCheckResult and set it as either true or false depending on the result of the DateCheck function. Then, in the button's On_Click events, I checked the value of blnDateCheckResult to see whether or not to run the button's specific command (requery the subform or open the report). Here is what I have now:


In the general section:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Dim strCommand As String
  5. Dim blnDateCheckResult As Boolean

Function:
Expand|Select|Wrap|Line Numbers
  1. Function DateCheck()
  2. If IsNull(Me.txtStartDate) Then
  3.  
  4.     MsgBox ("Please enter a start date")
  5.  
  6.     blnDateCheckResult = False
  7. Else
  8.  
  9.     If IsNull(Me.txtEndDate) Then
  10.         Me.txtEndDate = Date
  11.     End If
  12.  
  13.     blnDateCheckResult = True
  14.  
  15. End If
  16. End Function
btnRefresh On_Click:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnRefresh_Click()
  2.  
  3. DateCheck
  4.  
  5. If blnDateCheckResult = True Then
  6.     Me.sfrmPurchaseDateReport.Requery
  7. End If
  8.  
  9. End Sub
btnPrintReport On_Click:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnPrintReport_Click()
  2.  
  3. DateCheck
  4.  
  5. If blnDateCheckResult = True Then
  6.     DoCmd.OpenReport "rptPurchaseDateReport", acViewPreview
  7. End If
  8.  
  9.  
  10. End Sub
Does this look like a good solution? It does work, but I am aware that there are certain ways of doing things that do work, but aren't necessarily the best way or could even be a bad way.
Jul 26 '12 #2
Rabbit
12,516 Expert Mod 8TB
You could do it that way. But you could also have the function return the boolean instead of storing it in a global variable. Then you can just do something like this:

Expand|Select|Wrap|Line Numbers
  1. If DateCheck Then
  2.    'Do something
  3. End If
Jul 26 '12 #3
Seth Schrock
2,965 Expert 2GB
With my nested Ifs, what would the boolean result be? And I guess I'm not sure how to "return the boolean".
Jul 26 '12 #4
Rabbit
12,516 Expert Mod 8TB
Basically, in your DateCheck function, where ever you see blnDateCheckResult, replace it with DateCheck.
Jul 26 '12 #5
twinnyfo
3,653 Expert Mod 2GB
Seth,

Your function will assume that the data type it returns in Boolean, but for clarity's sake, you may also want to explicitly declare your function to return a Boolean result:

Expand|Select|Wrap|Line Numbers
  1. Function DateCheck() as Boolean
  2.  
Jul 26 '12 #6
Seth Schrock
2,965 Expert 2GB
Thanks Rabbit. That does make it a little cleaner. I also realized that it probably wouldn't be a good idea to try what I had originally had in mind since it would limit you to only one command per variable. I like this way much better.

Thanks twinnyfo. I had thought about that and checked in one of my books to see how.
Jul 26 '12 #7

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

Similar topics

2
by: cdt_sylvestre | last post by:
hi, i m trying to find how to access a variable of variable (which is an image) i want to know the size of the picture the name of the picture is photo1 next one is photo2, next one is photo3...
3
by: datactrl | last post by:
Hi, all Is that possible I can assign an window object variable to an already opened window? With window.open(), we can get a window object from opened window. If a window has already opened, is...
2
by: bebelino | last post by:
Hello, this should be an easy one, but I've had always troubles with it. How to pass trough a querydef-variable, form-variable and so on from a function to the caller-routine? Is there a simply...
2
by: MattB | last post by:
I've got an form (asp/vb.net) that might need a Checkboxlist or a RadioButtonList, depending on some data in the database. My implementation plan is this: have both controls on the page, but with...
13
by: Shelby | last post by:
Hi, if I have : rs.Fields("firstname").Value = "John" rs.Fields("middlename").Value = "D" rs.Fields("lastname").Value = "Paul" Is it possible to Dim the column name and make it a variable and...
2
by: André | last post by:
I try to initialize some Public Variable in a class use for code-behind The idea is to check all uxMsgXXX variables, declare in top of my class and assign the string value from a DB request. Why...
2
by: Kourosh | last post by:
I'm just wondering, is there a way I could use an environmental variable in an XML file to specify the path of its XSL file? something like <?xml-stylesheet type="text/xsl"...
3
by: Goran | last post by:
Hi @ all! Is it possible to overload a member variable? Example: class ClassA_t {
2
by: mguy27 | last post by:
I am working with VBA in ExcelUltimately I am trying to take a huge amount of data (9500-10000 rows) and do a search for text including the words "Violated Door". I then want to take the rows (which...
1
by: BrianDaze | last post by:
Hi, I am new to Reflection in C#, and hoping someone could give me pointers on how to set a variable using Reflection. The variable name, type, and value are stored in a sql server database...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.