Visual Basic Online Course - WildCards in Database

Visual Basic Online Course

Using Wild Cards when searching Database

Problem : I have 50 records of mobile phone numbers and they start with different digits according to network area code, I want to search the [MobileNo] record from [TextBox] so when I write the first digit i get some search result and the second digit returns the next search result .... etc as i go, using TextBox1_Change() event, i want when every time i type a digit i get a result in the DataGrid control

Solution Using ADO + MS-Access 2003 + Sql Statements

'Visual Basic 6.0 Lessons and Tricks
'How to use the Wildcards in Visual Basic 6.0
'to create a custom search
Option Explicit
Public CN As New ADODB.Connection
Public RS As New ADODB.Recordset
Private Sub Form_Load()
If CN.State = 1 Then CN.Close
CN.Open ("Provider = Microsoft.Jet.Oledb.4.0 ; Data Source = " & App.Path & "/test.mdb")
End Sub
Private Sub Text1_Change()
If RS.State = 1 Then RS.Close
RS.CursorLocation = adUseClient
RS.Open ("Select * from students where mobNo LIKE '%" & Text1.Text & "%'"), CN, adOpenDynamic, adLockOptimistic
End Sub
view raw gistfile1.vb hosted with ❤ by GitHub

That means : Search all records that contain these values within.
But, If you want to search for records start with certain values, then use :
'Visual Basic Online Course
'Wild Cards in Database - Sql Statement
Dim SqlStr As String
SqlStr = ("Select * from students where mobNo LIKE '" & Text1.Text & "%'")
RS.Open (SqlStr), CN, adOpenDynamic, adLockOptimistic
view raw gistfile1.vb hosted with ❤ by GitHub

And, If you want to search for records end with certain values, then use :
'Visual Basic Online Course
'Select Column From Table Where Value Like Value
Dim SqlStr As String
SqlStr = ( "Select * from students where mobNo LIKE '%" & Text1.Text & "'")
RS.Open (SqlStr), CN, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = RS
DataGrid1.Refresh
End Sub
view raw gistfile1.vb hosted with ❤ by GitHub


Comments

  1. Thanks on your marvelous posting! I quite
    enjoyed reading it, you are a great author. I will be sure to
    bookmark your blog and will eventually come back very soon. I want
    to encourage you continue your great writing, have a nice
    weekend!

    my web blog: EdwardoZMosure

    ReplyDelete
  2. Hey there! I've been following your web site for a while now and finally got the courage to go
    ahead and give you a shout out from Porter Texas!

    Just wanted to tell you keep up the fantastic work!


    Here is my web blog; JesicaWMiskinis

    ReplyDelete
  3. Greetings! Very helpful advice within this article! It's the little changes
    which will make the most important changes. Thanks for sharing!


    Feel free to surf to my blog :: BernieceEHolstine

    ReplyDelete
  4. Do you have a spam problem on this site; I also am a blogger, and I was curious about your
    situation; many of us have created some nice methods and we are looking to
    exchange techniques with other folks, why not shoot me an e-mail if interested.



    Here is my web blog :: LareeSThorin

    ReplyDelete

Post a Comment

visual basic 6 source code says (Hi)

VB6 Popular Posts

Visual Basic Online Course - Function Keys (F1 to F12)

VB 0.6 TreeView Control lesson Part1

VB 0.6 Copy, Paste, Cut and Create Folders