I'm making a SIMPLE, and when I mean simple, I mean simple. But any way, I have this thing where, the person enters in a word, and it will preview a text file. I have everything setup even the text reader, but something unusual I get in my code is the "if statement". Whenever I type in the word into the textbox, it won't run the thing I want it to, instead it will go directly to the else and display the error message. This is the code I have:
If mainText.Text = "book" Then
startProcess()
ElseIf mainText.Text = "greet" Then //Just there for testing..
mainText.Text = "Hello there..."
Else
mainText.Text = "Please either enter a valid command."
End If
Now i looked into mainText.text and saw the value when entering the greet command..
{System.Windows.Forms.TextBox, Text: greet" & vbCrLf & "}
.. I don't really understand why this is happening.. All my other program work fine.
Can anyone help me solve this!??!
Thanks,
Kevin
Is the textbox multi-line? A single-line textbox shouldn't allow these characters. Either way, you can probably just trim the string.
Dim command As String = mainText.Text.Trim()
If command = "book" Then
startProcess()
ElseIf command = "greet" Then // Just there for testing..
mainText.Text = "Hello there..."
Else
mainText.Text = "Please either enter a valid command."
End If
The fact that it works when you break it sounds like a synchronization problem (multithreading) .. could be something connected to the timer you use.
Can you provide more information regarding your code/logic?
Related
In the following code I am trying to look for a text box in an open word document. Once that text box has been found I would also like to check to make sure the correct text is inside. This is the code that does not work and I could not figure out why.
Dim i as Integer
for i = 1 to Application.ActiveDocument.Shapes.Count
if Application.ActiveDocument.Shapes(i).Name = "Text Box 2" and _
instr(Application.ActiveDocument.Shapes(i).TextFrame.TextRange.Text, "[Grab your reader") then
` Execute
end if
next i
Now if I replace the and with another if statement it works.
Dim i as Integer
for i = 1 to Application.ActiveDocument.Shapes.Count
if Application.ActiveDocument.Shapes(i).Name = "Text Box 2" then
if instr(Application.ActiveDocument.Shapes(i).TextFrame.TextRange.Text, "[Grab your reader") then
` Execute
end if
end if
next i
From what I understand the and should work exactly the same as adding a new if statement. If anyone could tell me why this happens that would be great.
If i change it manually it works.
If then i run this:
Sub cellformat()
ShowCellFormat = Range("A1").NumberFormat
MsgBox ShowCellFormat
Range("A2").NumberFormat = ShowCellFormat
End Sub
this works too, msgbox displays:
Generic "randomtext"
The only thing that i cant manage is to change the format like this:
Range("A1").NumberFormat = "Generic "randomtext""
Tried 100 different ways still not working...
Assuming you want your number format to be something like "XYZ"0000"DEF" to display the number 15 as XYZ0015DEF, then you can't write your code as
Range("A1").NumberFormat = ""XYZ"0000"DEF""
'or
'Range("A1").NumberFormat = "Generic "randomtext""
You instead need to write the code as:
Range("A1").NumberFormat = """XYZ""0000""DEF"""
'or
'Range("A1").NumberFormat = "Generic ""randomtext"""
because each double-quotation mark (i.e. ") used within a string literal needs to be escaped by writing it as "".
I have a form on Access where I have 3 ComboBoxes (Section, Rooms: White House, Rooms: Churchills). I need to have it so when 'White House' is selected from the 'Section' ComboBox, 'Rooms: Churchills will be Locked. And the opposite for 'Churchills'.
I have the code:
Private Sub Section_Change()
If Section.Value = "White House" Then
Rooms__White_House.Enabled = True
Rooms__Churchills.Enabled = False
Else: Section.Value = "Churchills"
Rooms__White_House.Enabled = False
Rooms__Churchills.Enabled = True
End If
End Sub
But when I test the form it throws the error:
Compile Error: Argument Not Optional
Any Help Appreciated. Thanks in Advance!
The problem is that Section is a reserved word. So the compiler doesn't recognise it as the name of your combobox. Just change the name of the combo to something else, e.g. cboSection, and your code will compile. (You naturally need to change your code to match the new name).
This link shows a list of names that you should avoid when working with Access http://allenbrowne.com/AppIssueBadWord.html
BTW, your line
Else: Section.Value = "Churchills"
seems a bit odd as it is setting the value of the combo. I'm guessing that you are meaning to comment what the value must be if you enter the Else section. If that's the case then you need to add the apostrophe in front of the comment, i.e.
Else: 'Section.Value = "Churchills"
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
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