Expression is not a method in Visual Basic.net - vb.net

I have a problem for this code. lstTotal is the error. It says Expressions is not a method for the lstTotal. Im not very knowledgable in coding yet so any help would be appreciated.
Private Sub UpdateTotal()
' Clear the previous subtotal, tax and total
lstTotal.Items.Clear()
' Compute and display the subtotal
lstTotal.Items.Add("SUB TOTAL = ")(subtotal.ToString("C"))
' Compute and Display the tax
Tax = subtotal * TAX_RATE
lstTotal.Items.Add(" TAX=" & Tax.ToString("C"))
' Compute and display the total
lstOrderReceipt.Items.Add("---------------")
End Sub

This doesn't make sense:
lstTotal.Items.Add("SUB TOTAL = ")(subtotal.ToString("C"))
Presumably you mean this:
lstTotal.Items.Add("SUB TOTAL = " & subtotal.ToString("C"))
The error message is saying that you are doing this:
Expression(subtotal.ToString("C"))
which is how you would call a method but what you have in place of Expression, i.e. lstTotal.Items.Add("SUB TOTAL = ") does not evaluate to a method so your code doesn't make sense.

Related

Object doesn't support this property or method, Error 438

I am trying to do some math through VBA, and came across this error which I haven't seen before. Some googling tells me that it has to do with Intelli-Sense options that aren't available for the object being applied to it. But as far as I can tell, it shouldn't be messing up anything here because I didn't use Intelli-Sense in the line the debugger is highlighting. I will post the full code below, any insight is very much appreciated!!
'Declare Variable
Dim L As Double
Dim Length As Double
Dim OrderFTG As Double
Dim UoM As String
Dim W As Double
Dim frm As Access.Form
Set frm = Forms!Frm_JobTicket
'Set L equal to Length from Tbl_JobTicketMould
L = DLookup("Length", "Tbl_JobTicketMould", "Access_ID =" & Forms!Frm_JobTicket!Part_Number)
'Convert Length to Feet
Length = (L \ 12)
'Find Unit of Measure for this part
UoM = DLookup("Unit_of_Measure", "Tbl_JobTicketUoM", "Access_ID =" & Forms!Frm_JobTicket!Part_Number)
'Mupltiply Length times Quantity to get Order Footage
OrderFTG = Int((Length * Me.Txt_Pcs_JobTicket))
'If UoM is PCS then insert that number. Otherwise set equal to Quantity Ordered divided by Length of piece(in FT)
If UoM = "PCS" Then Me.Txt_Pcs_JobTicket = Me.Quantity_Ordered Else: Me.Txt_Pcs_JobTicket = Abs(Int(Me.Quantity_Ordered \ Length))
'Define limits of the loop. Then runs through all Wrap SQ FTG fields and inputs calculation
For W = 1 To 3
'If UoM is PCS then calculate Order Footage to find Wrap Sqaure Footage. Otherwise take slit size in FT and multiply by Order Quantity and Scrap Rate
If UoM = "PCS" Then
frm("Txt_Wrap" & W & "SQFTG_JobTicket") = (((frm("Wrap_Slit" & W) \ 12) * OrderFTG) * (frm(RIP_Scrap_Rate + 1)))
Else: frm("Txt_Wrap" & W & "SQFTG_JobTicket") = (((frm("Wrap_Slit" & W) \ 12) * frm(Quantity_Ordered)) * (frm(RIP_Scrap_Rate + 1)))
End If
Next W
I am also having trouble with this line OrderFTG = Abs(Int((Length * Me.Txt_Pcs_JobTicket))) It keeps coming out to half of the required amount, the data tips for the Abs part of the evaluation will come out to 12,000 and OrderFTG then equals 6,000. I am not sure how that is happening as there is no division whatsoever even happening in that expression.
Thank you!
This simpler example demonstrates the problem I think you're dealing with.
I added a text box, txtScrap_Rate, to my form, opened it in Form View, and entered the number 27 into the text box.
So then I can retrieve that value with the frm(<control name>) approach you're using ... as long as I enclose the control name in quotes.
set frm = Forms!Form18b
? frm("txtScrap_Rate")
27
? frm("txtScrap_Rate") + 1 ' add one to Scrap Rate
28
Without the quotes, Access interprets txtScrap_Rate to be an undeclared variable instead of a control name.
? frm(txtScrap_Rate)
Null
' result is the same as giving it any undeclared variable name, such as this one:
? frm(Bogus)
Null
And, without the quotes, if I try to also do the plus one inside the parentheses ...
? frm(txtScrap_Rate + 1)
... I get the same 438 error as you:
Object doesn't support this property or method
I think you have this issue with RIP_Scrap_Rate in two places. Also I suspect you need to quote the control name in frm(Quantity_Ordered)
You would be wise to include Option Explicit in your module's Declarations section. Access will then alert you about anything it thinks is an undeclared variable but you think should be something else.

How to sum variables in VBA to check input data is correct?

May seem like a stupid question, but how do you sum variables in VBA to check input data is correct?
I'm trying to check the user has inputted data correctly into a UserForm before they continue to the next page. To do this I want to sum some of their input variables. If they don't input it correctly, they get a message box telling them to revise the numbers. My code is:
Dim BinQnt As Double
Dim FillQnt As Double
Dim FineQnt As Double
Dim CoarQnt As Double
Dim RAPQnt As Double
Dim CRQnt As Double
If BinQnt + FillQnt + FineQnt + CoarQnt + RAPQnt + CRQnt = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%."
End If
When I'm testing it, it always goes to the error message box and I'm not sure why. Very sure I am adding variables which sum to 100 (haha). I first tried it without defining the variables, and now I have it still doesn't work.
Am I missing something obvious?
Do you want to round it or not?
In your question, you have the variables as Doubles, and in the answer as integers. Having them as integers will make it easier to hit 100%, but it will round to the nearest integer (2,4 = 2 | 2,6 = 3)
I will assume you are using all texboxes in the form, and as such use this code that is universal:
Dim tot As Double
tot = 0
For Each c In Me.Controls
If TypeName(c) = "TextBox" And IsNumeric(c.Value) Then
tot = tot + c.Value
End If
Next c
If tot = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%. Currently at " & tot & "%."
End If
This would give us something like this:
However, using Integer or Long, gives us this:
Also, the reason as to why I'm using IsNumeric(c.Value), is to stop the code from failing in case of empty boxes (or filled with invalid entries).
You could also place a check here in case no boxes are allowed to be empty.

Adding items in a function to get a sum

I'm trying to build a pizza application and for the sides, based on what's selected they need to be added up and put into a total. I'm using a function to do this, but its only returning the value from the first selection I make in the combo box, and doesn't go through the if to continue to add the remaining values, I'm confused as to why its doing this. My code for the function is below.
Function sides() As Decimal
Dim total As Decimal
If cmboSides.SelectedIndex.Equals(0) Then
total = total + 4.99
ElseIf cmboSides.SelectedIndex.Equals(1) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(2) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(3) Then
total = total + 5.99
ElseIf cmboSides.SelectedIndex.Equals(4) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(5) Then
total = total + 7.99
End If
Return total
End Function
The specific issue here is that you are declaring total inside that method. As a result, it starts at zero every time you call the method so you only get the most recent value. If you expect the running total to be used each time then its value needs to persist between calls to that method, which means that it must be declared outside that method:
Private total As Decimal
Function sides() As Decimal
If cmboSides.SelectedIndex.Equals(0) Then
total += 4.99D
ElseIf cmboSides.SelectedIndex.Equals(1) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(2) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(3) Then
total += 5.99D
ElseIf cmboSides.SelectedIndex.Equals(4) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(5) Then
total += 7.99D
End If
Return total
End Function
There are various other things you could do to improve that code too but this addresses the specific issue that you're asking about.
Store each price as a value then use
Dim total As Decimal
For Each item In lstSides.SelectedItems
total+= CDbl(lstSides.GetItemValue(item))
Next
The most basic solution, assuming you are actually using a ListBox (rather than a ComboBox which can't be set to multiselect), is this:
Function sides() As Decimal
Dim total As Decimal
If lstSides.SelectedIndices.Contains(0) Then total += 4.99
If lstSides.SelectedIndices.Contains(1) Then total += 6.99
Etc
Return total
End Function
Your current code will never move to a second ElseIf after one has been evaluated as true, similar to a Case Select statement.
A much better solution would be to use data binding to a class of Pizza with properties such a Price, Availability, Size, etc.

Calculated textbox does not calculate if Unit Price is formatted as Currency

Simple calculation TotalPrice = QTY * UnitPrice
Does not calculate when I get the Unit Price to display as currency.
The unit price is retrieved from a combo box in the AfterUpdate event as follows:
Private Sub cboItemRequested_AfterUpdate()
' 0 1 2 3 4 5 6
' ITEM Category SIZE UI PRICE NSN_ORDER UNIT_PACK
With Me
.txtDescrOfItemRequested = .cboItemRequested
.txtUI = .cboItemRequested.Column(3)
.txtQTY = 1
.txtUnitPrice = Format(.cboItemRequested.Column(4), "Currency")
.txtPartNumNSN = .cboItemRequested.Column(5)
End With
End Sub
Even though the combo box displays the currency correctly in the 4th column, it does not populate the Unit Price txtbox correctly unless I apply the Format(XXXXX, "Currency"). Incidentally, the textbox is also formatted as currency. However, when I do get the dollar sign to appear, the final calculation TotalPrice remains zero.
I even applied the following to the txtTotalPrice
=Val(Nz([txtQTY],0))*Val(Nz([txtUnitPrice],0))
I tried responding to #krish KM so I scrapped everything above and instead created this function:
Public Function cTotalPrice(vQTY As Variant, vUnitPrice As Variant) As Currency
'only return a value if both fields are numeric
If IsNumeric(vQTY) = True And IsNumeric(vUnitPrice) = True Then
cTotalPrice = vQTY * vUnitPrice
End If
End Function
which I only call like this the AfterUpdate event of the cboItemRequested box and any time txtQTY or txtUnitPrice are updated
.txtTotalPrice = cTotalPrice(.txtQTY, .txtUnitPrice)
But txtUnitPrice simply does not display as currency even with txtUnitPrice formatted as currency.
SOLUTION:
I needed to Format the txtUnitPrice with VBA
.txtUnitPrice = Format(.cboItemRequested.Column(4), "Currency")
Yes, the txtBoxPrice formatted as currency just ignores it for some reason.
But in order for the TotalPrice to come out right, it was necessary to recalulate it at the end. This was the first tip I was given. Thanks krish KM

New to VBA having checkbox problems

So I'm new to programing(2weeks). I am making a data box to find total a price for a project. I works with text boxes, check boxes and a button to find total.
I input the number of lemons and oranges, when I press the button it calculates how much it will cost based on prices that are pulled from the sheet.
Now I put the check box because there is a set quantity of one. I would like to add the price of "sugar" to the total price when it is checked.
Can some one help me get the check box add to total price?
for example the data box looks like this:
Thanks enter image description here
enter image description here
New code almost works the way I was hoping. thank you for the advice, I tried referencing the sheet and it gave me errors. But it works like this for now.
-When I input numbers in the input boxes and press calculate it gives me the correct total.
-When I check the check box it adds to the total, BUT when I press "Calc" the total is calculated without including checked box.
-When I press more then one checkbox for example: Select "Checksugar" then "checkbox2" the total only adds the value of "Checkbox2". then when I press "Calc" again, the total ignores the checkboxes.
What im wanting it do: When I select more then one check box, I want the totals to add rather then replace eachother. I also want the code to give the total value when I press "Calc" rather then only the totals of the input boxes.
What I think I need to do: Some how add the If statements in the "TxtTotal", but I do not know how to do that. Or have multiple If statements.
Can someone help?
Private Sub Calculate_Click()
TxtTotal = TxtLemon * Range("E21").Value + TxtLime * Range("E20").Value _
+ TxtOranges * Range("E19").Value _
+ TxtApple * Range("E25").Value _
+ TxtCactus * Range("E24").Value
End Sub
Private Sub Checksugar_Click()
If Me.Checksugar.Value = True Then
TxtTotal = TxtLemon * Range("E21").Value + TxtLime * Range("E20").Value _
+ TxtOranges * Range("E19").Value _
+ TxtApple * Range("E25").Value _
+ TxtCactus * Range("E24").Value + Range("E22").Value
End If
End Sub
Private Sub Checkbox2_Click()
If Me.Checkbox2.Value = True Then
TxtTotal = TxtLemon * Range("E21").Value + TxtLime * Range("E20").Value _
+ TxtOranges * Range("E19").Value _
+ TxtApple * Range("E25").Value _
+ TxtCactus * Range("E24").Value + Range("E27").Value
End If
End Sub
IF CheckSugarValue.Value = True Then
'do stuff
TxtTotal.Value = TxtTotal.Value + ThisWorkbook.Sheets("your sheet name).Range("E22").Value
End if
You have a realloy bad habit of non reference stuff correctly. Since youre new its best to understand now while you can form good habits. Converting control objects contents to usable values is often done with ".Value" and often times most people will store those values in a variable. Your lack of explicitly referring to a sheet name will get you into some big trouble later b/c ambiguous references will make the compiler look at wrong sheets. I just got into the habit of doing it the long way like in my example.