Proper Formatting in a listbox - vb.net

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.

Related

Search on a form that is filtered with a combobox

I have a form called LookUpByStore that displays records from a query. The records are products that we carry in our chain of retail stores. I have a combobox at the top of the form called StoreCombo that lists the store numbers as text. ("109", "111", etc.)
I have a textbox called txtSearch and a button called SearchBtn. I wrote the code below to fetch the store number from the StoreCombo and then search for products within that store only. The columns involved are:
StoreNum - which is chosen in the combobox
and then I need to search these three columns for whatever is entered in the textbox:
ItemNameWeight - which is a text field
ItemNum - which is a number
ItemUPCCode - which is text
Here is my code currently, which I understand searches the ItemNameWeight AND sets the StoreNum, while the ItemNum and ItemUPCCode are searched without the storenum being set. I had this set as a macro, which worked, but I need to add the StoreNum to the other two fields as well, so it will go over the 255 character limit for macros.
[StoreNum]=[Forms]![LookupByStore]![StoreCombo] And [ItemNameWeight] Like "*" & [Forms]![LookupByStore]![txtSearch] & "*" Or [ItemNum] Like "*" & [Forms]![LookupByStore]![txtSearch] & "*" Or [ItemUPCCode] Like "*" & [Forms]![LookupByStore]![txtSearch]
I tried to move the code to VBA like this:
Private Sub SearchBtn_Click()
DoCmd.ApplyFilter , [StoreNum] = [Forms]![LookupByStore]![StoreCombo] And [ItemNameWeight] Like "*" & [Forms]![LookupByStore]![txtSearch] & "*" Or [ItemNum] Like "*" & [Forms]![LookupByStore]![txtSearch] & "*" Or [ItemUPCCode] Like "*" & [Forms]![LookupByStore]![txtSearch] & "*"
End Sub
But this gives me an error 2427 "You entered an expression that has no value"
A) What am I doing wrong
and
B) What is the best practice to add StoreNum to the other two columns in VBA, because I know writing, "[StoreNum] = [Forms]![LookupByStore]![StoreCombo] And " each time is not the correct way to do it.
Thanks,
Tony
Review ApplyFilter
You need to enclose literal text within quote marks and concatenate with variables. Text fields requires apostrophe delimiters. All those [ ] are not really needed in this case as none of the names have spaces nor punctuation/special characters nor or are reserved words.
I've never used ApplyFilter method. I set form Filter and FilterOn properties. Review Allen Browne Search
Also, you are using AND and OR operators. Correct parenthesizing is critical - This AND THIS OR THIS is not the same as This AND (This OR This). Since you are using wildcard, I think you should just use AND operator. Wildcard really only works well with text type fields.
If the code is behind the referenced form, can use the Me alias.
DoCmd.ApplyFilter , "StoreNum = " & Me.StoreCombo & " AND ItemNameWeight Like '*" & Me.txtSearch & "*' AND ItemNum Like '*" & Me.txtSearch & "*' AND ItemUPCCode Like '*" & Me.xtSearch & "*'"

Formatting to Currency 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))

Visual Studio 2012 - Add multiple items on same writeline

Okay, so what i"m doing is creating a word file with my application with my labels and textboxes to create a receipt. In my word file, I am trying to write what is in a first name label beside what is in a last name label.
Here is the code I have, but I just seem to be missing something, because the word file keeps putting a huge gap between the first name and last name. I'm sure it's a very simple fix, but Google doesn't seem to be helping with what I am trying to describe.
printFile.WriteLine("Name: " & vbTab & FirstNameTextBox.Text + LastNameTextBox.Text)
I assume it must be the vbTab keyword.
which is fixed and cannot be changed.
That said you can manage a workaround as follows:
Public Const MyTab = " "
and in your code
printFile.WriteLine("Name: " & MyTab & FirstNameTextBox.Text + LastNameTextBox.Text)

Using a Dlookup in Access VBA when the field has a single quote

I've found a lot on this topic but still can't seem to get it to work. I have the following line of code:
If isNull(DLookup("id", my_table, my_field & "='" & temp_value & "'")) Then
The problem is a value in my_field of my_table is "O'Connell" (with a single quote), and I'm not sure how to get Dlookup to find it. I've tried using:
my_field & "=" & chr(34) & temp_value & chr(34)
And a host of other multi-quote options, but I just can't seem to get it to work. Though I can use VBA to modify the temp_value to include or not include the single quote, since the single quote already exists in the table, I need to make sure it matches. I'm just not sure how to tackle it.
Though the suggestions and answers here do work and resolve many issues with quotes in text, my issue ended up being related to the character I was seeing as a single quote not really being a single quote. For what it's worth, the data I was using was exported from Siebel, and the single quote I was seeing was actually chr(146), where a regular single quote (I say "regular" for lack of a better term) is chr(39).
If having issues with quotes, I found it helpful to examine the chr values of each character in the string. There may be a better way to do this, but this loop should help:
for i=1 to len(a_string)
debug.print mid(a_string,i,1) & " - " & asc(mid(a_string,i,1)
next i
The asc function gives you the chr code for a character, so this loops through the string and shows you each character and its associated chr code in the Immediate window (using debug.print). This also helps in finding other "hidden" (or non-visible) characters that may exist in a string.
Once discovered, I used the replace function to replace chr(146) with two single quotes (two chr(39)s), as suggested by HansUp, and that worked perfectly.
In this example, my_table is the name of my table and my_field is the name of a field in that table.
Dim strCriteria As String
Dim temp_value As String
temp_value = "O'Connell"
' use double instead of single quotes to avoid a '
' problem due to the single quote in the name '
strCriteria = "my_field = """ & temp_value & """"
Debug.Print strCriteria
If IsNull(DLookup("id", "my_table", strCriteria)) Then
MsgBox "no id found"
Else
MsgBox "id found"
End If
If you prefer, you can double up the single quotes within the name. This should work, but make sure you can distinguish between which are double and which are single quotes.
strCriteria = "my_field='" & Replace(temp_value, "'", "''") & "'"

Access 2010 SQL Query find partial match in string of full word only

I hope this is a simple one, I just can't find how to get the result I want, maybe I am using the wrong keyword in SQL?
I am searching an Employee table which contains a Full Name field, this field can be "Sam" or "Mr Evans" or "Mr Sam Evans"
I am trying to find partial matches with another Names table which contains a Name field - this is usually a short name such as "Sam" or "Evans" but could be "Sam Evans"
I have tried like, with and without * as follows
SELECT tblEmployee.FullName, tblNames.Name
FROM tblEmployee, tblNames
WHERE tblEmployee.FullName Like "*" & tblNames.Name & "*"
as well as the results I do want, this query can return Employee FullName of "Mr. Samson" which I don't want. I only want to see results contaning the full word "Sam" not FullNames with Sam in part of a word within the string. So I want to see Mr. Sam Evans and Mr. Sam Smith and Ms. Sally Evans, etc.
I hope this makes sense. Many thanks for your help
Edit - solution
I have solved this - just posting in case it might help anyone with a similar problem.
WHERE " " & tblEmployee.FullName & " " Like "* " & tblNames.Name & " *"
so basically by adding a space at the beginning and the end of the Employee Full Name, I catch all the correct records. I hadn't realised you could pad out a field like this but a colleague tipped me off!
Wildcards in SQL and access are a % symbol;
so
"SELECT tblEmployee.FullName, tblNames.Name FROM tblEmployee, tblNames
WHERE tblEmployee.FullName Like '%" & tblNames.Name & "%'"
Will match any instance of the string tblNames.Name
You can use the % at the start or end only to get the match of just the start of the string / end of the string repectively.
edit
Apologies, you can use * and ? in access as you expect, however are you sending the query as a string to some VBA code? Or trying to run it directly? As directly in a query it'll not be able to parse the tblNames.Name string into the query and you'll need to pass that in from a form or other peice of code.
Edit based on comment
To select a specific word it's just a bit of a work around required:
SELECT * FROM table WHERE field LIKE '* myWord *' OR field LIKE '* myWord.*'
You can optionally caputure start of sentances `LIKE 'MyWord *'
and word with commas, fullstops, exclamtion marks etc...
You could do an IN statement and have all the variations in a lookup table to keep it easy to maintain as another option.
I used this solution from the edited post in Access 2007 and it worked perfectly!
WHERE " " & tblEmployee.FullName & " " Like "* " & tblNames.Name & " *"
Now my question is, what the heck is the SQL code doing and why does it work? Can someone break it down?
EDITED:
OK, my confusion arose from the different use of characters in Access SQL vs more standard SQL. & is concatenate in Access, while + is concatnate in standard SQL. And * is the wildcard for any number of characters in Access while it is % in SQL.