vb.net Input Box Function - vb.net

I am using an input box. In tat am getting the number as input. When the user presses OK the program is workin. But when I press the cancel button am getting a error message
" Conversion from String "" to 'Integer' type is not valid "
If i press cancel I need the program to end.
and also i want to know how to move to other form when i press cancel in input box

Its probably a good idea to use try parse for these situations it handles more cases than empty strings for example non numeric characters characters
Dim number As Integer
Dim result As Boolean = Int32.TryParse(inputBox.Text, number)
if Not result Then
number = 0
End If

If user didn't enter anything in the input box and press cancel, it will be an empty string. Your system won't be able to convert an empty string to integer. Therefore, your program should handle this scenario with code similar to below.
If inputBox.Text = "" Then
inputValue = 0
Else
inputValue = inputBox.Text

Related

Access Public Function for Maintain Combo Lists

I have over 40 combo controls in my application. I am working on developing a public function, to put in the not in list event of every combo. The goal is to have 1 continuous pop up form, which will open, if the user says they want to add a new value to the combo. The open form command will pass open args for the
record source
the control source for the 1 text box on the continuous form (generically the type)
the label for the control source.
I'm having some trouble getting it to pass the open args. I debug.print the parts of the args, I can split them, when they get passed (inconsistent results getting the open args to pass correctly, as I try to debug), and I cannot seem to set the record source for the pop up form correctly. I've tried doing 1 at a time, and still can't seem to get it.
This is the public function:
Option Explicit
Public Function TypeNotInList(ctl As Control, arg1 As String, arg2 As Variant, arg3 As String)
On Error GoTo Err_TypeNotInList
Dim Msg, Style, Title
'arg1 is the row source of the combo, to be passed as the recordsource for the frmAddTypeVal form
'arg2 is the control source of the combo, to be passed as the control source for the text box in the frmAddTypeVal form
'arg3 is the label of the combo, to be used for messages, and the label of the text box in the frmAddTypeVal form
Msg = "The " & arg3 & " you entered is not in the " & arg3 & " list, would you like to add it now?"
Style = vbYesNo
Title = "Type or listing must be maintained"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
ctl.Undo
DoCmd.OpenForm "frmAddTypeVal", acNormal, , , , acDialog, arg1 & "|" & arg2 & "|" & arg3
ctl.Requery
End If
Exit_TypeNotInList:
Exit Function
Err_TypeNotInList:
MsgBox Err.Description
Resume Exit_TypeNotInList
End Function
This is how I am calling it, in 1 combo's Not In List event:
Option Explicit
Private Sub FKAuditType_NotInList(NewData As String, Response As Integer)
Dim a1 As String
Dim a2 As String
Dim a3 As String
a1 = Me.FKTypeXYZ.RowSource
a2 = "txtXYZType"
a3 = Me.lblTypeXYZ.Caption
TypeNotInList Me.FKTypeXYZ, a1, a2, a3
Response = acDataErrContinue
End Sub
That should be calling the public function, and passing the 4 parameters.
This is the form load of the generic continuous pop-up form, called frmAddTypeVal:
Option Explicit
Private Sub Form_Load()
Dim VarArgs() As String
VarArgs = Split(Me.OpenArgs, "|")
Me.Form.RecordSource = VarArgs(0)
Me.txtType.ControlSource = VarArgs(1)
Me.lblType.Caption = VarArgs(2)
End Sub
When I run this as is, my debug.print (s) give me the following:
ctl = FKFKTypeXYZ
arg1 = SELECT tblXYZType.ID, tblXYZType.txtXYZType FROM tblXYZType ORDER BY tblXYZType.txtXYZType;
arg2 = FKXYZType
arg3 = XYZ Type
openargs =
I get each value, but the open args is null. What the heck, Beck?
Can anyone help guide this clueless coder? lol
Thanks!
I edited this to update the code, with the changes made. Now it's working! At least the first part of the process. The openargs get passed and the pop-up form works correctly. When I click close on that form, I'm back on the form with the notinlist combo. That resumes it's process and I get a message: The text you entered isn't an item in the list.
It know that's the default notinlist message. The thing is, the public function is supposed to handle this. It has, in the if Response = vbYes Then
ctl.undo
'undo trying to add a value that is not in the list yet
and then after the open form (which would involve the close of that form, I would think)
ctl.requery
'requery the combo, so the added value(s) can be seen
Anyone know how I can adjust this to prevent that message, but not just disable all warnings?
Thanks!
Got it! In the not in list, after I call the public function, I have to add:
Response = acDataErrContinue
This let's the default error message take a seat lol.
Thanks for all the help! This is going to make setting this up for every darn combo so much easier!!!!

Reading Numbers from Webbrowser Vb.net

In my WebBrowser I got text like this: Remaining balance: 10$
I would like to convert it to another currency, I want just to read number from my browser, then after that I will send it to a Label or TextBox with the new converted currency. I am stuck here.
A screenshot of it
Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
WebBrowser2.Navigate(TextBox9.Text + TextBox2.Text)
End Sub
Private Sub WebBrowser2_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
Label1.Text = (WebBrowser2.Document.Body.InnerText)
End Sub
I have this suggestion (I know this is probably not the perfect solution):
First, try loading that same webpage in a normal web browser such as Google Chrome or Firefox or any browser which has the feature to "inspect elements", meaning to view their HTML code.
Find out the element which displays the price you want.
Note down the ID of the element (usually written something like id="someID")
Back to your program's code, include the following Function, which will get the text displayed and convert it to another currency:
Public Function ConvertDisplayedCurrency() As Decimal
Dim MyCurrencyElement As HtmlElement = WebBrowser2.Document.GetElementById("theIdYouGotInStep3") 'This will refer to the element you want.
Dim TheTextDisplayed As String = MyCurrencyElement.InnerText 'This will refer to the text that is displayed.
'Assuming that the text begins like "Remaining balance: ###", you need to strip off that first part, which is "Remaining balance: ".
Dim TheNumberDisplayed As String = TheTextDisplayed.Substring(19)
'The final variable TheNumberDisplayed will be resulting into a String like only the number.
Dim ParsedNumber As Decimal = 0 'A variable which will be used below.
Dim ParseSucceeded As Boolean = Decimal.TryParse(TheNumberDisplayed, ParsedNumber)
'The statement above will TRY converting the String TheNumberDisplayed to a Decimal.
'If it succeeds, the number will be set to the variable ParsedNumber and the variable
'ParseSucceeded will be True. If the conversion fails, the ParseSucceeded will be set
'to False.
If Not ParseSucceeded = True Then Return 0 : Exit Function 'This will return 0 and quit the Function if the parse was a failure.
'Now here comes your turn. Write your own statements to convert the number "ParsedNumber"
'to your new currency and finally write "Return MyFinalVariableName" in the end.
End Function
Call that Function probably when the document of the WebBrowser2 loads, that is, in the WebBrowser2_DocumentCompleted sub.
I hope it helps!

Creating error macro

I currently have a macro that will deliver a pop up in the event that a field is left blank. The problem that I have is, once you acknowledge the error, it goes away and lets you proceed without filling in the field. Does anyone know what I am missing in the macro? I won't post the entire script because it is very long but it currently looks like this...
If Range("L58") = "Sigma Network" And Range("M58") = "" Then MsgBox "Please enter cable length "
Can I create one script that will work for all of the others or does each need to have its own?
Very simply, something like:
If Range("L58") = "Sigma Network" And Range("M58") = "" Then
Range("M58").Value = InputBox("Please enter cable length ", "Input value!", 0)
End If
You would of course need additional logic to prevent the user from entering a 0-value or empty string, etc.,
If you're doing this on for example, 50 different pairs of cells, (say L58 to L107 and M58 to M107 for example) this is a basic loop structure that you could use:
Dim cl as Range
For Each cl in Range("L58:L107")
If cl.Value = "Sigma Network" and cl.Offset(0,1).Value = "" Then
cl.Offset(0,1).Value = GetValue("Please enter cable length ", "Input value!", 0)
End If
Next
The loop can be further refined (e.g., if "Sigma Network" isn't the only thing you're checking for, or if you need different message text for the input box based on some other conditions, etc.
This will require the custom GetValue function which invokes the InputBox prompt, and configured to prevent 0-value input from the user. Additional logic may be required to prevent other types of data entry.
Function GetValue(msg as String, _
Optional title as String = "", _
Optional default as Double = 0)
'Function that calls the InputBox method
Dim ret
Do Until ret <> 0
ret = InputBox(msg, title, default)
Loop
GetValue = ret
End Function

Setting TextBox so user cannot type in non-Numeric characters or spaces

Is there a way to set a TextBox in vb such that it does not accept a space or a non-Numeric character entered by a user? I am writing a program that has a TextBox called phoneField in which the user should type in only numbers and if the user trys to type a non-Numeric character or space, nothing should be displayed in the TextBox. How could that be done?
First use 0's in your mask to allow only numeric characters.
Second, assign the ResetOnSpace property to false
Me.MaskedTextBox.ResetOnSpace = False
This will reject any spaces the user enters unless it is part of the prompt.
This is the wrong way to handle this. You'll drive your users insane with rage at your app. Instead, let them enter whatever they want, and have code on the backend that first strips out any non-digit and then validates the result.
Use MaskedTextBox Class
something alike:
Me.MaskedTextBox1.Mask = "0-000-0000000"
I've done similar to this using an inherited TextBox control.
The bonus of this way of doing it is it allows cut, copy and paste to function correctly.
''' <summary>
''' A TextBox control that only allows numeric input
''' </summary>
''' <remarks>Allows cut, copy and paste</remarks>
Public Class NumericTextBox
Inherits TextBox
Private _textBefore As String = ""
Protected Overrides Sub OnTextChanged(e As System.EventArgs)
Me.SuspendLayout()
If MyBase.Text.Length > 0 AndAlso Not IsNumeric(MyBase.Text) Then
' The text has been changed to a non numeric value
Dim selectionStart As Integer = MyBase.SelectionStart
MyBase.Text = _textBefore
MyBase.SelectionStart = selectionStart
Else
' The current text is numeric (or blank) remember it in case it changes to an invalid value
_textBefore = MyBase.Text
End If
MyBase.OnTextChanged(e)
Me.ResumeLayout()
End Sub
End Class
Actually the correct settings that work if your prompt character is a space:
.AllowPromptAsInput = False
.Mask = "00"
.ResetOnPrompt = False
.ResetOnSpace = True

Vb.net Input box Function

Am using An inputbox in my form.
If i Press OK the code is fine.
When i Press cancel the Program displayin an error. Wat should i Do ?
You should look for an empty string
Dim MyString As String
MyString = InputBox("Please enter something", "Request Info", Nothing)
If (MyString Is Nothing OrElse MyString = "") Then
'User hit cancel
Else
'Read MyString
End If
Change the code that is after the InputBox to support empty strings. InputBox will return an empty string if you cancel so the reason for the error must be that your code expects the string to have a lenght > 0.
If you edit the question to show the code that calls InputBox as well as a few lines following that line, someone can probably point out the exact error.
See the documentation for a working sample:
Interaction.InputBox Method