Fix IF Conditions in VB Code DO Loop

  • Thread starter Thread starter Calculus!
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around fixing and optimizing IF conditions within a DO loop in Visual Basic (VB) code for searching employee records based on first and last names. The focus is on handling various combinations of search criteria and improving code efficiency.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant requests improvements to the IF conditions to ensure all combinations of first and last name searches are handled.
  • Another participant suggests consolidating string comparisons to reduce redundancy in the code.
  • There are discussions about the use of fuzzy search logic and how to implement it effectively within the loop.
  • Some participants propose restructuring the code for better readability and efficiency, including moving declarations and handling file operations more effectively.
  • One participant mentions the potential for using an array to store employee records for better data handling.
  • Participants express uncertainty about the best practices for error handling and the implications of exiting subroutines early.

Areas of Agreement / Disagreement

Participants generally agree on the need for improvements in the code but do not reach a consensus on the best approach to implement those changes. Multiple competing views on code structure and logic remain present.

Contextual Notes

There are limitations regarding the clarity of pre-created form objects and the specific version of VB being used, which may affect the applicability of some suggestions. Additionally, some participants express differing opinions on error handling practices.

Who May Find This Useful

This discussion may be useful for individuals working on similar VB coding assignments, particularly those focused on string manipulation and file handling in employee management systems.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K