Formatting to Currency VB.Net - vb.net

All I need is the following code to get the variable into currency mode.
lines.Add("Labor Cost Per Run:" & laborCostpRun.ToString & " Per Peice: " & laborCostpPc.ToString & " Per Year: " & laborcostpYr.ToString)
I have tried using {0:C2} everywhere, to no end... I know its something simple that I am missed... Thanks

You need to pass a format string to ToString():
laborCostpRun.ToString("C2")
Alternatively, you could replace the entire concatenation with a string.Format() call with formatted placeholders ({0:C2}).

For me String.Format("{0:C2}", Total) worked.
Total has to be a numeric object (in my case a decimal). Using this formatter on a string may give different results.

Here are two different ways you could do it:
Using the Format Function (link to MSDN Documentation)
Format(laborCostpRun,"Currency")
Using ToString with a Specified Format (link to MSDN Documentation)
laborCostpRun.ToString("c")
Both methods offer too many formatting options to spell them all out here. Check the links for more details.

You could always use the FormatCurrency function:
lines.Add("Labor Cost Per Run:" & FormatCurrency(laborCostpRun, 2) & " Per Peice: " & FormatCurrency(laborCostpPc, 2) & " Per Year: " & FormatCurrency(laborcostpYr, 2))

Related

Access VBA error "Too few parameters. Expected 1"

I'm trying to find out why this is wrong, while in the queries generator it works properly.
SELECT Count(Audi.Id) AS CuentaDeId FROM Audi
WHERE (((Len." & filtro & ") Between #" & Format(Me!fechamin, "mm/dd/yyyy") & "# And #" & Format(Me!fechamax, "mm/dd/yyyy") & "#))
With some dates slightly different It works, but I think that when It does not find any value in this Table, the error appears.
No idea if it's a problem of the Query design, or if there is another way to define it, or not. Any clue, anyone?
Thanks in advance!
Len is a function name in SQL. That is why Access asked for the parameter. Besides, as Parfait said, Len is used in your query as an alias but not referring to any other table/query.

Proper Formatting in a listbox

I'm trying to format my listbox so that it looks neat when its in use.
I want to add dollar signs to my numbers, and left align them. Any help would be appreciated. They currently look really sloppy.
Dim salesTotal As Double
customerListBox.Items.Add("Customer: " & " " & "Total Sale: ")
For Each record As DataRow In Me._442_Project_Part_2DataSet.Customers
salesTotal += Double.Parse(CStr(record.Item("YTDSales")))
customerListBox.Items.Add((CStr(record.Item("CustomerName"))) & " " & (CStr(record.Item("YTDSales"))))
customerListBox.Items.Add("------------------------------------------------")
Next
First of all ListBox arn't meant to be easily customizable.
To answer your question I want to add dollar signs to my numbers, and left align them, you need to use String.PadLeft function.
customerListBox.Items.Add((CStr(record.Item("CustomerName"))) & " : $" & (CStr(record.Item("YTDSales"))).PadLeft(8))
Note : I added 8 spaces in this exemple. It might vary depending on the numbers you have. I also added a colon and dollar sign between CustomerName and Sales.

creating a search tool for Access listbox

I have a listbox with many fields in a form and I'm trying to create a search function that searches for partial strings inside it.
Private Sub SearchBox_Change()
Me.DataView.RowSource = "SELECT RowSrcString FROM MechDataFiltered WHERE " & _
"MechDataFiltered.* LIKE " & Chr(34) & Me.SearchBox.Text & "*" & Chr(34) & " "
End Sub
RowSrcString is a string of field names (from MechDataFiltered) that changes depending on other filters. I think my problem is in the 3rd line; MechDataFiltered.* is for "all" instead of "any". What should I change to make it search in each individual field?
You might take a look at some search criteria samples on Allen Brown's site. It's fairly advanced from a programming standpoint, but he is one of the experts in the industry and I have used tips from his site on numerous occasions.
Allen demonstrates how to use search criteria from multiple input fields to dynamically build a query (filter) expression that returns the results that I think you are looking for.
He has some sample databases you can download to try out the various techniques.
Hope that helps!

Converting characters to ASCII code

Need help with reading special characters within my VB code. ASCII code Char(34) = " works fine but Char(60) = < and Char(62) = > are not being read.
My Code
node.FirstChild.InnerText = Chr(60) & "httpRuntime executionTimeout=" & Chr(34) & "999999" & Chr(34) & " maxRequestLength=" & Chr(34) & "2097151" & Chr(34) & "/" & Chr(62)
Without ASCII Code
'node.FirstChild.InnerText = "<httpRuntime executionTimeout="999999" maxRequestLength="2097151"/>"
Are you trying to modify a Config file? Try:-
node.FirstChild.InnerXml = "<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
Note all that Chr marlarky is unnecessary, were you trying to avoid < and > being encoded as XML entities?
Maybe this doesn't answer your question, but you could use two double quotes to escape the quotes character in VB.NET:
node.FirstChild.InnerText = _
"<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
I'm just guessing: you could use the String.Format method for your purposes:
node.FirstChild.InnerText = _
String.Format( _
"<httpRuntime executionTimeout=""{0}"" maxRequestLength=""{1}"" />", _
timeoutValue.ToString(), reqLenValue.ToString())
You'll need to give more information about how you're "seeing" the results. In my experience, problems with this are as likely to be about viewing strings in the debugger as getting the right strings in the first place.
I don't really see why you need to use Chr(60) etc at all, other than for the quotes. What happens when you just use < and > in your code?
I strongly suggest you dump the string out to the console rather than using the debugger - the debugger tries to show you how you could represent the string in code, rather than showing you the contents verbatim.
Of course, if this is XML then I'd expect serializing the XML out again to end up escaping the < and > - again, more information about what you're trying to do would be helpful. The absolute ideal (IMO) would be a short but complete program demonstrating the problem - a small console app which does one thing, and a description of what you want it to do instead.

dd/mm automatically got changed to mm/dd

I coded something using Date statement in Access VBA. It was working fine until the start of this month, but now I am seeing that the Date has automatically changed the format from dd/mm/yyyy to mm/dd/yyyy. Has anyone else encountered the same problem?
The default Access SQL date format, regardless of locale, is mm/dd/yyyy. If you use an invalid date format, it will 'helpfully' try to convert that to a valid date for you.
So, if you use '30/09/2008', it will recognize you're using dd/mm/yyyy, and convert it appropriately. However, a value like '10/01/2008' is a valid mm/dd/yyyy value to begin with, so it will not be converted, and stored incorrectly in case you actually meant dd/mm/yyyy....
The solution is to always convert your date values to a mm/dd/yyyy string prior to using them in Access SQL statements. You have to be a bit careful here, as using VBA date format masks may not work entirely as you'd expect on non-US locales (e.g. 'helpfully' interpreting "mm/dd/yyyy" as "the localized short date format"), so please test carefully using your particular Access/VBA version.
Access requires a date to be unambiguous. It is generally recommended that you use yyyy/mm/dd, regardless of locale. For example:
strSQL="SELECT SomeDate FROM tblT WHERE SomeDate=#" & Format(DateVar, "yyyy/mm/dd") & "#"
Try this code:
stLinkCriteria = "[ProjectDate] Between #" & Format(CDate(Me![txtDateFrom]), "mm/dd/yyyy") & "# And #" & Format(CDate(Me![txtDateTo]), "mm/dd/yyyy") & "#"
It works for me.
This works:
sentenciaSQL = "UPDATE Numeraciones " & _
"SET Valor = " & Valor & ", " & _
"Fecha = #" & **Format(fecha,"mm/dd/yyyy HH:nn:ss") & "#, " &** _
"Id_Usuario = " & Id_Usuario & _
" WHERE Nombre = '" & Nombre & "'"
I have been very successful using the datevalue() function. When getting dates from unbound controls it seems to be clever enough to interpret the format "dd/mm/yyyy" correctly. Thus, a Jet SQL query like
"Select * from DateTable where StartDate = datevalue(" & me!TxtStartDate & ");"
seems to work every time.
I was experiencing same issue while trying to build a SQL string through VBA.
My locale settings use dd/mm/yyyy and I was trying to put into SQL statement data taken from an unbound textbox in a form.
The issue was due to the fact I was declaring, let's say, varMyDate as "date" type, so the engine reverted the format back even after the format.
Since you are really building a string the logical and proper data type is "string", and that solved the problem.