Fix IF Conditions in VB Code DO Loop

  • Thread starter Calculus!
  • Start date
  • Tags
    Code
In summary: In short, if the first name and last name don't match, check if the last name is the same as the first name, and if it is, then the employee is found; if the first name and last name don't match, check if the last name is different than the first name, and if it is, then the employee is not found. End If...)End IfIn summary, the code needs to be fixed to handle all possible combinations of first name and last name searches.
  • #1
Calculus!
20
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
  • #2
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.
 
  • #3
Thank you very much. Everything that I had to do, you did. Thanks Again. :D
 
  • #4
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?
 
  • #5
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.
 

Related to Fix IF Conditions in VB Code DO Loop

1. How do I fix an IF condition in VB code?

To fix an IF condition in VB code, you need to carefully examine the logical expression used in the condition and make sure it is properly evaluating the desired outcome. You may also need to check the syntax of the IF statement and make sure it is correctly written.

2. Why is my VB code not looping correctly?

There could be several reasons why your VB code is not looping correctly. One common issue is that the loop condition is not being met, so the loop is not executing at all. Another possibility is that the loop structure is incorrect, such as using the wrong type of loop or placing the wrong statements inside the loop.

3. How can I prevent infinite loops in my VB code?

To prevent infinite loops in VB code, you need to make sure that the loop condition is always changing or that there is a way to break out of the loop. You can also use a counter variable to limit the number of times the loop executes.

4. What is the purpose of a DO loop in VB code?

A DO loop in VB code is used to repeat a set of statements until a certain condition is met. This allows for efficient and concise code, as well as the ability to automate repetitive tasks.

5. Can I use multiple IF conditions in a DO loop in VB code?

Yes, you can use multiple IF conditions in a DO loop in VB code. This can be useful for creating more complex logic and controlling the flow of the loop based on different conditions.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
Back
Top