Fix IF Conditions in VB Code DO Loop

  • Thread starter Thread starter Calculus!
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion focuses on improving a Visual Basic code snippet that searches for employee records based on first and last names. Key points include the need to fix the IF conditions within the DO loop to ensure all combinations of first and last name searches are handled, particularly for fuzzy searches. The original code is critiqued for performing redundant string comparisons and not efficiently managing the search logic. Suggestions include consolidating the search conditions to streamline the code, using a single check for both names when fuzzy searches are enabled, and restructuring the code for better readability and efficiency. The importance of defining the context, such as the version of Visual Basic being used, is also highlighted to aid understanding. Overall, the conversation emphasizes enhancing code efficiency and clarity while addressing the functionality of the employee search feature.
Calculus!
Messages
20
Reaction score
0
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)
 
Technology news on Phys.org
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:
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 won't 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:
If 2+2=4 Then Boolean=True Else Boolean = False
or
Code:
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:
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.
 
Thank you very much. Everything that I had to do, you did. Thanks Again. :D
 
Calculus! said:
Thank you very much. Everything that I had to do, you did. Thanks Again. :D

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

Do you at least understand what I had done?
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
3
Views
4K
Replies
75
Views
6K
Replies
1
Views
3K
Replies
12
Views
2K
Replies
1
Views
2K
Back
Top