CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2011

    How Search Database using Inputbox

    Dear all I working with a project that created in VB6. I use MS Access 1997 as database and connect the database with VB6 using adodb connection. I am new i VB6 programming. I use a listview control to view the records. My database have a column "SEQURITY_NAME". Now I want to search a SEQURITY_NAMES using a inputbox from the database and also want to display the search result in the listview control.

    I am using these commands in my project:-

    Dim CON As New ADODB.Connection
    Dim RS As New ADODB.Recordset

    Private Sub Command1_Click()
    With RS
    !Date = Text1.Text
    !SEQURITY_NAME = Text2.Text
    !d_open = Text3.Text
    !d_high = Text4.Text
    !D_LOw = Text5.Text
    !d_close = Text6.Text
    !p_close = Text7.Text
    !d_p_l = Text8.Text
    End With
    End Sub

    Private Sub Form_Load()
    CON.Open ("provider=microsoft.jet.oledb.4.0;data source=" & App.Path & "\DB1.mdb")
    RS.Open "select * from EQUITY", CON, adOpenDynamic, adLockOptimistic
    Dim itm1 As ListItem
    With RS
    Do Until .EOF
    Set itm1 = lv1.ListItems.Add(, , !Date & "")
    itm1.SubItems(1) = !SEQURITY_NAME & ""
    itm1.SubItems(2) = !d_open & ""
    itm1.SubItems(3) = !d_high & ""
    itm1.SubItems(4) = !D_LOw & ""
    itm1.SubItems(5) = !d_close & ""
    itm1.SubItems(6) = !p_close & ""
    itm1.SubItems(7) = !d_p_l & ""
    Set itm1 = Nothing
    End With
    End Sub

    Private Sub lv1_DblClick()
    Text1.Text = lv1.SelectedItem.Text
    Text2.Text = lv1.SelectedItem.SubItems(1)
    Text3.Text = lv1.SelectedItem.SubItems(2)
    Text4.Text = lv1.SelectedItem.SubItems(3)
    Text5.Text = lv1.SelectedItem.SubItems(4)
    Text6.Text = lv1.SelectedItem.SubItems(5)
    Text7.Text = lv1.SelectedItem.SubItems(6)
    Text8.Text = lv1.SelectedItem.SubItems(7)
    End Sub

    Private Sub Text7_Change()
    Text8.Text = Val(Text7) - Val(Text6)
    End Sub

    Plz. Plz. friends help me regarding this.

  2. #2
    Join Date
    Jan 2006
    Fox Lake, IL

    Re: How Search Database using Inputbox

    If you set SORTED=TRUE, you can type into a LISTVIEW, as I recall. Might be thinking of a LISTBOX, though

    Try the sample here http://www.codeguru.com/vb/gen/vb_da...le.php/c14613/

    I always use a FLEXGRID, which puts things into a TABLE type view. There is also a MSFLEXGRID, which allows versatility.

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    Re: How Search Database using Inputbox

    You would need to execute a query with a where clause based on your input and then if results are returned clear your listview and add the items returned in your new recordset.

    Based on the code you have posted I would advise taking the code out of the form load event and place it in a sub routine. You can call this sub routine from form load if need be and you can call it from elsewhere as well. Add a parameter to the sub routine to let it know if it should prompt or not.

    Sub GrabData(PromptUser as Boolean)
    If PromptUser then
         Dim qValue as string
         qValue=Inputbox("Search for what")
         if qvalue<>"" then
            RS.Open "select * from EQUITY where SEQURITY_NAME ='" & qvalue &"'", CON, adOpenDynamic, adLockOptimistic
            RS.Open "select * from EQUITY ", CON, adOpenDynamic, adLockOptimistic
        End if
        RS.Open "select * from EQUITY ", CON, adOpenDynamic, adLockOptimistic
    end if
    '.... rest of code from form load that follows the open statement
    Don't forget to place a line to clear the listview before adding items else items will be appended to what was there before when the routine is called again. Also remember to close your recordset and connection when you have finished with them. You should never open a local recordset in a sub or function that you do not close before exiting the sub or function.
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

By using this site, you agree to the Privacy Policy