2
votes

What does this Visual Basic 6.0 code below do? However it has been used for a search function, I'm not clear with it. So please explain what it does.

Private Sub cmdSearch_Click()
    Dim key As Integer, str As String
    key = InputBox("Enter the Employee No whose details u want to know: ")
    Set rs = Nothing
    str = "select * from emp where e_no=" & key
    rs.Open str, adoconn, adOpenForwardOnly, adLockReadOnly
    txtNo.Text = rs(0)
    txtName.Text = rs(1)
    txtCity.Text = rs(2)
    txtDob.Text = rs(4)
    txtPhone.Text = rs(3)
    Set rs = Nothing
    str = "select * from emp"
    rs.Open str, adoconn, adOpenDynamic, adLockPessimistic
End Sub
4
I'm wondering about rs. It is set to nothing just before a method is called on it. I know that is legal, but it is normally frowned upon. - Jonathan Allen
Ya that's the point i too got stucked! - Ant's

4 Answers

5
votes

The point no one yet has explicitly said is rs must be declared As New RecordSet so that the Set rs = Nothing just means effectively the same as Set rs = New RecordSet.

3
votes
Private Sub cmdSearch_Click()
    Dim key As Integer, str As String
    key = InputBox("Enter the Employee No whose details u want to know: ") ''// query the user for a name
    Set rs = Nothing
    str = "select * from emp where e_no=" & key ''//create sql query on the fly
    rs.Open str, adoconn, adOpenForwardOnly, adLockReadOnly ''// create a connection to an sql database
    txtNo.Text = rs(0) ''//assign the results of the query to input fields or labels
    txtName.Text = rs(1)
    txtCity.Text = rs(2)
    txtDob.Text = rs(4)
    txtPhone.Text = rs(3)
    Set rs = Nothing
    str = "select * from emp"
    rs.Open str, adoconn, adOpenDynamic, adLockPessimistic ''// creates a new sql connection and load the whole emp table
End Sub

Short summary: Ask the user for a name and display the data of the user in labels or textboxes.

1
votes

It makes two searches against the database. The result of the first search is used to populate some text boxes. The results of the second search... well I have no idea what they are doing with it.

1
votes

It looks up data from a database based on a column called e_no, loads information from the found row into TextBox controls, and then re-queries the database for all emp rows.