472,809 Members | 4,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Bytes Tip of the Week #53 - How to Return Multiple Values From a Function

ADezii
8,834 Expert 8TB
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum.
Original Article by Allen Browne

Traditionally, one has always thought that Functions can only return a single value, and for the most part that was true. Ever since Access 95, we gained the new functionality, through VBA, to have Functions return an entire Structure of values. A User Defined Type can easily be created to handle the Structure returned by the Function. In this specific case, a picture is worth a thousand words, so I'll simply post the modified code, adequately comment it, and leave the rest up to you. Should you have any questions, please feel free to ask them.
  1. Declare a User Defined Type that can handle the Structure returned by the Function. Notice the use of the 'Public' in the type declaration, it gives the User Defined Type/Structure Global Scope, meaning its elements can be accessed from anywhere within the Application. Also, notice the diversified Data Types within the Structure.
    Expand|Select|Wrap|Line Numbers
    1. Public Type MyPC
    2.   PC_Type As String
    3.   MHz As Integer
    4.   Hard_Drive_Capacity As String
    5.   RAM As String
    6.   On_Board_Video As Boolean
    7.   USB_Ports As Byte
    8.   Date_Purchased As Date
    9. End Type
  2. The return value of the Function is set to the Global User Defined Type/Structure. Each line within the Function assigns values to individual elements of the Structure, thus giving it the capability to return multiple values.
    Expand|Select|Wrap|Line Numbers
    1. Public Function fReturnPCInfo() As MyPC     'Function returns Structure
    2. 'This is an example of how to return multiple values from a Function,
    3. 'by setting the Return Value of the Function equal to a User Defined
    4. 'Type, then setting the values of its elements
    5.   fReturnPCInfo.PC_Type = "Pentium 1000"
    6.   fReturnPCInfo.MHz = 750
    7.   fReturnPCInfo.Hard_Drive_Capacity = "80 Gb"
    8.   fReturnPCInfo.RAM = "500 Mb"
    9.   fReturnPCInfo.On_Board_Video = True
    10.   fReturnPCInfo.USB_Ports = 5
    11.   fReturnPCInfo.Date_Purchased = #5/15/2008#
    12. End Function
  3. Now, we can retrieve individual elements of the Structure by calling the single Function but with different qualifiers, as in:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print "********************************************"
    2. Debug.Print "PC Type: " & fReturnPCInfo().PC_Type
    3. Debug.Print "PC MegaHertz: " & fReturnPCInfo().MHz
    4. Debug.Print "Hard Drive Capacity: " & fReturnPCInfo().Hard_Drive_Capacity
    5. Debug.Print "RAM: " & fReturnPCInfo().RAM
    6. Debug.Print "On Board Video: " & IIf(fReturnPCInfo().On_Board_Video, "Yes", "No")
    7. Debug.Print "USB Ports: " & fReturnPCInfo().USB_Ports
    8. Debug.Print "Date of Purchase: " & fReturnPCInfo().Date_Purchased
    9. Debug.Print "********************************************"
  4. OUTPUT
    Expand|Select|Wrap|Line Numbers
    1. ********************************************
    2. PC Type: Pentium 1000
    3. PC MegaHertz: 750
    4. Hard Drive Capacity: 80 Gb
    5. RAM: 500 Mb
    6. On Board Video: Yes
    7. USB Ports: 5
    8. Date of Purchase: 5/15/2008
    9. ********************************************
Jun 9 '08 #1
2 3581
Denburt
1,356 Expert 1GB
Very nice and an interesting post, if I may expand on this a bit instead of locking yourself into particular constants this can be very dynamic, take no 2 line 6 (for 1 of many examples) CPU speed can be requested and returned via a function and make this more flexible and dynamic. Therefore Debug.Print "PC MegaHertz: " & fReturnPCInfo().MHz would return any particular users P.C. information: function added GetCPUSPeed...

Expand|Select|Wrap|Line Numbers
  1. Public Function fReturnPCInfo() As MyPC     'Function returns Structure
  2. 'This is an example of how to return multiple values from a Function,
  3. 'by setting the Return Value of the Function equal to a User Defined
  4. 'Type, then setting the values of its elements
  5.   fReturnPCInfo.PC_Type = "Pentium 1000"
  6. 'For example...
  7.   fReturnPCInfo.MHz = GetCPUSpeed
  8.   fReturnPCInfo.Hard_Drive_Capacity = "80 Gb"
  9.   fReturnPCInfo.RAM = "500 Mb"
  10.  fReturnPCInfo.On_Board_Video = True
  11.   fReturnPCInfo.USB_Ports = 5
  12.   fReturnPCInfo.Date_Purchased = #5/15/2008#
  13. End Function
  14.  
  15.  Public Function GetCPUSpeed()
  16. Dim objWMIService As Object
  17. Dim objItem As Object
  18. Dim colItems As Object
  19. On Error Resume Next
  20. Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
  21. Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor", , 48)
  22. For Each objItem In colItems
  23.       GetCPUSpeed = "MaxClockSpeed: " & objItem.MaxClockSpeed & " CurrentClockSpeed: " & objItem.CurrentClockSpeed
  24. Next
  25. End Function
Jun 12 '08 #2
ADezii
8,834 Expert 8TB
Very nice and an interesting post, if I may expand on this a bit instead of locking yourself into particular constants this can be very dynamic, take no 2 line 6 (for 1 of many examples) CPU speed can be requested and returned via a function and make this more flexible and dynamic. Therefore Debug.Print "PC MegaHertz: " & fReturnPCInfo().MHz would return any particular users P.C. information: function added GetCPUSPeed...

Expand|Select|Wrap|Line Numbers
  1. Public Function fReturnPCInfo() As MyPC     'Function returns Structure
  2. 'This is an example of how to return multiple values from a Function,
  3. 'by setting the Return Value of the Function equal to a User Defined
  4. 'Type, then setting the values of its elements
  5.   fReturnPCInfo.PC_Type = "Pentium 1000"
  6. 'For example...
  7.   fReturnPCInfo.MHz = GetCPUSpeed
  8.   fReturnPCInfo.Hard_Drive_Capacity = "80 Gb"
  9.   fReturnPCInfo.RAM = "500 Mb"
  10.  fReturnPCInfo.On_Board_Video = True
  11.   fReturnPCInfo.USB_Ports = 5
  12.   fReturnPCInfo.Date_Purchased = #5/15/2008#
  13. End Function
  14.  
  15.  Public Function GetCPUSpeed()
  16. Dim objWMIService As Object
  17. Dim objItem As Object
  18. Dim colItems As Object
  19. On Error Resume Next
  20. Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
  21. Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor", , 48)
  22. For Each objItem In colItems
  23.       GetCPUSpeed = "MaxClockSpeed: " & objItem.MaxClockSpeed & " CurrentClockSpeed: " & objItem.CurrentClockSpeed
  24. Next
  25. End Function
Very interesting point, Denburt, and a great indication of just how flexible this logic can be. Thanks.
Jun 13 '08 #3

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
7
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code...
5
by: D. Shane Fowlkes | last post by:
This may be a very basic question but it's something I've never done before. I've looked at a couple of my favorite sites and books and can't find an answer either. I can write a Function to...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
0
by: Lee Harr | last post by:
I wrote a function to return the first date of a given week (and a few related functions) : -- return the first date in the given week CREATE or REPLACE FUNCTION week_start(integer, integer)...
6
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
10
by: Jim | last post by:
I'm sure this has been asked before but I can't find any postings. I have a table that has weekly inspections for multiple buildings. What I need to do is break these down by the week of the...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.