VB code question

1. Nov 19, 2008

Calculus!

Hey. This is what I have to do:

Fix the IF conditions inside the DO loop to handle ALL possible combinations of first name and last name searches (it is missing at least one right now) and see if you can make the code more efficient.

Dim searchFname As String, searchLname As String
Type Employee
Fname As String
Lname As String
Manager As String
Office As String
Phone As String
Hired As String
Salary As Single
End Type
Dim inEmp As Employee
Dim fuzzyFirst As Boolean, fuzzyLast As Boolean
Dim found As Boolean

fuzzyFirst = False
fuzzyLast = False
found = False

FileOpen(1, "Employee")

'Get data
If txtFname.Text = "" And txtLname.Text = "" Then
MsgBox ("Please provide search criteria")
Else
If txtFname.Text <> "" Then searchFname = txtFname.Text
If txtLname.Text <> "" Then searchLname = txtLname.Text

'Determine if fuzzy search
If InStr(searchFname, "*") > 0 Then
fuzzyFirst = True
'Reset search name
If Left(searchFname, 1) = "*" Then searchFname = Mid(searchFname, 2)
If Right(searchFname, 1) = "*" Then searchFname = Left(searchFname, Len(searchFname) - 1)
End If
If InStr(searchLname, "*") > 0 Then
fuzzyLast = True
'Reset search name
If Left(searchLname, 1) = "*" Then searchLname = Mid(searchLname, 2)
If Right(searchLname, 1) = "*" Then searchLname = Left(searchLname, Len(searchLname) - 1)
End If

'Loop through file
Do Until (EOF(1) Or found = True)
FileGet(1, inEmp)
'search first name
If fuzzyFirst Then
If InStr(inEmp.Fname, searchFname) > 0 Then found = True
Else
If inEmp.Fname = searchFname Then found = True
End If
'Check last name
If found And searchLname <> "" Then
If fuzzyLast Then
If InStr(inEmp.Lname, searchLname) = 0 Then found = False
Else
inEmp.Lname <> searchLname then found = False
End If
End If
If found = False Then
'check last name
If fuzzyLast Then
If InStr(inEmp.Lname, searchLname) > 0 Then found = True
Else
inEmp.Lname = searchLname then found = true
End If
End If
Loop

If found Then
'Set output text boxes
txtManager.Text = inEmp.Manager
txtDOH.Text = inEmp.Hired
txtSalary.Text = FormatCurrency(inEmp.Salary)
Else
MsgBox("There are no employees with that name")
End If
End If

Me.Refresh()

End If

FileClose(1)

2. Dec 1, 2008

Tanner65

I apologize for the long wait for a response, but I only recently joined this message board.

My first request to you would be to mention any pre-created form objects in your original post, because the first time you meander through the code it looks a little confusing. The second would be to mention what version of VB your using. Be it VB6 (or VBA) ,which I'm thinking this is by your use of Left not LSet,or .Net X.X. Also, code tags make the code easier to read.

To answer some of your questions:
You're doing two string compares on the same information, do one and call it good. Obviously, don't take my following example as the correct answer, but this is what I would do:
Code (Text):
Type Employee
Fname As String
Lname As String
Manager As String
Office As String
Phone As String
Hired As String
Salary As Single
End Type
Public Sub SetEmpInformation()
Dim searchFname As String, searchLname As String
Dim inEmp As Employee
Dim fuzzyFirst As Boolean, fuzzyLast As Boolean
Dim found As Boolean

fuzzyFirst = False
fuzzyLast = False
found = False

'Get data
If txtFname.Text = "" And txtLname.Text = "" Then
MsgBox ("Please provide search criteria")
Exit Sub
End If

If txtFname.Text <> "" Then searchFname = txtFname.Text
If txtLname.Text <> "" Then searchLname = txtLname.Text

'Determine if fuzzy search
If InStr(searchFname, "*") > 0 Then
fuzzyFirst = True
'Reset search name
If Left(searchFname, 1) = "*" Then searchFname = Mid(searchFname, 2)
If Right(searchFname, 1) = "*" Then searchFname = Left(searchFname, Len(searchFname) - 1)
End If
If InStr(searchLname, "*") > 0 Then
fuzzyLast = True
'Reset search name
If Left(searchLname, 1) = "*" Then searchLname = Mid(searchLname, 2)
If Right(searchLname, 1) = "*" Then searchLname = Left(searchLname, Len(searchLname) - 1)
End If

FileOpen 1, "Employee"

'Loop through file
Do Until (EOF(1) Or found = True)
FileGet 1, inEmp
If fuzzyFirst And fuzzyLast Then
found = (InStr(inEmp.Fname, searchFname) > 0 And InStr(inEmp.Lname, searchLname) > 0)
ElseIf Not fuzzyFirst And Not fuzzyLast Then
found = (inEmp.Fname = searchFname And inEmp.Lname = searchLname)
Else
'search first name
If fuzzyFirst Then
If InStr(inEmp.Fname, searchFname) > 0 Then found = True
Else
found = inEmp.Fname = searchFname
End If
'Check last name
If fuzzyLast Then
If InStr(inEmp.Lname, searchLname) > 0 Then found = True
Else
found = inEmp.Lname = searchLname
End If
End If
Loop

FileClose (1)

If found Then
'Set output text boxes
txtManager.Text = inEmp.Manager
txtDOH.Text = inEmp.Hired
txtSalary.Text = FormatCurrency(inEmp.Salary)
Else
MsgBox ("There are no employees with that name")
End If

End Sub
The changes starting from the top:
The type isn't a procedural declaration, but the rest can be, so move them all into the procedure unless it is needed elsewhere in your program.
When it checks that the two text boxes aren't empty, throw an exit sub/function in there and kill the if statement. Some people disagree with this because of error handling and the such, so send it to a label if that's their gripe.
Open the file right before it's being used and if it were my program rather than a homework assignment, I'd make inEmp an array and populate the array using a function that open, reads the whole file into the array and then closes it again. That way any other programs using the file wont be walking on each other.
The first if in the loop handles the times when both the fuzzy searches are on. A Boolean can be set both using
Code (Text):
If 2+2=4 Then Boolean=True Else Boolean = False
or
Code (Text):
Boolean = 2+2=4
both of which result in True. (Hope that makes sense, ask if it doesn't.) So, in this one line I'm essentially doing
Code (Text):
If InStr(inEmp.Fname, searchFname) > 0 Then
If InStr(inEmp.Lname, searchLname) > 0 Then found = True
End If
The same is true for the second If statement handling the times when neither Fuzzy's are true.
And lastly, the Else statement, the red head step child of the If/Then statement. :-) This is handling the fuzzys when 1 or the other is true.
After the loop I'm closing the file.

I hope I was able to help you with your assignment, feel free to question my changes, it is early in the morning here.

3. Dec 1, 2008

Calculus!

Thank you very much. Everything that I had to do, you did. Thanks Again. :D

4. Dec 1, 2008

Tanner65

If that's the case can I get partial credit for the assignment?

Do you at least understand what I had done?

5. Dec 1, 2008

Calculus!

haha sure you can get partial credit!

Yes, I do understand what you did. Thanks again. You really clarified certain things I didn't understand.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook