Friends please help me... I have Three text boxes 1.Bank 2.Acno 3.Name I am selecting record from database where Bank and Acno are =(txtbank.text and txtacno.text) If any record found, the focus should go to the third textbox-'Name' else message "Record not found" should be displayed.I tried the following code but no success.

CN.Open()
SQL = "SELECT *  from Receipts WHERE Bank='" & TxtBank.Text & "'" & "AND Acno='" & TxtAcno.Text & "'"
DA = New OleDb.OleDbDataAdapter(SQL, CN)
DA.Fill(DS, "Deposit")
If DS.Tables(0).Rows.Count = 0 Then
MsgBox("No Such Account")
Else
TxtName.Focus()
End If

Dani AI

Generated

Quick recap: wanted to look up a record by Bank + Acno, fill TxtName with the account holder and set focus there, or show "No such account" if nothing matches. was right that you assign the textbox via TxtName.Text = ..., and supplied a working DataReader approach. Common causes for the original symptom (no message shown but focus still moving) are SQL built by string concatenation, swallowed exceptions (empty Catch), or incorrect table/index checks.

Practical, safe pattern to use

  • Always parameterize queries (prevents quoting bugs and SQL injection).
  • Use Using blocks so connections/commands are closed automatically.
  • Check for DBNull before assigning to the textbox.
  • If Acno is numeric in the DB, pass it as a numeric parameter rather than a quoted string.
  • Avoid empty Catch blocks; surface the exception while debugging.

Example (compact, different from the posted samples):

' use OleDb positional parameters (question marks) and ExecuteScalar for a single value
Using cn As New System.Data.OleDb.OleDbConnection(CN.ConnectionString)
    Using cmd As New System.Data.OleDb.OleDbCommand("SELECT [Name] FROM Receipts WHERE [Bank]=? AND [Acno]=?", cn)
        cmd.Parameters.AddWithValue("?", TxtBank.Text.Trim())
        cmd.Parameters.AddWithValue("?", TxtAcno.Text.Trim())
        cn.Open()
        Dim nameObj As Object = cmd.ExecuteScalar()
        If nameObj IsNot Nothing AndAlso nameObj IsNot DBNull.Value Then
            TxtName.Text = nameObj.ToString()
            TxtName.Focus()
        Else
            MessageBox.Show("No such account")
            TxtAcno.Focus()
        End If
    End Using
End Using

Quick debug checklist

  • Remove empty Catch blocks and show/log ex.Message so you know if something fails.
  • If using DataAdapter/DataSet, verify DS.Tables.Contains("Deposit") before indexing.
  • Confirm column names (wrap reserved words like Name in brackets: [Name]).
  • Test the SQL directly in the DB tool with sample inputs to rule out data/type mismatches.
  • If updating controls from background threads, marshal back to the UI thread.

This approach is more robust, easier to debug, and avoids the subtle bugs that come from concatenated SQL and suppressed exceptions.

Recommended Answers

All 7 Replies

TxtName.Text = "value" is how you assign to the TxtName text field

String value -Name of the a/c holder.It is retrieved from the table if "record is found". But does it affect the selection and record count? I think,txtName Will not come to the picture till the selection and record count is over.

There are so many experts in this forum,I know. Many times they have helped me much.But this time.. Being very new to VB.net my questions may be very silly.Don't disregard me. Please help please please.....

is there any error ? if yes what is it ?
what are you trying to do, just focus on name textbox or get the name value.

I want this ..if record exists name value should be reflected in txtname,simultaneously the focus should be shifted to txtName, if no record exists message "No such account" should be shown. On working my code neither name value is shown nor message is displayed, still the focus is shifted to txtName.

ok here's the code

        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader
        Try
            CN.Open()
            cmd = New OleDbCommand("SELECT *  from Receipts WHERE Bank='" & TxtBank.Text & "'" & "AND Acno='" & TxtAcno.Text & "'", CN)
            dr = cmd.ExecuteReader
            If dr.Read Then
                TxtName.Text = dr("Name")
                TxtName.Focus()
            Else
                MsgBox("No Such Account")
            End If
        Catch
        End Try
        dr.Close()
        cn.Close()

Ok Thank You.....I got it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.