I want to create a fake run box to use against Technical Support Scammers to waste their time. I want to have some specified text run a msgbox. Is there a way to accept all cases? (notepad, NoTePaD, NOTEPAD etc.).
If textbox1.text = "Notepad" Then
Msgbox("Unable to open")
I'm a bit confused as to what "all versions of it" means, however the following will ignore spaces and capitalization:
Dim str As String = textbox1.text.ToLower().Replace(" ", "")
If str = "notepad" Then MsgBox("Unable to open")
This is a VB.Net solution, however as Plutonix mentioned in the comments, it's hard to tell what sort of solution you're looking for - you've tagged two different types of VB.
Related
I am working on a project for the company I work for, designing a database to keep track of and create project numbers. I have it up and running, but my supervisor has asked that I include user-input sanitizing to escape special characters that could cause a problem for the existing code and SQL. I have a few different user-input forms, which are just bound text boxes which get entered into my table when the form is closed. I also have one Input Box, which asks for the project number which an employee would like to update the info for.
From my understanding, a local Access database on our company server is not going to be very prone to SQL injection, and MS Access has a way of handling injection which I do not really understand. However, I am looking for a list of characters which could potentially cause problems, where they could potentially cause problems, and the best way to deal with them.
I have tried inputting a few different special characters which I know to be problematic into the text boxes on the forms, but Access just parses them straight into the record, with no errors. I DO have one function written in which replaces single apostrophes with two apostrophes, and this is used on the InputBox.
Here is the code behind the InputBox:
Private Sub btnOpenUpdate_Click()
Dim strInput$, safeInput$
strInput = InputBox("Enter the EP-Number for the project that you would like to update:", "Update Project")
safeInput = safeEntry(strInput) 'change all single apostrophes to double apostrophes '
If Len(safeInput & vbNullString) > 0 Then
If DCount("EPNumber", "tblProjects", "EPNumber = '" & safeInput & "'") > 0 Then
DoCmd.OpenForm "frmUpdateProject", , , "EPNumber = '" & safeInput & "'"
Else
MsgBox "Please enter a valid EP-Number.", vbInformation, "Error: Invalid EP-Number entered"
End If
Else
MsgBox "The field was left blank. Please enter a valid EP-number.", vbInformation, "Error: Empty field"
End If
End Sub
And here is the code behind the safeEntry function:
Public Function safeEntry(strEntry)
safeEntry = Replace(Nz(strEntry), "'", "''")
End Function
Apologies for the somewhat lengthy summary of my situation, but any help and input would be very appreciated, as I am fairly new to the world of MS Access and SQL, and I am trying my best to learn how to protect the database.
I want to write some text to a .txt file, this is what I have, and it works mostly:
If System.IO.File.Exists(sListItems) = True Then
Dim writeToFile As New System.IO.StreamWriter(sListItems, True)
writeToFile.WriteLine(vbCrLf & txtGameTitle.Text)
writeToFile.Close()
Else
MsgBox("Error!")
End If
End If
The problem is with that, when I enter text such as 'Hello', it would instead put into the txt file 'Hello ' (with a space). Is there anyway to resolve this?
Don't use File.Exists(). It's a race condition waiting to blow up, and it's slower. Instead, look for the FileMode that matches how you want to use the file; then handle the exception if it fails (you need to handle the exception anyway, because of the race condition potential mentioned earlier).
Additionally, if you have your Using blocks correct, you don't need to call .Close().
Try
Using fs As New FileStream(sListItems, FileMode.Open)
fs.Seek(0, SeekOrigin.End)
Using sw As New StreamWriter(fs)
sw.WriteLine(txtGameTitle.Text)
End Using
End Using
Catch ex As IOException
MsgBox("Error! -- " & ex.Message)
End If
But I wonder if you really care that the file already exists, and all you really need to do is this:
File.AppendAllText(sListItems, txtGameTitle.Text)
Well, I think I figured it out. I changed:
writeToFile.WriteLine()
to
writeToFile.Write()
and it no longer gives spaces.
Edit:
I know what my problem was, I was using lbGameList.SelectedItem.ToString() to find the name and it was giving me spaces, so I added .TrimEnd to it and it works now, thanks
At some point of execusion of my project, the input to the database is in the format:
Buy Requirement of "xxxxxx & xxxxxx" through mycompany.com
It results in an incorrect SQL syntax error. I need to replace " with white space. I searched in google but no helpful suggestions were there.
How to replace " with whitespace?
Dim str As String
str="Buy Requirement of "Telemarketing & ERP Software" through IndiaMART.com"
' TODO: perform replace
' result
str = "Buy Requirement of Telemarketing & ERP Software through IndiaMART.com"
I finally understand what you are asking now...
Here, this is what you want to be using: teststring.Replace(""""c, "")
If you really want to use Linq and extension methods, then use this: New String(teststring.Where(Function(c) Char.IsLetterOrDigit(c) OrElse Char.IsWhiteSpace(c)).ToArray()).
But that's just making things complicated for no reason.
I got the error "Invalid Characters in Path." My path is being output to a .txt file using the $findstr command and get deleted in the process of the application so I wrote a simple sub to output the string to a Textbox to see what was throwing the error.
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim Folder = Application.StartupPath & "\PRODUCTION"
Dim MyFile As String = File.ReadAllText(Folder & "\results3.txt")
MyFile.Replace("/n", "") ' One of my various attempts
TextBox2.Text = Folder & "\" & MyFile & "<--END"
End Sub
An example of what is showing up:
C:/user/.../file.asasm
<--END
For some reason the findstr command is adding a second blank line after my path and I have so far been unable to remove it. At the advice of several other threads on here I have tried:
MyFile.Trim() ' From MSDN is seems this only removes null spaces and not blank lines
MyFile.TrimEnd() ' Same result as trim
MyFile.Replace("/n","") ' I'm not sure if the /n parameter can be used in this way
MyFile.Replace(10,"") ' I thought this one for sure would work since 10 corresponds to a new line in the ASCII Table
Any help resolving this would be greatly appreciated. I realize there is several other threads on this topic, but none of the solutions seem to be working here.
REVISED:
The solution I implemented in my code was
MyFile = MyFile.Replace(Envirnment.NewLine,"")
Have you tried:
MyFile.Replace(Environment.NewLine, "")
Which on Windows should be the same as:
MyFile.Replace(vbCrLf, "")
Also try:
MyFile.Replace(vbCr, "")
or
MyFile.Replace(vbLf, "")
Depending on the type of newline you have, either should work for you.
EDIT: regarding why MyFile.Replace(10,"") did not work for you. You became a victim of implicit type conversion (a feature of VB.NET), so this line was executed as:
MyFile.Replace("10","")
Please make sure you have Option Strict On, if you want to avoid such issues.
Trim will also work MyFile = MyFile.Trim(vbNewLine.ToCharArray)
Environment.NewLine is probably the best way to refer to a newline. "/n" is definitely not. For one thing, you're surely thinking of "\n", not "/n" - and for another, in vb.net even backslashes are interpreted literally (not as a control character).
I am using visual basic to write a Macro for Autodesk Inventor. I created a macro that calls a file dialog, see code below. Everything works fine except when a user puts a file name in with a period and a number greater than zero following it.
For example, if a user puts testfile.test in the box and hits ok. When I ask for what they put in there using .FileName, I get "testfile.test". Just like I should.
However, if the user puts testfile.1 or testfile.10 or testfile.1mdksj or anything as long as a number greater than zero directly follows the period I get back "testfile". For some reason, everything after the period and the period gets removed.
What is the reason for this? Is this a bug in visual basic or am I doing something wrong?
'Set up the file dialog
Dim oFileDlg As FileDialog
' Create a new FileDialog object.
Call ThisApplication.CreateFileDialog(oFileDlg)
'Define the filter to select part and assembly files or any file.
oFileDlg.Filter = "All Files (*.*)|*.*"
'Define the part and assembly files filter to be the default filter.
oFileDlg.FilterIndex = 1
'Set the title for the dialog.
oFileDlg.DialogTitle = "Save File As"
'Tell the dialog box to throw up and error when cancel is hit by user
oFileDlg.CancelError = True
'Show the file dialog
On Error Resume Next
oFileDlg.ShowSave
'save the user specified file
Dim newFileName As String
newFileName = oFileDlg.FileName
UPDATE:
I ended up doing the following "hack" to make things still work while dealing with a period:
oFileDlg.fileName = sFname & "."
oFileDlg.ShowSave
fullName = Left$(oFileDlg.fileName, Len(oFileDlg.fileName) - 1)
That worked fine for quite a while on Windows 7 and then Windows 10. Unfortunately, the Windows 10 Creative update seems to have changed how the file dialog works. With the above code, fullName would come back blank if there were no periods in the name and would truncate everything from the FIRST period from the left if there was a period in the name.
I'm not really sure what changed in Windows 10, but it pretty much destroyed my hack. Windows 7 still works fine and Windows 10 before the creative update works. I ended up doing the following to make everything work again in the version of Windows I mentioned above.
oFileDlg.fileName = sFname & ".00"
oFileDlg.ShowSave
fullName = Left$(oFileDlg.fileName, Len(oFileDlg.fileName) - 3)
This is a VB property, but it may extend to VBA as well. Have you tried setting the save settings to support multidotted extensions? Try something like this:
SupportMultiDottedExtensions = True
This setting is intended permit the use dotted extensions - meaning the use of periods in the file name. See this MSDN reference for documentation and information: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.supportmultidottedextensions.aspx#Y129
This SO article may also shed further light: SaveAs Dialog with a period in the filename does not return extension
EDIT
After checking the autodesk documentation - a difficult and unpleasant task, in my opinion - there does indeed appear to be no support for MultidottedExtensions. I did, however, find a function on VBAExpress that I have very closely adapted. The function can be used to filter strings with contain unacceptable characters. Jimmy Pena's blog has an excellent function for just such a purpose: http://www.jpsoftwaretech.com/excel-vba/validate-filenames/. I have only substantively added a period and a replace to the code:
'A function for filtering strings, with a focus on filenames.
Function FilterFileNameString(stringToScrub As String) As String
'Filters a filename string - or any string for that matter.
Dim FilteredString As String
'A highly nested replace function.
FilteredString = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(stringToScrub, ".","|", ""), ">", ""), "<", ""), Chr(34), ""), "?", ""), "*", ""), ":", ""), "/", ""), "\", "")
'Returns filtered string.
FilterFileNameString = FilteredString
End Function
Jimmy Pena's blog also contains a recursive version as well, although he does not recommend it.
You can filter any strings to be used as filenames with another character - a space in this case. You could use an underscore, however, or any other character you deemed pleasant.
In general, if you are trying to use periods for versioning or a similar purpose, and inventor will not let you, I would strongly advise going to another character or set of characters that can provide such an indication, such an underscore "_", a numbering system, "001", "002", a lettering system, "AAA", "AAB", or whatever makes sense for your focus.
If you are just making the application user-friendly, I would suggest filtering the strings entered before saving them in the desired filetype, and separate the filtering of the strings from the save dialog if the period filtering gives you grief. It may add an extra step, but it may be the best and easiest way to filter out pesky invalid characters without creating unnecessary extra hassles for your users.
~JOL