473,769 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling Function From query error 3061

Cintury
81 New Member
The problem is I have a function that I've created and stored in a module. I call it as an expression (e.g. total: Function(parame ter)). I'm receiving the error 3061: too few parameters, expected 1. Now the query and database are already open so I'm not entirely sure of any connection strings I may need. I think part of the problem may be that the parameter I am using to call the function is part of the query. I am not sure how to come by this value before hand. I may need to split this into a two-stage query.
Advice greatly appreciated!

MS-Access SQL Query
Expand|Select|Wrap|Line Numbers
  1. SELECT tblInventory.InventoryID, tblInventory.Food_Donations_ID, tblFoodDonor.[Donor Company], tblProductBrand.Brand_Name, tblFood_Category.Food_Category_ID, tblFood_Category.Food_Cat_Description, tblInventory!Num_Of_Containers*tblInventory!Weight_Per_Container AS Total_Poundage_From_Inv, totFdPoundage([tblInventory]![Food_Category_ID]) AS Total_Poundage_From_FD
  2. FROM tblProductBrand INNER JOIN ((tblFood_Category INNER JOIN tblInventory ON tblFood_Category.Food_Category_ID = tblInventory.Food_Cat_ID) INNER JOIN (tblFoodDonor INNER JOIN tblFoodDonations ON tblFoodDonor.DonorID = tblFoodDonations.DonorID) ON tblInventory.InventoryID = tblFoodDonations.InventoryID) ON tblProductBrand.Brand_ID = tblInventory.Product_Brand_ID
  3. WHERE (((tblInventory.Distribution_completed)=False));
  4.  
My Function
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3.  
  4. Public Function totFdPoundage(fcat As Integer) As Integer
  5.  
  6. Dim strsql As String
  7. Dim rsfh As Recordset
  8. Dim lbsContainer As Integer
  9. On Error GoTo verbose_warning
  10.  
  11.  
  12.  
  13.  
  14.  
  15. Select Case fcat
  16. Case 0:
  17.  
  18.    strsql = "SELECT Bakery FROM tblFoodDonations WHERE " & _
  19.             "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  20.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  21.     lbsContainer = rsfh("Bakery")
  22.  
  23.  
  24.     totFdPoundage = lbsContainer
  25.  
  26. Case 1:
  27.  
  28.     strsql = "SELECT Dairy FROM tblFoodDonations WHERE " & _
  29.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  30.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  31.     lbsContainer = rsfh("Diary")
  32.  
  33.  
  34.     totFdPoundage = lbsContainer
  35.  
  36.  
  37. Case 2:
  38.  
  39.     strsql = "SELECT [tblFoodDonations].[Meat] FROM [tblFoodDonations] WHERE " & _
  40.              "[tblFoodDonations].[FoodDonationsID] = [qryCurr_Inv].[Food_Donations_ID]"
  41.  
  42.  
  43.     Debug.Print strsql
  44.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  45.     lbsContainer = rsfh("Meat")
  46.  
  47.  
  48.     totFdPoundage = lbsContainer
  49.  
  50.  
  51. Case 3:
  52.  
  53.     strsql = "SELECT Fruit FROM tblFoodDonations WHERE " & _
  54.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  55.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  56.     lbsContainer = rsfh("Fruit")
  57.  
  58.  
  59.     totFdPoundage = lbsContainer
  60.  
  61.  
  62. Case 4:
  63.  
  64.     strsql = "SELECT Vegetable FROM tblFoodDonations WHERE " & _
  65.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  66.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  67.     lbsContainer = rsfh("Vegetable")
  68.  
  69.  
  70.     totFdPoundage = lbsContainer
  71.  
  72.  
  73. Case 5:
  74.  
  75.     strsql = "SELECT Prepared FROM tblFoodDonations WHERE " & _
  76.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  77.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  78.     lbsContainer = rsfh("Prepared")
  79.  
  80.  
  81.     totFdPoundage = lbsContainer
  82.  
  83.  
  84. Case 6:
  85.  
  86.     strsql = "SELECT Non-Perishable FROM tblFoodDonations WHERE " & _
  87.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  88.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  89.     lbsContainer = rsfh("Juice")
  90.  
  91.  
  92.     totFdPoundage = lbsContainer
  93.  
  94.  
  95. Case 7:
  96.  
  97.     strsql = "SELECT Non-Food FROM tblFoodDonations WHERE " & _
  98.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  99.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  100.     lbsContainer = rsfh("Non-Perishable")
  101.  
  102.  
  103.     totFdPoundage = lbsContainer
  104.  
  105.  
  106. Case 8:
  107.  
  108.     strsql = "SELECT  FROM tblFoodDonations WHERE " & _
  109.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  110.     Set rsfh = CurrentDb.OpenRecordset(strsql)
  111.     lbsContainer = rsfh("Non-Food")
  112.  
  113.  
  114.     totFdPoundage = lbsContainer
  115.  
  116.  
  117. End Select
  118.  
  119. verbose_warning:
  120.    msgbox "Error Number: " & Err.Number & vbCrLf & "Problem Description: " & Err.Description & " " & vbCrLf & "Help Context: " & Err.HelpContext
  121.    Exit Function
  122.  
  123.  
  124. rsfh.Close
  125. Set rsfh = Nothing
  126.  
  127.  
  128. End Function
  129.  
Oct 5 '09 #1
8 2992
NeoPa
32,573 Recognized Expert Moderator MVP
No definitive answers, but I'd consider running the query without the function call to start with and see what shows. The parameter is defined as an Integer, so there must be no missing records, and the field passed must also be Integer (It is most usual for ID type fields to be defined as Longs).

See where this gets you. You may need to progress from there to debugging your actual code. Do it first without the query. See Debugging in VBA for help with debugging.
Oct 5 '09 #2
ChipR
1,287 Recognized Expert Top Contributor
What does this do?
lbsContainer = rsfh("Bakery")
Oct 5 '09 #3
ChipR
1,287 Recognized Expert Top Contributor
Case 8 is missing a parameter.
strsql = "SELECT FROM ...
Oct 5 '09 #4
Cintury
81 New Member
@ChipR
According to my reading on recordsets, it should take the value of that field and place it in the lbsContainer variable.
Oct 5 '09 #5
ChipR
1,287 Recognized Expert Top Contributor
Sorry, but this is bothering me. The purpose of Select Case is to make your life easier.
Expand|Select|Wrap|Line Numbers
  1. strSQL = "Select "
  2. Select Case fcat 
  3.   Case 0
  4.     strSQL = strSQL & "Bakery "
  5.   Case 1
  6.     strSQL = strSQL & "Dairy "
  7.   Case 2
  8.     strSQL = strSQL & "Fruit "
  9.   ...
  10.   Case Else
  11.     MsgBox "Invalid Category!"
  12. End Select
  13. strSQL = strSQL & "FROM tblFoodDonations WHERE " & _ 
  14.              "tblFoodDonations.FoodDonationsID = qryCurr_Inv.Food_Donations_ID"
  15. Set rsfh = CurrentDb.OpenRecordset(strSQL) 
  16. ...
That being said, this seems like a job for DLookup.
Oct 5 '09 #6
Cintury
81 New Member
@ChipR
Ok, I've written up a DLookup function and it seems to be correctly formatted yet it gives me a data type mismatch for the criteria section. Can you let me know if I am giving the correct parameters?
Expand|Select|Wrap|Line Numbers
  1. Public Function totLBS(fdreceipt As String, fcat As String) As Long
  2.  
  3.  
  4. Dim numLbsContainer As Long
  5. Dim strCriteria As String
  6.  
  7. strCriteria = "FoodDonationsID =" & fdreceipt
  8. numLbsContainer = DLookup(fcat, "tblFoodDonations", strCriteria)
  9.  
  10. totLBS = numLbsContainer
  11. End Function
When Debugging the fcat is "Meat" which is the correct string for the category I want to look up in the tblFoodDonation s category.

The strCriteria in my test case comes out to "FoodDonationsI D=80012" which appears to be the correct format when going by the link you gave me.

I think the problem is alllllmost solved.
Thanks for all the help!
Oct 6 '09 #7
Cintury
81 New Member
Nevermind, I figured out where my formatting was wrong. I'll repost the code below in case anyone else has DLookup trouble.

Expand|Select|Wrap|Line Numbers
  1. Public Function totLBS(fdreceipt As String, fcat As String) As Long
  2.  
  3.  
  4. Dim numLbsContainer As Long
  5. Dim strCriteria As String
  6.  
  7. strCriteria = "[FoodDonationsID] =" & "'" & fdreceipt & "'"
  8. numLbsContainer = DLookup("[" & fcat & "]", "tblFoodDonations", strCriteria)
  9.  
  10. totLBS = numLbsContainer
  11. End Function
  12.  
  13.  
I appreciate all the help I received from this forum, special thanks to ChipR & NeoPa.
Oct 6 '09 #8
NeoPa
32,573 Recognized Expert Moderator MVP
@Cintury
Is the field you want returned called [fcat] or [Meat]. If the former, then you would need the following instead :
Expand|Select|Wrap|Line Numbers
  1. numLbsContainer = DLookup("[fcat]", "tblFoodDonations", strCriteria)
Oct 6 '09 #9

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

Similar topics

12
7531
by: alessia | last post by:
hi, I have a query that visualizes me, according to some criterions from me inserted, name , Surname , Address , phone ok.. Now, through a button, positioned in a Form, I have to only export from this query the field telephone in a text file the name of the text file I would like to choose me...
15
7191
by: deko | last post by:
I need a way to create a table with a programmatically defined name. I have a Make Table query that will create the table with the name that I put in the query, but I don't know how to (or if I can) use a function to pass the table name into the query. Here is the basic form of the Mke Tble query: SELECT TxDate, Amount, Tx_ID, TxAcctName, INTO FROM tblTxJournal;
0
1675
by: Richard Hollenbeck | last post by:
I have a crosstab query that shows all the scores of all the activities of all the students in all courses, with the students being in the rows and the activities being in the columns and the scores being in the intersections of the rows and columns. I also have a report based on this query that, depending on the activity names, will dynamically insert the field names into the column labels, so that I don't have to make a new report...
13
6589
by: MLH | last post by:
Suppose I have this simple SQL string... SELECT tblDrivers.DriverID, tblDrivers.DName FROM tblDrivers WHERE (((tblDrivers.DName) Like "N*")) ORDER BY tblDrivers.DriverID; And suppose that its not a saved querydef - just an SQL string that I cooked up in code and assigned to a global var strMySQL.
9
3062
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped. Below you will find the code I've written and the error that results. I'm hoping that someone can give me some direction as to what syntax or parameter is missing from the code that is expected by VBA. Overview: I'm trying to copy calculated...
3
2063
by: Richard Hollenbeck | last post by:
I am very sorry about the (almost) re-post, but you will see that my first question wasn't very clear; I have another question I posted this morning called, "in DAO: Run time error 3061 Too few parameters...." I have read many articles on the web about how to make a dynamic report based on a cross-tab query. But for some reason mine never works right. First, my saved query's criteria is the data in an open form's combo box. So the...
9
2780
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes from searching on the internet and books but the problem is when i run the command button "Export" it just only open the Blank Spreadsheet, no data at all that it came from my query named "HVACWindwardQuery" and there's an error on it...
4
8981
kcdoell
by: kcdoell | last post by:
Hello: What is the best way to stop a report from running if the query is empty? Currently, I have a form that has a command button on it. The user has to make selections from 3 combo boxes on the form and then via the cmdbutton the report opens in preview mode. I want to stop it from executing if the query is empty. I thought I could do this by checking the record count by the following code but I error out with the following message: ...
4
3588
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category order, Input field and a submit button. The Category name is being fetched from the oracle db along with the corresponding Category order. In the corresponding input field (text box) the user enters a new category order which gets stored in the...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.