Selecting specific text from a textbox in vb.net - vb.net

I had this select query where textbox value is my parameter.
The case is, when I type text in the textbox it will be the reference for my select query. For example below,
Suppose my textbox has already a content;
123 32 5
And then i will add some text for my select query
123 32 5 66
The return value will be "66". And if i add another text
123 32 55 66 6952
The return value will be "6952". And So on, regardless of the length of the text
My idea here is that I start selecting the values from the first space to the right of the textbox. But I have no idea on how to do it.
Please help. Thanks

Here's an answer to your question. Now if you have a question related to your code then you should post some code to go with it showing you have done a bit of work. I'll give you a hint. Strings.Split()
This gives the same result....the last item in your split
Dim xx As String = "438 2374 8437 46327"
Dim xstr As String = xx.Split(" ").Last
Dim xxstr() As String = xx.Split(" ")
MsgBox(xstr)
MsgBox(xxstr(xxstr.Count - 1))
Play with strings!

Related

change credit card coding

I have a mail merge VBA code of:
If GetField(oDS, "mCCNumber") <> "" Then
sCCNumber = Right(GetField(oDS, "mCCNumber"), Len(GetField(oDS, "mCCNumber")) - 12)
but now our CC #s are only 4 characters with no tokens, but some still come through with the 16 characters.
how do I change this code to add "OR" 3 characters (some are 3 and others 4).
also, I cannot comment this out to stop this portion
First, GetField(oDS, "mCCNumber") is being dereferenced 3 times in these two lines alone - pull it into its own local variable:
Dim ccNumberField As Field
Set ccNumberField = GetField(oDS, "mCCNumber")
Actually we're more interested in the field's contents. Your code is making an implicit default member call against the Field object - but a Field's default property is its Code:
A field's code is everything that's enclosed by the field characters ({ }) including the leading space and trailing space characters.
That means the string you're working with is longer than your code is expecting. I'd think you'd be more interested in the Text of the field's Result:
Dim ccNumberFieldValue As String
ccNumberFiedValue = ccNumberField.Result.Text
If ccNumberFieldValue <> "" Then
'...
Now, ccNumberFieldValue should hold the field's actual content. I'm not 100% clear from the OP exactly what that contains though. So I'm going to assume one of such:
Last 4 digits: **** **** **** 1234
Last 3 digits: **** **** **** *234
Woopsie, all 16 digits: 1234 1234 1234 1234
So the first thing to do is to grab the last 4; Right$ does that:
sCCNumber = Right$(ccNumberFiedValue, 4)
That leaves us with either:
1234
*234
From there you can use IsNumeric to determine if the entire substring is numeric (assuming there's a non-numeric padding character) - if it isn't, then you know you're looking at the 3-character variant:
If Not IsNumeric(sCCNumber) Then sCCNumber = Right$(sCCNumber, 3)

Need to identify first character of my string (variable)

I have a string called Policynumber
I need to know if it begins with a 1 or a 2 so I can create an if statement for if it starts with 1 do something and if it starts with 2 to do something else.
I keep finding ways to do this for string text but not for a variable.
I have tried doing the following:
If policynumber Like "1*" Then
display.text = policynumber
End If
I am simply looking for possible ways to know what the first character is and therefore determine if it's a 1 or a 2. When I try using the variable name or even a textbox.text I get no result in the display textbox so I know it's not working.
I would use VBA's ASC() function for this purpose. This function returns the ASCII code of the first character of a string. ASCII for the character 1 is 49 and 2 is 50. Therefore ...
Dim PolicyNumber As String
Dim n As Integer
PolicNumber = "1ABC-45678910"
n = ASC(PolicyNumber)
MsgBox "Policy number starts with a " & Chr(n)
For testing the result you can use either If n = 49 Then or If Chr(n) = "1" Then.

Checking if textbox corresponds with any of the strings of a text file.VB.NET VB2010

I'm working to make a POS software in vb2010,but i'm stuck at this issue:
I'm trying to make the following: * means that it's what I want how the code to be:
if textbox1.text = *matches to a barcode then
dim bar as string
dim price as string
dim name as string
bar = *get.currentline.ofbarcode()
price = readtextline(bar+1)
name = readtextline(bar+2)
namelist.items.add(name)
pricelist.items.add(price)
end if
NOTE: I'm not a begginer!I'm medium advanced
If it exists another post that's the same that I requested,you can write me with the topic and close this topic.I'm still learning...
Thank you!
EDIT:
The file form is like so(it's C:\price.ini):
barcode1
price1
name1
barcode2
price2
name2
at end,
9879,security label
and so on...
Thank you again.
The easiest way would be to load all the barcodes from the file in to an array or list, then you can loop through the array and see if any of the barcodes match the entered code
If you have control of how the text file is created, you might find it easier to have one line (containing barcode, price and name) for each item, but the following code uses the format that you have posted.
You can use System.IO.ReadAllLines to read the entire text file into a String array. Then loop through the array checking every third element to see if it is the barcode you want. When you find what you want, add the next two elements to your ListBoxes.
Dim lines() As String = IO.File.ReadAllLines("C:\price.ini")
For i As Integer = 0 To lines.Length - 1 Step 3
If lines(i) = TextBox1.Text Then
priceList.Add(lines(i + 1))
nameList.Add(lines(i + 2))
Exit For
End If
Next
If I run this code with the text file that you posted, I see that with "999" in TextBox1, I see "1" in priceList and "Soap" in nameList; and with "333" in TextBox1, I see "2" in priceList and "Bread" in nameList.

How can i get a large number of text boxs to subtract from each other

I'm trying to make a program where a user inputs numbers into a subtraction equation and the program tells them if they are right or wrong and what the correct answer is in a label. There are 20 different equations with 3 text boxes each. The first two text boxes are for the two numbers that are being subtracted and the third text box is the answer. I declared them into a array but I can't figure out how make them subtract. The code i have so far is:
Dim i As Integer
Dim txtNumber1() As TextBox = {txt1Number1, txt2Number1, txt3Number1, txt4Number1, txt5Number1, txt6Number1, txt7Number1, txt8Number1, txt9Number1, txt10Number1, txt11Number1, txt12Number1, txt13Number1, txt14Number1, txt15Number1, txt16Number1, txt17Number1, txt18Number1, txt19Number1, txt20Number1}
Dim txtNumber2() As TextBox = {txt1Number2, txt2Number2, txt3Number2, txt4Number2, txt5Number2, txt6Number2, txt7Number2, txt8Number2, txt9Number2, txt10Number2, txt11Number2, txt12Number2, txt13Number2, txt14Number2, txt15Number2, txt16Number2, txt17Number2, txt18Number2, txt19Number2, txt20Number2}
Dim txtAnswer() As TextBox = {txt1Answer, txt2Answer, txt3Answer, txt4Answer, txt5Answer, txt6Answer, txt7Answer, txt8Answer, txt9Answer, txt10Answer, txt11Answer, txt12Answer, txt13Answer, txt14Answer, txt15Answer, txt16Answer, txt17Answer, txt18Answer, txt19Answer, txt20Answer}
Dim intAnswer() As Integer
For i = 0 To txtNumber1.Length - 1
intAnswer(i) = txtNumber1(i) - txtNumber2(i)
Next
I also can't figure out how i would make each answer display into a label. I think it would be some like
If intAnswer(0) = txtAnswer(0) Then
Me.lblAnswer1.Text = "Correct:" & intAnswer(0)
Else
Me.lblAnswer1.Text = "Incorrect:" & intAnswer(0)
End If
But I'm not sure how i would loop that to make it do all 20 labels, or would i just need to have it 20 different times, one for each label.
Thanks for the help.
Best to create a user control with 3 labels and 3 textboxes on each. Then you only need to code this much, and wrap this logic in a loop to repeat as many times as you want. Basically, narrow down your problem to "I only have 1 equation", solve it using this approach, the rest is as easy as using adding a loop to your code.

excel 2007: append certain characters to value in a column to make it proper lenght

I have following values in a column :
123
456
789
65
1
I want to append correct number of zeros in all the values in that column such that the total length of character is 5.
00123
00456
00789
00065
00001
How do I do that?
If there is one number per cell, you can do this easily by changing the format to "Custom."
Right-click on the cells you would like to format.
From the context menu, choose "Format cells"
Choose the Custom category.
Over the word "General," in the Type textbox, enter 00000. (This tells Excel to print with
leading 0s).
Click OK.
If the number is bigger than five digits, it will print all of the digits.
===EDIT===
You explained that these were all in one cell. #paulmorriss has an excellent Excel-function-only solution, but let me proffer a VBA solution as an alternative:
Sub Macro1()
Dim txt As String
Dim asWords() As String
Dim zeros As String
txt = vbNullString
asWords = Split(Range("A1").Value) 'asWords(0)="123" etc.
For i = 0 To UBound(asWords) ' emulate StrDup (missing in VBA)
zeros = vbNullString
For j = Len(asWords(i)) + 1 To 5: zeros = zeros + "0": Next j
txt = txt + zeros + asWords(i) + " "
Next i
Range("B1").Value = txt 'Places answer in B1
End Sub
If the value you specify is in cell A1 then put the following formulae in B1 to K1. The value in K1 is what you need. You could specify one massive formula, but it's easier for people maintaining the spreadsheet to see what's going on if it's split up like this.
in B1 =TEXT(VALUE(LEFT(A1,SEARCH(" ",A1))),"000000")
in C1 =RIGHT(A1,LEN(A1)-SEARCH(" ",A1))
etc. =TEXT(VALUE(LEFT(C1,SEARCH(" ",C1))),"000000")
=RIGHT(C1,LEN(C1)-SEARCH(" ",C1))
=TEXT(VALUE(LEFT(E1,SEARCH(" ",E1))),"000000")
=RIGHT(E1,LEN(E1)-SEARCH(" ",E1))
=TEXT(VALUE(LEFT(G1,SEARCH(" ",G1))),"000000")
=RIGHT(G1,LEN(G1)-SEARCH(" ",G1))
=TEXT(I1,"000000")
=B1&" "&D1&" "&F1&" "&H1&" "&J1