2

I am Trying to Create a Library management system, I am a beginner at coding. The Problem I am having is I want to search my books database by title in Visual Basic, using a Textbox and Search button and wanting it to display the results in an seperate form. How would I go about a search in my database in visual basic.

I have imported my database into visual basic. I have used the query below to make it work in Microsoft Access, but couldn't in Visual Basic. The Query I used in Microsoft access was this:

SELECT Books.[Book ID], Books.Title, Books.Author, Books.Category, Books.Location, Books.[Fiction/Non-Fiction], Books.Loaned

FROM Books

WHERE (((Books.Title) Like [Search Certin Title] & "*"));

Please help me in this regard.

1
  • I Have Figured out how to bring in results from my database into my form, now all i need help with is to make the code below work in visual basic like it does in Microsoft Access code: WHERE (((Books.Title) Like [Search Certin Title] & "*")); With the use of a Search Box Commented Jul 23, 2015 at 1:52

2 Answers 2

2

I have found someone elses code and have modified it to work with my application http://www.sattsoft.com/sourcecodes/details/1/4/vb-net-add-edit-delete-and-search-data-from-access-database.html

The Code used is as followed:

  Private Sub search_btn_Click(sender As Object, e As EventArgs) Handles search_btn.Click
    Searched_Books_frm.Show()
    Search_Record()

End Sub
Private Sub Search_Record()
    'The Code Below is not Mine, But I modified it to work with my code. This Code below belongs to Christopher Tubig:

    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim sSQL As String = String.Empty

    Try
        'get connection string declared in the Module1.vb and assing it to conn variable
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        sSQL = "SELECT Books.[Book ID], Books.Title, Books.Author, Books.Category, Books.Location, Books.[Fiction/Non-Fiction], Books.Loaned FROM Books"
        sSQL = sSQL & " Where Books.Title like '%" & Me.search_txt.Text & "%'"

        cmd.CommandText = sSQL
        da.SelectCommand = cmd
        da.Fill(dt)

        Searched_Books_frm.search_datagrid.DataSource = dt
        If dt.Rows.Count = 0 Then
            MsgBox("No record found!")
        End If

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

This works perfectly fine for me :)

Sign up to request clarification or add additional context in comments.

Comments

1

You will need to connect to the database by using either ODBC, ADO or DAO connectivity. You will need to use a specific connection string, dependant on which option you decide to take.

ADO (ActiveX Data Objects) uses OLEDB, so you should use one of the Jet or ACE connection strings. Below is an example of connecting to an Access Database using ADO within VBA.

Option Explicit
Sub queryADO()
    Private Const pw = "password"
    Dim rsData As ADODB.Recordset, rsConn As ADODB.Connection
    Dim strSQL As String, strConn As String
    Dim wb As Workbook, ws As Worksheet

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("mySheet")
    strConn = "C:\Users\lturner\Documents\myDatabase.accdb"
    Set rsConn = New ADODB.Connection
        With rsConn
            .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                strConn & ";Jet OLEDB:Database Password=" & pwd
            .Open
        End With
    strSQL = "SELECT * FROM myTable"
    Set rsData = rsConn.Execute(strSQL)
    ws.Range("A1").CopyFromRecordset rsData
    Set rsData = Nothing
    Set rsConn = Nothing
End Sub

The above will copy the query results into cell A1. You should easily be able to adapt the above to populate a userform with the query results. If you need any help, let me know.

You can find a comparison between ADO, DAO and ODBC connections here.

4 Comments

Don't Know if this will help but, I imported the database in visual basic as Microsoft Access not as SQL, and all I want to do is to be able to search a book title and the results pop up in a different form (the results taken from my database). @Iturner Your Answer kinda just flew over my head as I am only a Begginer and couldn't understand half the stuff written (sorry)
Are you trying to return results into a UserForm in Excel, or a Form within Access?
I am trying to return the results from microsoft access into a windows application form in visual basic.
I Have Figured out how to bring in results from my database into my form, now all i need help with is to make the code below work in visual basic like it does in Microsoft Access code: WHERE (((Books.Title) Like [Search Certin Title] & "*")); With the use of a Search Box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.