473,387 Members | 1,510 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.

VBA Function only working for some cases

166 100+
Hello:

I have a function that I created that returns the single sample weight of each material that is selected

My function accepts 5 arguments:
1)matType-The material the user has selected
2)batchWeight-The batchweight of each material used
3)SumCem-Sum of the cement material batchweights used
4)CemSingWt- Sum of cement single sample weight
5)pigPercent- the percent of pigments being used

Here is my function:
Public Function GetSingleWeight(matType As Integer, BatchWeight As Double, SumCem As Double, CemSingWt As Double, PigPerc As Double) As Double
'returns the single sample weight of each material in the 'DM_SingleSampWt' calculation

Select Case matType
'Cement
Case 1
GetSingleWeight = BatchWeight / 164.5
Case 2, 3
'Course, Fine
GetSingleWeight = CemSingWt / SumCem * BatchWeight
Case 4
'Pigment
GetSingleWeight = CemSingWt * 453.5924 * PigPerc
End Select
End Function

I have an unbound text box on my continuous subform called DM_SingleSampWt. the control source for this text box is:
Expand|Select|Wrap|Line Numbers
  1. =GetSingleWeight([cbo_matTypeID],[matBatchWeight],[txtCemSum],[txtCemSing],[txtPigPercent])
The problem is that only some of my Cases are being calculated. Cases 1, 2 and 3 do not work. I am receiving a #Error, error. If you notice, all of my cases do not use the same arguments, but I know it's necessary to have them when I call my function.
If you can offer any assistance, I would appreciate it.

Thank You
Dec 3 '08 #1
25 1938
ChipR
1,287 Expert 1GB
Are any of your values zero or null? Specifically, you might have to say
GetSingleWeight = iif(SumCem = 0, 0, CemSingWt / SumCem * BatchWeight)
Dec 3 '08 #2
csolomon
166 100+
No, none of them are zero or null; they will never be 0 or null. In Case 1 in this instance, SumCem is 658 and CemSingWt is 4.
Dec 3 '08 #3
ChipR
1,287 Expert 1GB
Is txtCemSum a text box set to Number format or a string?
Dec 3 '08 #4
csolomon
166 100+
txtCemSum is set to a General Number
Dec 3 '08 #5
ChipR
1,287 Expert 1GB
Also, is it a text box on the detail of the continuous form? Can you set a breakpoint in your function and check the values passed in?
Dec 3 '08 #6
csolomon
166 100+
No, it is a text box in the form footer of the continuous form.

How do I set a break point to check the values passed in?
Dec 3 '08 #7
ChipR
1,287 Expert 1GB
In the editor you should have a margin on the left hand side of your code that you can click in and set a red circle.
Dec 3 '08 #8
csolomon
166 100+
OK...I've set the red circle for both Case 1 and case 2, 3...what am I supposed to be looking out for? something in the immediate window?
Dec 3 '08 #9
ChipR
1,287 Expert 1GB
When the form is opened and the function is called, it should stop there in the code. Hover the mouse over the variable names to see their values, or right-click them and set a watch as you step through the code.
Dec 3 '08 #10
ChipR
1,287 Expert 1GB
You can also put in the immediate window ?variablename and it will print out the value.
Dec 3 '08 #11
csolomon
166 100+
Chip,

when I hover over case 1,
BatchWeight = 28

Which is incorrect. I have two instances on this continuous form where the case is 1 and the BatchWeight for those two instances are 526 and 132.

Also, when I type: ?cbo_matTypeID I get a blank line
Dec 3 '08 #12
csolomon
166 100+
actually when i type ?matType, it =4

There is an instance of a record h aving the matType =4 but my function only works when the matType is 4.
Dec 3 '08 #13
ChipR
1,287 Expert 1GB
Make sure your breakpoints are on the calculation lines rather than the checks for case.

Select Case matType
'Cement
Case 1
* GetSingleWeight = BatchWeight / 164.5
Case 2, 3
'Course, Fine
* GetSingleWeight = CemSingWt / SumCem * BatchWeight
Case 4
'Pigment
* GetSingleWeight = CemSingWt * 453.5924 * PigPerc
End Select
End Function

And open the form and use the green arrow or F5 to continue until it stops on the case 2 calculation. Then check the values on that line.
Dec 3 '08 #14
ChipR
1,287 Expert 1GB
Also, put in a case else getsingleweight=0 or something. You should almost always have a case else.
Dec 3 '08 #15
csolomon
166 100+
Hi Chip,

I tried to hover over the actual calculation, but nothing happened when i hovered over the variable names. My day is over for today, but if I will be here bright and early tomorrow and I would appreciate if you could assist me.

Thank you for all your help today.
Dec 3 '08 #16
ChipR
1,287 Expert 1GB
I suspect that you aren't referencing the field values that you want, but I'm about to leave work also. We'll figure it out tomorrow.
When you get back, can you upload the form?
Dec 3 '08 #17
ChipR
1,287 Expert 1GB
Here's a question: What is the matBatchWeight control, and when is its content and the content of the txtCemSum set?
Dec 4 '08 #18
csolomon
166 100+
Hey Chip,

I have been trying to reduce the size of my DB so that I could attach it, but I have not been successful. Is there an email I can send it to?

the matBatchWeight control is an input value. Each material that is used to create a design mix, uses a certain amount (lbs or ozs) of a material. The content of matBatchWeight is set after the user selects the the material they are using, they then input the amount they are using. txtCemSum is a calculation. it is set after the user selects the cement they are using...they can select up to two. txtCemSum is the total sum of the matBatchWeights used for the two cements selected.
Dec 4 '08 #19
ChipR
1,287 Expert 1GB
As I suspected. The subform is calculated when the form loads, and the values are empty. Place a button on your form somewhere and have it do
subform.form.requery
or
subform.form.recalc
I'm not sure what the difference is and can't look it up at the moment, for after the values are entered.
Dec 4 '08 #20
csolomon
166 100+
The .requery and .recalc are basically the same. I used .recalc

Private Sub Command46_Click()
[SF_MixSample].Form.Recalc
End Sub

But my Cases for 1, 2, and 3 still show as #error
Dec 4 '08 #21
csolomon
166 100+
Also, matBatchWeight is a bound column, so the values have already been entered
Dec 4 '08 #22
ChipR
1,287 Expert 1GB
Sent you a private message with an email address. I just can't figure it out without seeing what's what on the form I guess.
Dec 4 '08 #23
csolomon
166 100+
Chip,

I sent the zip file to your email.
Dec 4 '08 #24
ChipR
1,287 Expert 1GB
Turns out, it won't even call your function if you don't have the proper number of arguments, which happens when [txtPigPercent] is null.
Change that in your function call to Nz([txtPigPercent],0) or some number other than zero that you would want to substitute.
Also the form complained when the function returned null, so return something else in that case.
Dec 4 '08 #25
csolomon
166 100+
Thanks chip, that worked perfectly.
Dec 5 '08 #26

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

Similar topics

4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
3
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can...
3
by: Dave | last post by:
Hello all, When working with the STL, under what circumstances may I use a function object that modifies its internal state from one call to the next? I know this can be done with for_each<>()....
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
5
by: Andy | last post by:
Sigh... working on it for whole day and got no idea... Basically I want to have a bitmask filtering function, I will throw in 3 parameters: bitMaskValue - current bitmask value filterValue -...
6
by: rep_movsd | last post by:
Hi folks I was on topcoder and came across an interesting problem... It involved dynamic programming ( storing function results in a map to avoid repeated computation ) and I ended up having...
25
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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.